diff --git a/classes/Config.php b/classes/Config.php index 69cb26b..208e85b 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -1,78 +1,88 @@ file != $yamlfile) { - self::$instance = new Config($yamlfile); + self::$instance = new self($yamlfile); } + return self::$instance; } /** - * Destroy singleton instance + * Destroy singleton instance. + * * @return void */ public static function destroyInstance() diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 5023a20..e497113 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -1,21 +1,19 @@ procBuilder = new ProcessBuilder(); $this->procBuilder->setPrefix( array_merge( - array($this->config->python, $this->config->youtubedl), + [$this->config->python, $this->config->youtubedl], $this->config->params ) ); } /** - * List all extractors + * List all extractors. * * @return string[] Extractors * */ public function listExtractors() { $this->procBuilder->setArguments( - array( - '--list-extractors' - ) + [ + '--list-extractors', + ] ); $process = $this->procBuilder->getProcess(); $process->run(); + return explode(PHP_EOL, trim($process->getOutput())); } /** - * Get all information about a video + * Get all information about a video. * * @param string $url URL of page * @param string $format Format to use for the video @@ -57,10 +56,10 @@ class VideoDownload public function getJSON($url, $format = null) { $this->procBuilder->setArguments( - array( + [ '--dump-json', - $url - ) + $url, + ] ); if (isset($format)) { $this->procBuilder->add('-f '.$format); @@ -75,7 +74,7 @@ class VideoDownload } /** - * Get URL of video from URL of page + * Get URL of video from URL of page. * * @param string $url URL of page * @param string $format Format to use for the video @@ -85,10 +84,10 @@ class VideoDownload public function getURL($url, $format = null) { $this->procBuilder->setArguments( - array( + [ '--get-url', - $url - ) + $url, + ] ); if (isset($format)) { $this->procBuilder->add('-f '.$format); @@ -103,7 +102,7 @@ class VideoDownload } /** - * Get filename of video file from URL of page + * Get filename of video file from URL of page. * * @param string $url URL of page * @param string $format Format to use for the video @@ -113,10 +112,10 @@ class VideoDownload public function getFilename($url, $format = null) { $this->procBuilder->setArguments( - array( + [ '--get-filename', - $url - ) + $url, + ] ); if (isset($format)) { $this->procBuilder->add('-f '.$format); @@ -131,7 +130,7 @@ class VideoDownload } /** - * Get filename of audio from URL of page + * Get filename of audio from URL of page. * * @param string $url URL of page * @param string $format Format to use for the video @@ -151,7 +150,7 @@ class VideoDownload } /** - * Get audio stream of converted video + * Get audio stream of converted video. * * @param string $url URL of page * @param string $format Format to use for the video @@ -172,14 +171,14 @@ class VideoDownload $video->http_headers->{'User-Agent'} ); $avconvProc = ProcessBuilder::create( - array( + [ $this->config->avconv, '-v', 'quiet', '-i', '-', '-f', 'mp3', '-vn', - 'pipe:1' - ) + 'pipe:1', + ] ); if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') { @@ -187,13 +186,13 @@ class VideoDownload throw(new \Exception('Can\'t find rtmpdump')); } $builder = new ProcessBuilder( - array( + [ $this->config->rtmpdump, '-q', '-r', $video->url, - '--pageUrl', $video->webpage_url - ) + '--pageUrl', $video->webpage_url, + ] ); if (isset($video->player_url)) { $builder->add('--swfVfy'); @@ -226,19 +225,20 @@ class VideoDownload $chain = new Chain( ProcessBuilder::create( array_merge( - array( + [ $this->config->curl, '--silent', '--location', '--user-agent', $video->http_headers->{'User-Agent'}, - $video->url - ), + $video->url, + ], $this->config->curl_params ) ) ); $chain->add('|', $avconvProc); } + return popen($chain->getProcess()->getCommandLine(), 'r'); } } diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 177e636..e884fab 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -1,41 +1,45 @@ container->view->render( $response, 'index.tpl', - array( - 'convert'=>$this->config->convert, - 'class'=>'index', - 'description'=>'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.' - ) + [ + 'convert' => $this->config->convert, + 'class' => 'index', + 'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.', + ] ); } /** - * Display a list of extractors + * Display a list of extractors. * * @param Request $request PSR-7 request * @param Response $response PSR-7 response @@ -79,18 +83,17 @@ class FrontController $this->container->view->render( $response, 'extractors.tpl', - array( - 'extractors'=>$this->download->listExtractors(), - 'class'=>'extractors', - 'title'=>'Supported websites', - 'description' - =>'List of all supported websites from which Alltube Download can extract video or audio files' - ) + [ + 'extractors' => $this->download->listExtractors(), + 'class' => 'extractors', + 'title' => 'Supported websites', + 'description' => 'List of all supported websites from which Alltube Download can extract video or audio files', + ] ); } /** - * Dislay information about the video + * Dislay information about the video. * * @param Request $request PSR-7 request * @param Response $response PSR-7 response @@ -101,36 +104,38 @@ class FrontController { $params = $request->getQueryParams(); $this->config = Config::getInstance(); - if (isset($params["url"])) { + if (isset($params['url'])) { if (isset($params['audio'])) { try { - $url = $this->download->getURL($params["url"], 'mp3[protocol^=http]'); + $url = $this->download->getURL($params['url'], 'mp3[protocol^=http]'); + return $response->withRedirect($url); } catch (\Exception $e) { $response = $response->withHeader( 'Content-Disposition', 'attachment; filename="'. - $this->download->getAudioFilename($params["url"], 'bestaudio/best').'"' + $this->download->getAudioFilename($params['url'], 'bestaudio/best').'"' ); $response = $response->withHeader('Content-Type', 'audio/mpeg'); if ($request->isGet()) { - $process = $this->download->getAudioStream($params["url"], 'bestaudio/best'); + $process = $this->download->getAudioStream($params['url'], 'bestaudio/best'); $response = $response->withBody(new Stream($process)); } + return $response; } } else { - $video = $this->download->getJSON($params["url"]); + $video = $this->download->getJSON($params['url']); $this->container->view->render( $response, 'video.tpl', - array( - 'video'=>$video, - 'class'=>'video', - 'title'=>$video->title, - 'description'=>'Download "'.$video->title.'" from '.$video->extractor_key - ) + [ + 'video' => $video, + 'class' => 'video', + 'title' => $video->title, + 'description' => 'Download "'.$video->title.'" from '.$video->extractor_key, + ] ); } } else { @@ -139,10 +144,12 @@ class FrontController } /** - * Display an error page - * @param Request $request PSR-7 request - * @param Response $response PSR-7 response - * @param \Exception $exception Error to display + * Display an error page. + * + * @param Request $request PSR-7 request + * @param Response $response PSR-7 response + * @param \Exception $exception Error to display + * * @return Response HTTP response */ public function error(Request $request, Response $response, \Exception $exception) @@ -150,17 +157,18 @@ class FrontController $this->container->view->render( $response, 'error.tpl', - array( - 'errors'=>$exception->getMessage(), - 'class'=>'video', - 'title'=>'Error' - ) + [ + 'errors' => $exception->getMessage(), + 'class' => 'video', + 'title' => 'Error', + ] ); + return $response->withStatus(500); } /** - * Redirect to video file + * Redirect to video file. * * @param Request $request PSR-7 request * @param Response $response PSR-7 response @@ -170,19 +178,21 @@ class FrontController public function redirect(Request $request, Response $response) { $params = $request->getQueryParams(); - if (isset($params["url"])) { + if (isset($params['url'])) { try { - $url = $this->download->getURL($params["url"], $params["format"]); + $url = $this->download->getURL($params['url'], $params['format']); + return $response->withRedirect($url); } catch (\Exception $e) { $response->getBody()->write($e->getMessage()); + return $response->withHeader('Content-Type', 'text/plain'); } } } /** - * Output JSON info about the video + * Output JSON info about the video. * * @param Request $request PSR-7 request * @param Response $response PSR-7 response @@ -192,13 +202,14 @@ class FrontController public function json(Request $request, Response $response) { $params = $request->getQueryParams(); - if (isset($params["url"])) { + if (isset($params['url'])) { try { - $video = $this->download->getJSON($params["url"]); + $video = $this->download->getJSON($params['url']); + return $response->withJson($video); } catch (\Exception $e) { return $response->withJson( - array('success'=>false, 'error'=>$e->getMessage()) + ['success' => false, 'error' => $e->getMessage()] ); } } diff --git a/index.php b/index.php index 60026bf..0653081 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ get( '/', - array($controller, 'index') + [$controller, 'index'] )->setName('index'); $app->get( '/extractors', - array($controller, 'extractors') + [$controller, 'extractors'] )->setName('extractors'); $app->get( '/video', - array($controller, 'video') + [$controller, 'video'] )->setName('video'); $app->get( '/redirect', - array($controller, 'redirect') + [$controller, 'redirect'] )->setName('redirect'); $app->get( '/json', - array($controller, 'json') + [$controller, 'json'] ); $app->run(); diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 6527ec3..9e7e1d8 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -1,25 +1,25 @@