livecoding-console/src/Deblan/Helper/LiveCoding.php
2015-07-14 01:28:36 +02:00

110 lines
2.9 KiB
PHP

<?php
namespace Deblan\Helper;
use Deblan\Application;
use Deblan\Model\Stream;
/**
* Class LiveCoding
* @author Simon Vieille <simon@deblan.fr>
*/
class LiveCoding
{
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function getLanguages()
{
return [
'objc-swift-ios' => 'Obj-C/Swift (iOS)',
'java' => 'Java',
'android' => 'Android',
'c-cplusplus' => 'C/C++',
'csharp-dotnet' => 'C#/.NET',
'python' => 'Python',
'javascript' => 'JavaScript',
'php' => 'PHP',
'ruby"' => 'Ruby',
'sql"' => 'SQL',
'html-css' => 'HTML/CSS',
'others' => 'Others',
];
}
public function getDifficulties()
{
return [
1 => 'beginner',
2 => 'intermediate',
3 => 'expert',
];
}
public function getStreams($language = null, $difficulty = null)
{
$content = file_get_contents($this->app->buildUrl(sprintf(
'livestreams/%s%s',
$language !== null ? $language.'/' : 'all',
$difficulty !== null ? '/'.$difficulty.'/' : null
)));
preg_match_all(
'`<div class="small-video-box">(.+)\s+</div>`isU',
$content,
$matches,
PREG_SET_ORDER
);
if (empty($matches)) {
return [];
}
$streams = [];
foreach ($matches as $match) {
preg_match('`href="([^"]+)"`', $match[1], $urlMatch);
preg_match('`data-title="([^"]+)"`', $match[1], $countryMatch);
preg_match('`/>&nbsp;([^\s]+)`', $match[1], $authorMatch);
preg_match('`</i>&nbsp;(\d+)</span>`', $match[1], $viewsMatch);
preg_match(
'`<img class="thumbnail thumbnail-250x140" src=".+" alt="(.*)" />`',
$match[1],
$titleMatch
);
preg_match_all('`<span class="item">(.+)</span>`', $match[1], $languagesMatches, PREG_SET_ORDER);
$languages = [];
foreach ($languagesMatches as $languagesMatch) {
$languages[] = $languagesMatch[1];
}
if (!empty($languages)) {
$difficulty = trim(array_pop($languages), '()');
} else {
$difficulty = null;
}
$stream = new Stream();
$stream
->setTitle(html_entity_decode(trim($titleMatch[1])))
->setViews($viewsMatch[1])
->setCountry($countryMatch[1])
->setAuthor($authorMatch[1])
->setLanguages($languages)
->setDifficulty($difficulty)
->setUrl($this->app->buildUrl($urlMatch[1]));
$streams[] = $stream;
}
return $streams;
}
}