diff --git a/lib/Tools/Youtube.php b/lib/Tools/Youtube.php index cedfae1..e1e94b3 100644 --- a/lib/Tools/Youtube.php +++ b/lib/Tools/Youtube.php @@ -141,7 +141,6 @@ class Youtube $this->setOption("--output", $this->downloadDir . "/" . $this->outTpl); $this->setUrl($url); $this->prependOption($this->bin); - //\OC::$server->getLogger()->error($process->getCommandLine(), ['app' => 'PHP']); $process = new Process($this->options, null, $this->env); $process->setTimeout($this->timeout); $data = ['link' => $url]; diff --git a/lib/Tools/YoutubeHelper.php b/lib/Tools/YoutubeHelper.php index 6f94801..8be0554 100644 --- a/lib/Tools/YoutubeHelper.php +++ b/lib/Tools/YoutubeHelper.php @@ -6,18 +6,8 @@ use OCA\NCDownloader\Tools\Helper; class YoutubeHelper { - public const PROGRESS_PATTERN = '#\[download\]\s+' . - '(?\d+(?:\.\d+)?%)' . //progress - '\s+of\s+[~]?' . - '(?\d+(?:\.\d+)?(?:K|M|G)iB)' . //file size - '(?:\s+at\s+' . - '(?(\d+(?:\.\d+)?(?:K|M|G)iB/s)|Unknown speed))' . //speed - '(?:\s+ETA\s+(?([\d:]{2,8}|Unknown ETA)))?' . //estimated download time - '(\s+in\s+(?[\d:]{2,8}))?#i'; public $file = null; - public $filesize = null; protected $pid = 0; - private static $instance = null; public function __construct() { $this->dbconn = new DbHelper(); @@ -28,20 +18,52 @@ class YoutubeHelper public static function create() { - if (!self::$instance) { - self::$instance = new static(); - } - - return self::$instance; + return new static(); } - public function getFilePath($output) + public function getDownloadInfo(string $output): ?array { - $rules = '#\[(download|ExtractAudio|VideoConvertor|Merger)\]((\s+|\s+Converting.*;\s+)Destination:\s+|\s+Merging formats into\s+\")' . - '(?.*\.(?(mp4|mp3|aac|webm|m4a|ogg|3gp|mkv|wav|flv)))#i'; + $rules = '#\[(?(download|ExtractAudio|VideoConvertor|Merger|ffmpeg))\]((\s+|\s+Converting.*;\s+)Destination:\s+|\s+Merging formats into\s+\")' . + '(?.*\.(?(mp4|mp3|aac|webm|m4a|ogg|3gp|mkv|wav|flv)))#i'; - preg_match($rules, $output, $matches); + if (preg_match($rules, $output, $matches)) { + return $matches; + } + return null; + } - return $matches['filename'] ?? null; + public function getSiteInfo(string $buffer): ?array + { + $regex = '/\[(?.+)]\s(?.+):\sDownloading\s.*/i'; + if (preg_match($regex, $buffer, $matches)) { + return ["id" => $matches["id"], "site" => $matches["site"]]; + } + return null; + } + + public function getProgress(string $buffer): ?array + { + $progressRegex = '#\[download\]\s+' . + '(?\d+(?:\.\d+)?%)' . //progress + '\s+of\s+[~]?' . + '(?\d+(?:\.\d+)?(?:K|M|G)iB)' . //file size + '(?:\s+at\s+' . + '(?(\d+(?:\.\d+)?(?:K|M|G)iB/s)|Unknown speed))' . //speed + '(?:\s+ETA\s+(?([\d:]{2,8}|Unknown ETA)))?' . //estimated download time + '(\s+in\s+(?[\d:]{2,8}))?#i'; + + if (preg_match_all($progressRegex, $buffer, $matches, PREG_SET_ORDER) !== false) { + if (count($matches) > 0) { + return reset($matches); + } + } + return null; + } + + protected function updateProgress(array $data) + { + extract($data); + $sql = sprintf("UPDATE %s set filesize = ?,speed = ?,progress = ? WHERE gid = ?", $this->tablename); + $this->dbconn->executeUpdate($sql, [$filesize, $speed, $percentage, $this->gid]); } public function log($message) { @@ -52,64 +74,59 @@ class YoutubeHelper if (isset($status)) { $this->status = trim($status); } - //$sql = sprintf("UPDATE %s set status = ? WHERE gid = ?", $this->tablename); $this->dbconn->updateStatus($this->gid, $this->status); } public function setPid($pid) { $this->pid = $pid; } - public function run($buffer, $extra) + public function run(string $buffer, array $extra) { - $this->gid = Helper::generateGID($extra['link']); - $file = $this->getFilePath($buffer); - if ($file) { - $extra = serialize($extra); - if($this->dbconn->getDBType() == "pgsql"){ - if (function_exists("pg_escape_bytea")) { - $extra = pg_escape_bytea($extra); - } - } - $data = [ - 'uid' => $this->user, - 'gid' => $this->gid, - 'type' => Helper::DOWNLOADTYPE['YOUTUBE-DL'], - 'filename' => basename($file), - 'status' => Helper::STATUS['ACTIVE'], - 'timestamp' => time(), - 'data' => $extra, - ]; - if (isset($this->file)) { - $sql = sprintf("UPDATE %s set filename = ? WHERE gid = ?", $this->tablename); - $this->dbconn->executeUpdate($sql, [basename($file), $this->gid]); - } else { - $this->dbconn->insert($data); - } - //save the filename as this runs only once - $this->file = basename($file); - //$this->dbconn->save($data,[],['gid' => $this->gid]); + $info = $this->getSiteInfo($buffer); + if (isset($info["id"])) { + $this->gid = Helper::generateGID($info["id"]); } - if (preg_match_all(self::PROGRESS_PATTERN, $buffer, $matches, PREG_SET_ORDER) !== false) { - if (count($matches) > 0) { - $match = reset($matches); - - //save the filesize - if (!isset($this->filesize) && isset($match['size'])) { - $this->filesize = $match['size']; - } - $size = $match['size']; - $percentage = $match['percentage']; - $speed = $match['speed'] . "|" . $match['eta']; - $sql = sprintf("UPDATE %s set filesize = ?,speed = ?,progress = ? WHERE gid = ?", $this->tablename); - $this->dbconn->executeUpdate($sql, [$this->filesize, $speed, $percentage, $this->gid]); - /* $data = [ - 'filesize' => $size, - 'speed' => $speed, - 'progress' => $percentage, - 'gid' => $this->gid, - ]; - $this->dbconn->save([], $data, ['gid' => $this->gid]);*/ + if (!$this->gid) { + $this->gid = Helper::generateGID($extra["link"]); + } + $downloadInfo = $this->getDownloadInfo($buffer); + if ($downloadInfo) { + $file = $downloadInfo["filename"]; + $module = $downloadInfo["module"]; + $this->file = basename($file); + if (strtolower($module) == "download") { + $this->save($file, $extra); + } else { + $this->updateFilename($file); } } + if ($progress = $this->getProgress($buffer)) { + $this->updateProgress($progress); + } + } + protected function save(string $file, array $extra) + { + $data = []; + $extra = serialize($extra); + if ($this->dbconn->getDBType() == "pgsql") { + if (function_exists("pg_escape_bytea")) { + $extra = pg_escape_bytea($extra); + } + } + $data = [ + 'uid' => $this->user, + 'gid' => $this->gid, + 'type' => Helper::DOWNLOADTYPE['YOUTUBE-DL'], + 'filename' => basename($file), + 'status' => Helper::STATUS['ACTIVE'], + 'timestamp' => time(), + 'data' => $extra, + ]; + $this->dbconn->insert($data); + } + private function updateFilename(string $file) + { + $sql = sprintf("UPDATE %s set filename = ? WHERE gid = ?", $this->tablename); + $this->dbconn->executeUpdate($sql, [basename($file), $this->gid]); } }