From c7fd0f32afaf479f6d8d52bf84205bc68cf5eab9 Mon Sep 17 00:00:00 2001 From: huangjx Date: Sat, 5 Mar 2022 09:58:05 +0800 Subject: [PATCH] used a faster version of youtube-dl(#36);fixed yt binary download issuse;cleaning up --- lib/Tools/Helper.php | 11 ++++++++++- lib/Tools/Youtube.php | 41 +++++++++++++---------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/Tools/Helper.php b/lib/Tools/Helper.php index 1f452bd..fd6641a 100644 --- a/lib/Tools/Helper.php +++ b/lib/Tools/Helper.php @@ -174,8 +174,17 @@ class Helper curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec($ch); + if (curl_errno($ch)) { + $error = 'Error:' . curl_error($ch); + } curl_close($ch); - file_put_contents($file, $result); + if (isset($error)) { + throw new \Exception($error); + } else { + if (!file_put_contents($file, $result)) { + throw new \Exception("failed to save " . $file); + } + } } public static function is_function_enabled($function_name) diff --git a/lib/Tools/Youtube.php b/lib/Tools/Youtube.php index f369be5..84eff0a 100644 --- a/lib/Tools/Youtube.php +++ b/lib/Tools/Youtube.php @@ -3,7 +3,6 @@ namespace OCA\NCDownloader\Tools; use OCA\NCDownloader\Tools\Helper; use OCA\NCDownloader\Tools\YoutubeHelper; -use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; class Youtube @@ -31,7 +30,7 @@ class Youtube if (isset($binary) && $this->isExecutable($binary)) { $this->bin = $binary; } else { - $this->bin = Helper::findBinaryPath('youtube-dl', __DIR__ . "/../../bin/youtube-dl"); + $this->bin = Helper::findBinaryPath('youtube-dl', __DIR__ . "/../../bin/yt-dlp"); } if ($this->isInstalled() && !$this->isExecutable()) { chmod($this->bin, 0744); @@ -90,7 +89,7 @@ class Youtube $this->setOption('--audio-format', $format); } - public function setvideoFormat($format) + public function setVideoFormat($format) { $this->videoFormat = $format; } @@ -122,29 +121,9 @@ class Youtube array_unshift($this->options, $option); } - public function downloadSync($url) - { - $this->downloadDir = $this->downloadDir ?? $this->defaultDir; - $this->prependOption($this->downloadDir . $this->outTpl); - $this->prependOption("--output"); - $this->setUrl($url); - $this->prependOption($this->bin); - // $this->buildCMD(); - $process = new Process($this->options, null, $this->env); - //the maximum time required to download the file - $process->setTimeout($this->timeout); - try { - $process->mustRun(); - $output = $process->getOutput(); - } catch (ProcessFailedException $exception) { - $output = $exception->getMessage(); - } - - return $output; - } - public function download($url) { + $this->install(); if ($this->audioOnly) { $this->audioMode(); } else { @@ -255,16 +234,22 @@ class Youtube { return $this->bin; } - public static function install() + public function install() { $url = $this->installUrl(); - $path = \OC::$server->getSystemConfig()->getValue('datadirectory'); - Helper::Download($url, $path . "/youtube-dl"); + $file = __DIR__ . "/../../bin/yt-dlp2"; + try { + Helper::Download($url, $file); + chmod($file, 0744); + } catch (\Exception $e) { + return $e->getMessage(); + } + return false; } public function installUrl() { - return "https://yt-dl.org/downloads/latest/youtube-dl"; + return "https://github.com/shiningw/ncdownloader-bin/raw/master/yt-dlp"; } }