Streams listing with language and difficulty filters

This commit is contained in:
Simon Vieille 2015-07-14 00:47:49 +02:00
parent eb705fdc78
commit 497ef179af
2 changed files with 59 additions and 9 deletions

View file

@ -15,16 +15,34 @@ class ListCommand extends Command
$this $this
->setName('streams') ->setName('streams')
->setDescription('List available streams.') ->setDescription('List available streams.')
->addOption('category', null, InputOption::VALUE_REQUIRED, '') ->addOption('language', null, InputOption::VALUE_REQUIRED, '')
->addOption('difficulty', null, InputOption::VALUE_REQUIRED, '')
->setHelp("The <info>%command.name%</info> lists live streams"); ->setHelp("The <info>%command.name%</info> lists live streams");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$liveCoding = $this->getApplication()->getContainer()['livecoding']; $liveCoding = $this->getApplication()->getContainer()['livecoding'];
$languages = $liveCoding->getLanguages();
$difficulties = $liveCoding->getDifficulties();
$language = $input->getOption('language');
$difficulty = $input->getOption('difficulty');
if (in_array($language, $languages)) {
$language = array_keys($languages, $language)[0];
} elseif (!in_array($language, array_keys($languages))) {
$language = null;
}
if (in_array($difficulty, array_keys($difficulties))) {
$difficulty = $difficulties[$difficulty];
} elseif (!in_array($difficulty, $difficulties)) {
$difficulty = null;
}
try { try {
$streams = $liveCoding->getStreams(); $streams = $liveCoding->getStreams($language, $difficulty);
foreach ($streams as $stream) { foreach ($streams as $stream) {
$output->writeln(sprintf( $output->writeln(sprintf(

View file

@ -18,9 +18,41 @@ class LiveCoding
$this->app = $app; $this->app = $app;
} }
public function getStreams($category = null) public function getLanguages()
{ {
$content = file_get_contents($this->app->buildUrl('livestreams/')); 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( preg_match_all(
'`<div class="small-video-box">(.+)\s+</div>`isU', '`<div class="small-video-box">(.+)\s+</div>`isU',