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
->setName('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");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$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 {
$streams = $liveCoding->getStreams();
$streams = $liveCoding->getStreams($language, $difficulty);
foreach ($streams as $stream) {
$output->writeln(sprintf(
@ -47,8 +65,8 @@ class ListCommand extends Command
'<info>Languages:</info> %s',
implode(', ', $stream->getLanguages())
));
$output->writeln(sprintf(
$output->writeln(sprintf(
'<info>Difficulty:</info> %s',
$stream->getDifficulty()
));

View file

@ -18,9 +18,41 @@ class LiveCoding
$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(
'`<div class="small-video-box">(.+)\s+</div>`isU',
@ -57,8 +89,8 @@ class LiveCoding
if (!empty($languages)) {
$difficulty = trim(array_pop($languages), '()');
} else {
$difficulty = null;
}
$difficulty = null;
}
$stream = new Stream();
$stream
@ -67,7 +99,7 @@ class LiveCoding
->setCountry($countryMatch[1])
->setAuthor($authorMatch[1])
->setLanguages($languages)
->setDifficulty($difficulty)
->setDifficulty($difficulty)
->setUrl($this->app->buildUrl($urlMatch[1]));
$streams[] = $stream;