livecoding-console/src/Deblan/Helper/LiveCoding.php
2015-07-14 00:19:59 +02:00

79 lines
2 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 getStreams($category = null)
{
$content = file_get_contents($this->app->buildUrl('livestreams/'));
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(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;
}
}