Files
ncdownloader/lib/Ytdl/Helper.php

140 lines
4.4 KiB
PHP

<?php
namespace OCA\NCDownloader\Ytdl;
use OCA\NCDownloader\Db\Helper as DbHelper;
use OCA\NCDownloader\Tools\Helper as ToolsHelper;
class Helper
{
public $file = null;
protected $pid = 0;
protected $dbconn;
protected $tablename;
protected $user;
protected $tools;
protected $gid;
protected $ytdl;
protected $status;
public function __construct()
{
$this->dbconn = new DbHelper();
$this->tablename = $this->dbconn->queryBuilder->getTableName("ncdownloader_info");
$this->user = \OC::$server->getUserSession()->getUser()->getUID();
}
public static function create()
{
return new static();
}
public function getDownloadInfo(string $output): ?array
{
$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|opus)))#i';
if (preg_match($rules, $output, $matches)) {
return $matches;
}
return 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+[~]?\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)
{
ToolsHelper::debug($message);
}
public function updateStatus($status = null)
{
if (isset($status)) {
$this->status = trim($status);
}
$this->dbconn->updateStatus($this->gid, $this->status);
}
public function setPid($pid)
{
$this->pid = $pid;
}
public function run(string $buffer, array $extra)
{
$info = $this->getSiteInfo($buffer);
if (isset($info["id"])) {
$this->gid = ToolsHelper::generateGID($info["id"]);
}
if (!$this->gid) {
$this->gid = ToolsHelper::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' => ToolsHelper::DOWNLOADTYPE['YOUTUBE-DL'],
'filename' => basename($file),
'status' => ToolsHelper::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]);
}
}