*/
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(
'`
(.+)\s+
`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('`/> ([^\s]+)`', $match[1], $authorMatch);
preg_match('` (\d+)`', $match[1], $viewsMatch);
preg_match(
'``',
$match[1],
$titleMatch
);
preg_match_all('`(.+)`', $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;
}
}