From 9ad024888ab2273734f71d214e811ad6a2d960d5 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 13 Oct 2016 16:40:19 +0200 Subject: [PATCH] Refactor getter classes --- classes/VideoDownload.php | 83 ++++++++++++++------------------------- 1 file changed, 29 insertions(+), 54 deletions(-) diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index c7aba78..39fa401 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -48,47 +48,11 @@ class VideoDownload return explode(PHP_EOL, trim($process->getOutput())); } - /** - * Get all information about a video. - * - * @param string $url URL of page - * @param string $format Format to use for the video - * - * @return object Decoded JSON - * */ - public function getJSON($url, $format = null) + private function getProp($url, $format = null, $prop = 'dump-json') { $this->procBuilder->setArguments( [ - '--dump-json', - $url, - ] - ); - if (isset($format)) { - $this->procBuilder->add('-f '.$format); - } - $process = $this->procBuilder->getProcess(); - $process->run(); - if (!$process->isSuccessful()) { - throw new \Exception($process->getErrorOutput()); - } else { - return json_decode($process->getOutput()); - } - } - - /** - * Get URL of video from URL of page. - * - * @param string $url URL of page - * @param string $format Format to use for the video - * - * @return string URL of video - * */ - public function getURL($url, $format = null) - { - $this->procBuilder->setArguments( - [ - '--get-url', + '--'.$prop, $url, ] ); @@ -104,6 +68,32 @@ class VideoDownload } } + /** + * Get all information about a video. + * + * @param string $url URL of page + * @param string $format Format to use for the video + * + * @return object Decoded JSON + * */ + public function getJSON($url, $format = null) + { + return json_decode($this->getProp($url, $format, 'dump-json')); + } + + /** + * Get URL of video from URL of page. + * + * @param string $url URL of page + * @param string $format Format to use for the video + * + * @return string URL of video + * */ + public function getURL($url, $format = null) + { + return $this->getProp($url, $format, 'get-url'); + } + /** * Get filename of video file from URL of page. * @@ -114,22 +104,7 @@ class VideoDownload * */ public function getFilename($url, $format = null) { - $this->procBuilder->setArguments( - [ - '--get-filename', - $url, - ] - ); - if (isset($format)) { - $this->procBuilder->add('-f '.$format); - } - $process = $this->procBuilder->getProcess(); - $process->run(); - if (!$process->isSuccessful()) { - throw new \Exception($process->getErrorOutput()); - } else { - return trim($process->getOutput()); - } + return trim($this->getProp($url, $format, 'get-filename')); } /**