This commit is contained in:
Simon Vieille 2015-07-14 00:19:59 +02:00
parent 050f47afaf
commit f43ca2e22d
4 changed files with 265 additions and 1 deletions

View file

@ -0,0 +1,78 @@
<?php
namespace Deblan\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Exception;
class ListCommand extends Command
{
protected function configure()
{
$this
->setName('streams')
->setDescription('List available streams.')
->addOption('category', null, InputOption::VALUE_REQUIRED, '')
->setHelp("The <info>%command.name%</info> lists live streams");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// $this->getContainer()->get('foo.bar');
// $output->writeln(sprintf('<comment>%s</comment> bar.', $example));
// $input->getArgument('foo');
// $input->getOption('bar');
$liveCoding = $this->getApplication()->getContainer()['livecoding'];
try {
$streams = $liveCoding->getStreams();
foreach ($streams as $stream) {
$output->writeln(sprintf(
'<comment>%s</comment>',
$stream->getTitle()
));
$output->writeln(sprintf(
'<comment>%s</comment>',
str_repeat('-', mb_strlen($stream->getTitle()))
));
$output->writeln(sprintf(
'<info>Author:</info> %s',
$stream->getAuthor()
));
if (count($stream->getLanguages())) {
$output->writeln(sprintf(
'<info>Languages:</info> %s',
implode(', ', $stream->getLanguages())
));
$output->writeln(sprintf(
'<info>Difficulty:</info> %s',
$stream->getDifficulty()
));
}
$output->writeln(sprintf(
'<info>URL:</info> %s',
$stream->getUrl()
));
$output->writeln(sprintf(
'<info>Views:</info> %d',
$stream->getViews()
));
$output->writeln('');
}
} catch (Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
}
}
}

View file

@ -25,7 +25,7 @@ class PlayCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = $this->getApplication()->getConfigLoader()->getConfig();
$stream = trim($input->getArgument('stream'), "/ \r\n");
$stream = strtolower(trim($input->getArgument('stream'), "/ \r\n"));
if (0 === preg_match('/^\w+$/', $stream)) {
$output->writeln('<error>The stream name is not valid.');

View file

@ -0,0 +1,78 @@
<?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;
}
}

108
src/Deblan/Model/Stream.php Normal file
View file

@ -0,0 +1,108 @@
<?php
namespace Deblan\Model;
/**
* Class Stream
* @author Simon Vieille <simon@deblan.fr>
*/
class Stream
{
protected $country;
protected $author;
protected $languages = [];
protected $difficulty;
protected $url;
protected $title;
protected $views;
public function setCountry($country)
{
$this->country = $country;
return $this;
}
public function getCountry()
{
return $this->country;
}
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
public function getAuthor()
{
return $this->author;
}
public function setLanguages(array $languages)
{
$this->languages = $languages;
return $this;
}
public function getLanguages()
{
return $this->languages;
}
public function setUrl($url)
{
$this->url = $url;
return $this;
}
public function getUrl()
{
return $this->url;
}
public function setViews($views)
{
$this->views = (int) $views;
return $this;
}
public function getViews()
{
return $this->views;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setDifficulty($difficulty)
{
$this->difficulty = $difficulty;
return $this;
}
public function getDifficulty()
{
return $this->difficulty;
}
}