fixed #52;create a new entry for each file when downloading a youtube-dl playlist;
This commit is contained in:
@@ -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];
|
||||
|
||||
@@ -6,18 +6,8 @@ use OCA\NCDownloader\Tools\Helper;
|
||||
|
||||
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 $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 new static();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
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+\")' .
|
||||
$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';
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -52,20 +74,41 @@ 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) {
|
||||
$info = $this->getSiteInfo($buffer);
|
||||
if (isset($info["id"])) {
|
||||
$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);
|
||||
if($this->dbconn->getDBType() == "pgsql"){
|
||||
if ($this->dbconn->getDBType() == "pgsql") {
|
||||
if (function_exists("pg_escape_bytea")) {
|
||||
$extra = pg_escape_bytea($extra);
|
||||
}
|
||||
@@ -79,37 +122,11 @@ class YoutubeHelper
|
||||
'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]);
|
||||
}
|
||||
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]);*/
|
||||
}
|
||||
}
|
||||
private function updateFilename(string $file)
|
||||
{
|
||||
$sql = sprintf("UPDATE %s set filename = ? WHERE gid = ?", $this->tablename);
|
||||
$this->dbconn->executeUpdate($sql, [basename($file), $this->gid]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user