From 4ad5d79f00e88a73898a801ab6530f036a617651 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 13 Jul 2015 22:32:34 +0200 Subject: [PATCH] console and play command --- bin/console | 2 + composer.json | 4 +- src/Deblan/.Application.php.swp | Bin 12288 -> 0 bytes src/Deblan/Application.php | 48 +++++++++++++++++- src/Deblan/Command/PlayCommand.php | 79 +++++++++++++++++++++++++++++ src/Deblan/ConfigLoader.php | 39 ++++++++++++++ 6 files changed, 169 insertions(+), 3 deletions(-) delete mode 100644 src/Deblan/.Application.php.swp create mode 100644 src/Deblan/Command/PlayCommand.php create mode 100644 src/Deblan/ConfigLoader.php diff --git a/bin/console b/bin/console index 40342d2..e97d952 100755 --- a/bin/console +++ b/bin/console @@ -4,10 +4,12 @@ require_once __DIR__ . '/../vendor/autoload.php'; use Deblan\Application; +use Deblan\ConfigLoader; $app = new Application('LiveCoding CLI', '1'); $app->chdir(__DIR__.'/../'); $app->addCommandsPath('src/Deblan/Command/', 'Deblan\\Command'); $app->loadCommands(); +$app->setConfigLoader(new ConfigLoader()); $app->run(); diff --git a/composer.json b/composer.json index 6970a3d..8fbc752 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,8 @@ "require": { "symfony/filesystem": "^2.7", "symfony/finder": "^2.7", - "symfony/console": "^2.7" + "symfony/console": "^2.7", + "symfony/yaml": "^2.7", + "symfony/process": "^2.7" } } diff --git a/src/Deblan/.Application.php.swp b/src/Deblan/.Application.php.swp deleted file mode 100644 index 481386dad8e6ebeb69f5d831733122183f13c9fc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&Jxjwt7zgmDZXJ9uz!SA4xPpU%Dp>ySbEV$FRc=f2_o@$U+qcCy-gE2eHqzw ze!#H$Li=FAB0hc7FDT=rZgf~{`b#6*?Y3;GIFd$RsW572dtMA9>msA1YlU)O)lFdK zZXEMp097?2MS2tY%l>Hv}L60SG_<0uX=z z1R(Iw1$>;NTYkoo@8{%C0R55swf{cE4gwH>00bZa0SG_<0uX=z1Rwx`AruH2M6Y8+ z<#E1we)<1-|Nr3Qcg_pvne)WyaUM8#9Lq7B15Smr!CB+1aQd}|&;qv%0SG_<0uX=z z1Rwwb2tWV=5XcBzQ=YfHl!6=973I5>|7vL$XNe8d9(7TxrA_RR~oVMbN6Ror{Dm}6HyAsJlfhci`*BaG1 R%@h;CpZc3bTiaconfigLoader = $configLoader; + + return $this; + } + + public function getConfigLoader() + { + return $this->configLoader; + } + + public function buildUrl($uri = '') + { + return sprintf('https://www.livecoding.tv/%s', ltrim($uri, '/')); + } + + public function buildStreamUrl($stream) + { + return sprintf( + 'rtmp://eumedia1.livecoding.tv:1935/livecodingtv/%s', + trim($stream, '/') + ); + } + + public function getPlayer($stream) + { + $stream = $this->buildStreamUrl($stream); + + return str_replace( + '%stream%', + escapeshellarg($stream), + $this->getConfigLoader()->getConfig()['player']['command'] + ); + } + + public function isDebugMode() + { + return $this->getConfigLoader()->getConfig()['debug']; + } +} diff --git a/src/Deblan/Command/PlayCommand.php b/src/Deblan/Command/PlayCommand.php new file mode 100644 index 0000000..da47823 --- /dev/null +++ b/src/Deblan/Command/PlayCommand.php @@ -0,0 +1,79 @@ +setName('play') + ->setDescription('Play a stream') + ->addArgument('stream', InputArgument::REQUIRED, 'Name of the stream') + // ->addOption('bar', null, InputOption::VALUE_NONE, '') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $config = $this->getApplication()->getConfigLoader()->getConfig(); + $stream = trim($input->getArgument('stream'), "/ \r\n"); + + if (0 === preg_match('/^\w+$/', $stream)) { + $output->writeln('The stream name is not valid.'); + + return false; + } + + $output->writeln(sprintf('Playing %s', $stream)); + + if (!empty($config['player']['before'])) { + $command = $config['player']['before']; + // $process = new Process($command); + // $process->run(); + } + + $hash = $this->getHash($this->getApplication()->buildUrl($stream.'/')); + $command = $this->getApplication()->getPlayer($stream.'?t='.$hash); + $process = new Process($command); + + if ($this->getApplication()->isDebugMode()) { + $callback = function ($type, $buffer) use ($output) { + if ('err' !== $type) { + $output->writeln($buffer); + } + }; + } else { + $callback = function ($type, $buffer) use ($output) { + if ('err' === $type) { + $output->writeln(sprintf('%s', $buffer)); + } else { + $output->writeln($buffer); + } + }; + } + + $process->run($callback); + } + + protected function getHash($streamUrl) + { + $page = file_get_contents($streamUrl); + + preg_match('/var streamURL = ".+\?t=([^"]+)"/s', $page, $match); + + if (!isset($match[1])) { + throw new RuntimeException('Hash is not accessible.'); + } + + return $match[1]; + } +} diff --git a/src/Deblan/ConfigLoader.php b/src/Deblan/ConfigLoader.php new file mode 100644 index 0000000..3c7a9e6 --- /dev/null +++ b/src/Deblan/ConfigLoader.php @@ -0,0 +1,39 @@ +configFile = $configFile; + $this->filesystem = new Filesystem(); + } + + public function configExists() + { + return $this->filesystem->exists($this->configFile); + } + + public function save(array $data) + { + $config = array_merge( + $this->getConfig(), + $data + ); + + $this->filesystem->dumpFile($this->configFile, Yaml::dump($config)); + } + + public function getConfig() + { + return $this->configExists() ? Yaml::parse($this->configFile) : []; + } +}