fixed youtube-dl issues

This commit is contained in:
huangjx
2021-09-11 15:07:01 +08:00
parent bf539e90b7
commit cba9d22cc8
3 changed files with 53 additions and 38 deletions

View File

@@ -2,33 +2,40 @@
namespace OCA\NCDownloader\AppInfo; namespace OCA\NCDownloader\AppInfo;
use OCA\NCDownloader\Controller\MainController;
use OCA\NCDownloader\Controller\Aria2Controller; use OCA\NCDownloader\Controller\Aria2Controller;
use OCA\NCDownloader\Controller\MainController;
use OCA\NCDownloader\Tools\Aria2; use OCA\NCDownloader\Tools\Aria2;
use OCA\NCDownloader\Tools\Helper;
use OCA\NCDownloader\Tools\Settings; use OCA\NCDownloader\Tools\Settings;
use OCA\NCDownloader\Tools\Youtube;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\IContainer; use OCP\IContainer;
use \OC\Files\Filesystem;
class Application extends App class Application extends App
{ {
public function __construct(array $urlParams = array()) public function __construct(array $urlParams = array())
{ {
parent::__construct('ncdownloader', $urlParams); parent::__construct('ncdownloader', $urlParams);
$user = \OC::$server->getUserSession()->getUser();
$this->uid = ($user) ? $user->getUID() : '';
$this->settings = new Settings($this->uid);
$this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory');
$this->userFolder = Helper::getUserFolder($this->uid);
$container = $this->getContainer(); $container = $this->getContainer();
$container->registerService('UserId', function (IContainer $container) { $container->registerService('UserId', function (IContainer $container) {
$user = \OC::$server->getUserSession()->getUser(); return $this->uid;
return ($user) ? $user->getUID() : '';
}); });
$container->registerService('Aria2', function (IContainer $container) { $container->registerService('Aria2', function (IContainer $container) {
$uid = $container->query('UserId'); return new Aria2($this->getConfig());
return new Aria2($this->getConfig($uid)); });
$container->registerService('Youtube', function (IContainer $container) {
return new Youtube(['downloadDir' => $this->getRealDownloadDir()]);
}); });
$container->registerService('Settings', function (IContainer $container) { $container->registerService('Settings', function (IContainer $container) {
$uid = $container->query('UserId'); return new Settings($this->uid);
return new Settings($uid);
}); });
$container->registerService('MainController', function (IContainer $container) { $container->registerService('MainController', function (IContainer $container) {
@@ -37,7 +44,8 @@ class Application extends App
$container->query('Request'), $container->query('Request'),
$container->query('UserId'), $container->query('UserId'),
\OC::$server->getL10N('ncdownloader'), \OC::$server->getL10N('ncdownloader'),
\OC::$server->getRootFolder(), //\OC::$server->getRootFolder(),
$container->query("Youtube"),
$container->query('Aria2') $container->query('Aria2')
); );
}); });
@@ -54,26 +62,28 @@ class Application extends App
}); });
} }
private function getConfig($uid) private function getRealDownloadDir()
{
//relative nextcloud user path
$dir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads";
return $this->dataDir . $this->userFolder . $dir;
}
private function getConfig()
{ {
//$this->config = \OC::$server->getAppConfig(); //$this->config = \OC::$server->getAppConfig();
$this->settings = new Settings($uid); $realDownloadDir = $this->getRealDownloadDir();
$this->userFolder = Filesystem::getRoot();
$this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory');
//relative nextcloud user path
$this->downloadDir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads";
$this->torrentsDir = $this->settings->get('torrents_dir'); $this->torrentsDir = $this->settings->get('torrents_dir');
//get the absolute path
$this->realDownloadDir = $this->dataDir . $this->userFolder . $this->downloadDir;
$aria2_dir = $this->dataDir . "/aria2"; $aria2_dir = $this->dataDir . "/aria2";
$this->appPath = \OC::$server->getAppManager()->getAppPath('ncdownloader'); //$this->appPath = \OC::$server->getAppManager()->getAppPath('ncdownloader');
$settings['seed_time'] = $this->settings->get("ncd_seed_time"); $settings['seed_time'] = $this->settings->get("ncd_seed_time");
$settings['seed_ratio'] = $this->settings->get("ncd_seed_ratio"); $settings['seed_ratio'] = $this->settings->get("ncd_seed_ratio");
if (is_array($customSettings = $this->settings->getAria2())) { if (is_array($customSettings = $this->settings->getAria2())) {
$settings = array_merge($customSettings, $settings); $settings = array_merge($customSettings, $settings);
} }
$token = $this->settings->setType(1)->get('ncd_rpctoken'); $token = $this->settings->setType(1)->get('ncd_rpctoken');
$config = ['dir' => $this->realDownloadDir, 'conf_dir' => $aria2_dir, 'token' => $token, 'settings' => $settings]; $config = ['dir' => $realDownloadDir, 'conf_dir' => $aria2_dir, 'token' => $token, 'settings' => $settings];
return $config; return $config;
} }

View File

@@ -5,12 +5,13 @@ namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Search\torrentSearch; use OCA\NCDownloader\Search\torrentSearch;
use OCA\NCDownloader\Tools\Aria2; use OCA\NCDownloader\Tools\Aria2;
use OCA\NCDownloader\Tools\DBConn; use OCA\NCDownloader\Tools\DBConn;
use OCA\NCDownloader\Tools\File;
use OCA\NCDownloader\Tools\Helper; use OCA\NCDownloader\Tools\Helper;
use OCA\NCDownloader\Tools\YouTube; use OCA\NCDownloader\Tools\Youtube;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\Files\IRootFolder; //use OCP\Files\IRootFolder;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OC_Util; use OC_Util;
@@ -25,18 +26,19 @@ class MainController extends Controller
private $aria2Opts; private $aria2Opts;
private $l10n; private $l10n;
public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, IRootFolder $rootFolder, Aria2 $aria2) public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Youtube $youtube, Aria2 $aria2)
{ {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->appName = $appName; $this->appName = $appName;
$this->uid = $UserId; $this->uid = $UserId;
$this->l10n = $IL10N; $this->l10n = $IL10N;
$this->rootFolder = $rootFolder; $this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory');
//$this->rootFolder = $rootFolder;
OC_Util::setupFS(); OC_Util::setupFS();
$this->urlGenerator = \OC::$server->getURLGenerator(); $this->urlGenerator = \OC::$server->getURLGenerator();
$this->aria2 = $aria2; $this->aria2 = $aria2;
$this->aria2->init(); $this->aria2->init();
$this->youtube = new Youtube(); $this->youtube = $youtube;
$this->dbconn = new DBConn(); $this->dbconn = new DBConn();
} }
@@ -86,17 +88,10 @@ class MainController extends Controller
return new JSONResponse(['error' => $this->l10n->t("Youtube-dl NOT installed!")]); return new JSONResponse(['error' => $this->l10n->t("Youtube-dl NOT installed!")]);
} }
if (Helper::isGetUrlSite($inputValue)) { $resp = $yt->forceIPV4()->download($inputValue);
if ($data = $yt->forceIPV4()->getDownloadUrl($inputValue)) { File::syncFolder();
$this->Save($data['url'], $data['filename']); return new JSONResponse(['yt' => $resp]);
return new JSONResponse(['yt' => $data]);
} else {
return new JSONResponse(['error' => $this->l10n->t("failed to get any url!")]);
}
} else {
$yt->setDownloadDir($this->realDownloadDir);
return new JSONResponse(['yt' => $yt->download($inputValue)]);
}
} else if ($type === 'search') { } else if ($type === 'search') {
$data = torrentSearch::go($inputValue); $data = torrentSearch::go($inputValue);
$resp['title'] = ['title', 'seeders', 'info', 'actions']; $resp['title'] = ['title', 'seeders', 'info', 'actions'];

View File

@@ -5,15 +5,18 @@ use OCA\NCDownloader\Tools\Helper;
use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
class YouTube class Youtube
{ {
private $ipv4Only; private $ipv4Only;
private $audioOnly = 0; private $audioOnly = 0;
private $audioFormat, $videoFormat = 'mp4'; private $audioFormat, $videoFormat = 'mp4';
private $options = []; private $options = [];
public function __construct() private $downloadDir;
public function __construct($config)
{ {
$config += ['downloadDir' => '/tmp/downloads'];
$this->bin = Helper::findBinaryPath('youtube-dl'); $this->bin = Helper::findBinaryPath('youtube-dl');
$this->setDownloadDir($config['downloadDir']);
} }
public function GetUrlOnly() public function GetUrlOnly()
@@ -33,6 +36,11 @@ class YouTube
$this->downloadDir = rtrim($dir, '/'); $this->downloadDir = rtrim($dir, '/');
} }
public function getDownloadDir()
{
return $this->getDownloadDir;
}
public function prependOption($option) public function prependOption($option)
{ {
array_unshift($this->options, $option); array_unshift($this->options, $option);
@@ -47,6 +55,8 @@ class YouTube
$this->prependOption($this->bin); $this->prependOption($this->bin);
// $this->buildCMD(); // $this->buildCMD();
$process = new Process($this->options); $process = new Process($this->options);
//the maximum time required to download the file
$process->setTimeout(60*60*15);
try { try {
$process->mustRun(); $process->mustRun();
$output = $process->getOutput(); $output = $process->getOutput();
@@ -101,7 +111,7 @@ class YouTube
private function buildCMD() private function buildCMD()
{ {
$this->cmd = $this->bin;//. " 2>&1"; $this->cmd = $this->bin; //. " 2>&1";
foreach ($this->options as $option) { foreach ($this->options as $option) {
$this->cmd .= " " . $option; $this->cmd .= " " . $option;