Streams listing with language and difficulty filters
This commit is contained in:
parent
eb705fdc78
commit
497ef179af
|
@ -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(
|
||||||
|
@ -47,8 +65,8 @@ class ListCommand extends Command
|
||||||
'<info>Languages:</info> %s',
|
'<info>Languages:</info> %s',
|
||||||
implode(', ', $stream->getLanguages())
|
implode(', ', $stream->getLanguages())
|
||||||
));
|
));
|
||||||
|
|
||||||
$output->writeln(sprintf(
|
$output->writeln(sprintf(
|
||||||
'<info>Difficulty:</info> %s',
|
'<info>Difficulty:</info> %s',
|
||||||
$stream->getDifficulty()
|
$stream->getDifficulty()
|
||||||
));
|
));
|
||||||
|
|
|
@ -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',
|
||||||
|
@ -57,8 +89,8 @@ class LiveCoding
|
||||||
if (!empty($languages)) {
|
if (!empty($languages)) {
|
||||||
$difficulty = trim(array_pop($languages), '()');
|
$difficulty = trim(array_pop($languages), '()');
|
||||||
} else {
|
} else {
|
||||||
$difficulty = null;
|
$difficulty = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$stream = new Stream();
|
$stream = new Stream();
|
||||||
$stream
|
$stream
|
||||||
|
@ -67,7 +99,7 @@ class LiveCoding
|
||||||
->setCountry($countryMatch[1])
|
->setCountry($countryMatch[1])
|
||||||
->setAuthor($authorMatch[1])
|
->setAuthor($authorMatch[1])
|
||||||
->setLanguages($languages)
|
->setLanguages($languages)
|
||||||
->setDifficulty($difficulty)
|
->setDifficulty($difficulty)
|
||||||
->setUrl($this->app->buildUrl($urlMatch[1]));
|
->setUrl($this->app->buildUrl($urlMatch[1]));
|
||||||
|
|
||||||
$streams[] = $stream;
|
$streams[] = $stream;
|
||||||
|
|
Loading…
Reference in a new issue