fixed #52;create a new entry for each file when downloading a youtube-dl playlist;

This commit is contained in:
huangjx
2022-04-28 18:21:14 +08:00
parent 32b6484596
commit 015be9e3c9
2 changed files with 86 additions and 70 deletions

View File

@@ -141,7 +141,6 @@ class Youtube
$this->setOption("--output", $this->downloadDir . "/" . $this->outTpl); $this->setOption("--output", $this->downloadDir . "/" . $this->outTpl);
$this->setUrl($url); $this->setUrl($url);
$this->prependOption($this->bin); $this->prependOption($this->bin);
//\OC::$server->getLogger()->error($process->getCommandLine(), ['app' => 'PHP']);
$process = new Process($this->options, null, $this->env); $process = new Process($this->options, null, $this->env);
$process->setTimeout($this->timeout); $process->setTimeout($this->timeout);
$data = ['link' => $url]; $data = ['link' => $url];

View File

@@ -6,18 +6,8 @@ use OCA\NCDownloader\Tools\Helper;
class YoutubeHelper class YoutubeHelper
{ {
public const PROGRESS_PATTERN = '#\[download\]\s+' .
'(?<percentage>\d+(?:\.\d+)?%)' . //progress
'\s+of\s+[~]?' .
'(?<size>\d+(?:\.\d+)?(?:K|M|G)iB)' . //file size
'(?:\s+at\s+' .
'(?<speed>(\d+(?:\.\d+)?(?:K|M|G)iB/s)|Unknown speed))' . //speed
'(?:\s+ETA\s+(?<eta>([\d:]{2,8}|Unknown ETA)))?' . //estimated download time
'(\s+in\s+(?<totalTime>[\d:]{2,8}))?#i';
public $file = null; public $file = null;
public $filesize = null;
protected $pid = 0; protected $pid = 0;
private static $instance = null;
public function __construct() public function __construct()
{ {
$this->dbconn = new DbHelper(); $this->dbconn = new DbHelper();
@@ -28,20 +18,52 @@ class YoutubeHelper
public static function create() public static function create()
{ {
if (!self::$instance) { return new static();
self::$instance = new static();
} }
public function getDownloadInfo(string $output): ?array
return self::$instance;
}
public function getFilePath($output)
{ {
$rules = '#\[(download|ExtractAudio|VideoConvertor|Merger)\]((\s+|\s+Converting.*;\s+)Destination:\s+|\s+Merging formats into\s+\")' . $rules = '#\[(?<module>(download|ExtractAudio|VideoConvertor|Merger|ffmpeg))\]((\s+|\s+Converting.*;\s+)Destination:\s+|\s+Merging formats into\s+\")' .
'(?<filename>.*\.(?<ext>(mp4|mp3|aac|webm|m4a|ogg|3gp|mkv|wav|flv)))#i'; '(?<filename>.*\.(?<ext>(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 = '/\[(?<site>.+)]\s(?<id>.+):\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+' .
'(?<percentage>\d+(?:\.\d+)?%)' . //progress
'\s+of\s+[~]?' .
'(?<filesize>\d+(?:\.\d+)?(?:K|M|G)iB)' . //file size
'(?:\s+at\s+' .
'(?<speed>(\d+(?:\.\d+)?(?:K|M|G)iB/s)|Unknown speed))' . //speed
'(?:\s+ETA\s+(?<eta>([\d:]{2,8}|Unknown ETA)))?' . //estimated download time
'(\s+in\s+(?<totalTime>[\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) public function log($message)
{ {
@@ -52,20 +74,41 @@ class YoutubeHelper
if (isset($status)) { if (isset($status)) {
$this->status = trim($status); $this->status = trim($status);
} }
//$sql = sprintf("UPDATE %s set status = ? WHERE gid = ?", $this->tablename);
$this->dbconn->updateStatus($this->gid, $this->status); $this->dbconn->updateStatus($this->gid, $this->status);
} }
public function setPid($pid) public function setPid($pid)
{ {
$this->pid = $pid; $this->pid = $pid;
} }
public function run($buffer, $extra) public function run(string $buffer, array $extra)
{ {
$this->gid = Helper::generateGID($extra['link']); $info = $this->getSiteInfo($buffer);
$file = $this->getFilePath($buffer); if (isset($info["id"])) {
if ($file) { $this->gid = Helper::generateGID($info["id"]);
}
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); $extra = serialize($extra);
if($this->dbconn->getDBType() == "pgsql"){ if ($this->dbconn->getDBType() == "pgsql") {
if (function_exists("pg_escape_bytea")) { if (function_exists("pg_escape_bytea")) {
$extra = pg_escape_bytea($extra); $extra = pg_escape_bytea($extra);
} }
@@ -79,37 +122,11 @@ class YoutubeHelper
'timestamp' => time(), 'timestamp' => time(),
'data' => $extra, '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); $this->dbconn->insert($data);
} }
//save the filename as this runs only once private function updateFilename(string $file)
$this->file = basename($file); {
//$this->dbconn->save($data,[],['gid' => $this->gid]); $sql = sprintf("UPDATE %s set filename = ? WHERE gid = ?", $this->tablename);
} $this->dbconn->executeUpdate($sql, [basename($file), $this->gid]);
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]);*/
}
}
} }
} }