2015-10-31 14:42:25 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2015-10-31 14:42:25 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Config class.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-12-05 12:12:27 +00:00
|
|
|
|
2015-10-31 14:42:25 +00:00
|
|
|
namespace Alltube;
|
2016-03-29 23:49:08 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Exception\ConfigException;
|
|
|
|
use Alltube\Library\Downloader;
|
2020-05-13 19:18:32 +00:00
|
|
|
use Jawira\CaseConverter\CaseConverterException;
|
2020-06-21 13:30:39 +00:00
|
|
|
use Jean85\PrettyVersions;
|
|
|
|
use PackageVersions\Versions;
|
2020-06-20 12:14:38 +00:00
|
|
|
use Symfony\Component\ErrorHandler\Debug;
|
2015-10-31 14:42:25 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2019-11-10 17:05:53 +00:00
|
|
|
use Jawira\CaseConverter\Convert;
|
2016-03-29 23:49:08 +00:00
|
|
|
|
2015-10-31 14:42:25 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Manage config parameters.
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-03-29 23:49:08 +00:00
|
|
|
class Config
|
2015-10-31 14:42:25 +00:00
|
|
|
{
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Singleton instance.
|
|
|
|
*
|
2019-03-30 17:21:45 +00:00
|
|
|
* @var Config|null
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2016-03-29 23:49:08 +00:00
|
|
|
private static $instance;
|
2015-10-31 14:42:25 +00:00
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* youtube-dl binary path.
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2020-02-27 13:07:30 +00:00
|
|
|
public $youtubedl = 'vendor/ytdl-org/youtube-dl/youtube_dl/__main__.py';
|
2016-08-01 11:29:13 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* python binary path.
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-10-31 14:42:25 +00:00
|
|
|
public $python = '/usr/bin/python';
|
2016-08-01 11:29:13 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* youtube-dl parameters.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @var string[]
|
2016-08-01 11:29:13 +00:00
|
|
|
*/
|
2018-05-23 19:43:34 +00:00
|
|
|
public $params = ['--no-warnings', '--ignore-errors', '--flat-playlist', '--restrict-filenames', '--no-playlist'];
|
2016-08-01 11:29:13 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Enable audio conversion.
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @var bool
|
|
|
|
*/
|
2015-10-31 14:42:25 +00:00
|
|
|
public $convert = false;
|
2016-08-01 11:29:13 +00:00
|
|
|
|
2018-01-24 22:30:24 +00:00
|
|
|
/**
|
|
|
|
* Enable advanced conversion mode.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $convertAdvanced = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of formats available in advanced conversion mode.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @var string[]
|
2018-01-24 22:30:24 +00:00
|
|
|
*/
|
|
|
|
public $convertAdvancedFormats = ['mp3', 'avi', 'flv', 'wav'];
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2020-06-22 21:26:47 +00:00
|
|
|
* ffmpeg binary path.
|
2016-09-07 22:28:28 +00:00
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-22 21:26:47 +00:00
|
|
|
public $ffmpeg = '/usr/bin/ffmpeg';
|
2016-08-01 11:29:13 +00:00
|
|
|
|
2018-01-25 14:10:11 +00:00
|
|
|
/**
|
|
|
|
* Path to the directory that contains the phantomjs binary.
|
2018-01-25 14:13:13 +00:00
|
|
|
*
|
2018-01-25 14:10:11 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-22 21:16:42 +00:00
|
|
|
public $phantomjsDir = '/usr/bin/';
|
2018-01-25 14:10:11 +00:00
|
|
|
|
2017-01-10 22:37:29 +00:00
|
|
|
/**
|
|
|
|
* Disable URL rewriting.
|
2017-01-10 22:39:58 +00:00
|
|
|
*
|
|
|
|
* @var bool
|
2017-01-10 22:37:29 +00:00
|
|
|
*/
|
|
|
|
public $uglyUrls = false;
|
|
|
|
|
2017-01-16 10:29:56 +00:00
|
|
|
/**
|
|
|
|
* Stream downloaded files trough server?
|
2017-01-16 11:11:37 +00:00
|
|
|
*
|
|
|
|
* @var bool
|
2017-01-16 10:29:56 +00:00
|
|
|
*/
|
|
|
|
public $stream = false;
|
|
|
|
|
2017-04-24 22:40:24 +00:00
|
|
|
/**
|
|
|
|
* Allow to remux video + audio?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $remux = false;
|
|
|
|
|
2017-11-10 22:50:17 +00:00
|
|
|
/**
|
2017-11-10 22:52:04 +00:00
|
|
|
* MP3 bitrate when converting (in kbit/s).
|
2017-11-10 22:50:17 +00:00
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $audioBitrate = 128;
|
|
|
|
|
2017-12-09 22:16:48 +00:00
|
|
|
/**
|
2020-06-22 21:26:47 +00:00
|
|
|
* ffmpeg logging level.
|
2017-12-09 22:57:21 +00:00
|
|
|
* Must be one of these: quiet, panic, fatal, error, warning, info, verbose, debug.
|
2017-12-09 22:16:48 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-22 21:26:47 +00:00
|
|
|
public $ffmpegVerbosity = 'error';
|
2017-12-09 22:16:48 +00:00
|
|
|
|
2019-01-06 15:59:16 +00:00
|
|
|
/**
|
2019-01-06 16:00:12 +00:00
|
|
|
* App name.
|
2019-01-06 15:59:16 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $appName = 'AllTube Download';
|
|
|
|
|
2019-05-08 17:46:37 +00:00
|
|
|
/**
|
|
|
|
* Generic formats supported by youtube-dl.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @var string[]
|
2019-05-08 17:46:37 +00:00
|
|
|
*/
|
|
|
|
public $genericFormats = [];
|
|
|
|
|
2019-11-27 22:15:49 +00:00
|
|
|
/**
|
|
|
|
* Enable debug mode.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2019-11-27 20:22:23 +00:00
|
|
|
public $debug = false;
|
|
|
|
|
2015-10-31 14:50:32 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Config constructor.
|
2016-09-05 22:36:47 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param mixed[] $options Options
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2015-10-31 14:50:32 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private function __construct(array $options = [])
|
|
|
|
{
|
|
|
|
$this->applyOptions($options);
|
|
|
|
$this->getEnv();
|
2019-11-29 21:56:41 +00:00
|
|
|
$localeManager = LocaleManager::getInstance();
|
2019-05-08 17:46:37 +00:00
|
|
|
|
|
|
|
if (empty($this->genericFormats)) {
|
|
|
|
// We don't put this in the class definition so it can be detected by xgettext.
|
|
|
|
$this->genericFormats = [
|
2020-06-20 11:33:16 +00:00
|
|
|
'best/bestvideo' => $localeManager->t('Best'),
|
2019-11-29 21:56:41 +00:00
|
|
|
'bestvideo+bestaudio' => $localeManager->t('Remux best video with best audio'),
|
2020-06-20 11:33:16 +00:00
|
|
|
'worst/worstvideo' => $localeManager->t('Worst'),
|
2019-05-08 17:46:37 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->genericFormats as $format => $name) {
|
|
|
|
if (strpos($format, '+') !== false) {
|
|
|
|
if (!$this->remux) {
|
|
|
|
// Disable combined formats if remux mode is not enabled.
|
|
|
|
unset($this->genericFormats[$format]);
|
|
|
|
}
|
|
|
|
} elseif (!$this->stream) {
|
|
|
|
// Force HTTP if stream is not enabled.
|
2020-06-20 11:33:16 +00:00
|
|
|
$keys = array_keys($this->genericFormats);
|
|
|
|
$keys[array_search($format, $keys)] = $this->addHttpToFormat($format);
|
|
|
|
if ($genericFormats = array_combine($keys, $this->genericFormats)) {
|
|
|
|
$this->genericFormats = $genericFormats;
|
|
|
|
}
|
2019-05-08 17:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-20 11:33:16 +00:00
|
|
|
* Add HTTP condition to a format.
|
2019-05-08 17:46:37 +00:00
|
|
|
*
|
2020-06-20 11:33:16 +00:00
|
|
|
* @param string $format Format
|
2019-05-08 17:46:37 +00:00
|
|
|
*
|
2020-06-20 11:33:16 +00:00
|
|
|
* @return string
|
2019-05-08 17:46:37 +00:00
|
|
|
*/
|
2020-09-27 13:53:53 +00:00
|
|
|
public static function addHttpToFormat(string $format)
|
2019-05-08 17:46:37 +00:00
|
|
|
{
|
2020-06-20 11:33:16 +00:00
|
|
|
$newFormat = [];
|
|
|
|
foreach (explode('/', $format) as $subformat) {
|
|
|
|
$newFormat[] = $subformat . '[protocol=https]';
|
|
|
|
$newFormat[] = $subformat . '[protocol=http]';
|
2019-06-17 21:19:18 +00:00
|
|
|
}
|
2020-06-20 11:33:16 +00:00
|
|
|
|
|
|
|
return implode('/', $newFormat);
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an exception if some of the options are invalid.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @return void
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException If Python is missing
|
|
|
|
* @throws ConfigException If youtube-dl is missing
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
|
|
|
private function validateOptions()
|
|
|
|
{
|
|
|
|
if (!is_file($this->youtubedl)) {
|
2020-06-20 23:44:20 +00:00
|
|
|
throw new ConfigException("Can't find youtube-dl at " . $this->youtubedl);
|
|
|
|
} elseif (!Downloader::checkCommand([$this->python, '--version'])) {
|
|
|
|
throw new ConfigException("Can't find Python at " . $this->python);
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
2020-06-20 12:14:38 +00:00
|
|
|
|
|
|
|
if (!class_exists(Debug::class)) {
|
|
|
|
// Dev dependencies are probably not installed.
|
|
|
|
$this->debug = false;
|
|
|
|
}
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the provided options.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param mixed[] $options Options
|
2019-04-21 16:30:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function applyOptions(array $options)
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2019-03-30 17:21:45 +00:00
|
|
|
foreach ($options as $option => $value) {
|
|
|
|
if (isset($this->$option) && isset($value)) {
|
|
|
|
$this->$option = $value;
|
2015-10-31 14:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 17:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-15 19:18:01 +00:00
|
|
|
* Override options from environment variables.
|
2019-11-10 17:05:53 +00:00
|
|
|
* Environment variables should use screaming snake case: CONVERT, PYTHON, AUDIO_BITRATE, etc.
|
|
|
|
* If the value is an array, you should use the YAML format: "CONVERT_ADVANCED_FORMATS='[foo, bar]'"
|
2018-01-06 17:03:06 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2018-01-06 17:03:06 +00:00
|
|
|
*/
|
|
|
|
private function getEnv()
|
|
|
|
{
|
2019-11-10 17:05:53 +00:00
|
|
|
foreach (get_object_vars($this) as $prop => $value) {
|
2020-06-20 23:44:20 +00:00
|
|
|
try {
|
|
|
|
$convert = new Convert($prop);
|
|
|
|
$env = getenv($convert->toMacro());
|
|
|
|
} catch (CaseConverterException $e) {
|
|
|
|
// This should not happen.
|
|
|
|
throw new ConfigException('Could not parse option name: ' . $prop, $e->getCode(), $e);
|
|
|
|
}
|
2018-01-06 17:03:06 +00:00
|
|
|
if ($env) {
|
2019-11-10 17:05:53 +00:00
|
|
|
$this->$prop = Yaml::parse($env);
|
2018-01-06 17:03:06 +00:00
|
|
|
}
|
2016-12-22 12:46:31 +00:00
|
|
|
}
|
2015-10-31 14:42:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 14:50:32 +00:00
|
|
|
/**
|
2019-04-21 16:30:02 +00:00
|
|
|
* Get Config singleton instance.
|
2016-09-05 22:36:47 +00:00
|
|
|
*
|
2015-10-31 14:50:32 +00:00
|
|
|
* @return Config
|
2020-07-15 21:05:41 +00:00
|
|
|
* @todo Stop using a singleton.
|
2015-10-31 14:50:32 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public static function getInstance()
|
2015-10-31 14:50:32 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
if (!isset(self::$instance)) {
|
|
|
|
self::$instance = new self();
|
2015-10-31 14:42:25 +00:00
|
|
|
}
|
2016-09-07 22:28:28 +00:00
|
|
|
|
2016-03-29 23:49:08 +00:00
|
|
|
return self::$instance;
|
2015-10-31 14:42:25 +00:00
|
|
|
}
|
2016-07-30 10:40:49 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
/**
|
|
|
|
* Set options from a YAML file.
|
|
|
|
*
|
|
|
|
* @param string $file Path to the YAML file
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return void
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
2020-09-27 13:53:53 +00:00
|
|
|
public static function setFile(string $file)
|
2019-04-21 16:30:02 +00:00
|
|
|
{
|
|
|
|
if (is_file($file)) {
|
2020-05-13 20:28:05 +00:00
|
|
|
$options = Yaml::parse(strval(file_get_contents($file)));
|
2019-04-21 16:30:02 +00:00
|
|
|
self::$instance = new self($options);
|
2019-04-28 13:52:01 +00:00
|
|
|
self::$instance->validateOptions();
|
2019-04-21 16:30:02 +00:00
|
|
|
} else {
|
2020-06-20 23:44:20 +00:00
|
|
|
throw new ConfigException("Can't find config file at " . $file);
|
2019-04-21 16:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manually set some options.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @param mixed[] $options Options (see `config/config.example.yml` for available options)
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param bool $update True to update an existing instance
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return void
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
|
|
|
public static function setOptions(array $options, $update = true)
|
|
|
|
{
|
|
|
|
if ($update) {
|
|
|
|
$config = self::getInstance();
|
|
|
|
$config->applyOptions($options);
|
|
|
|
$config->validateOptions();
|
|
|
|
} else {
|
|
|
|
self::$instance = new self($options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 11:29:13 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Destroy singleton instance.
|
|
|
|
*
|
2016-08-01 11:29:13 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-30 10:40:49 +00:00
|
|
|
public static function destroyInstance()
|
|
|
|
{
|
|
|
|
self::$instance = null;
|
|
|
|
}
|
2020-06-20 23:44:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a downloader object with the current config.
|
|
|
|
*
|
|
|
|
* @return Downloader
|
|
|
|
*/
|
|
|
|
public function getDownloader()
|
|
|
|
{
|
|
|
|
return new Downloader(
|
|
|
|
$this->youtubedl,
|
|
|
|
$this->params,
|
|
|
|
$this->python,
|
2020-06-22 21:26:47 +00:00
|
|
|
$this->ffmpeg,
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->phantomjsDir,
|
2020-06-22 21:26:47 +00:00
|
|
|
$this->ffmpegVerbosity
|
2020-06-20 23:44:20 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-21 13:30:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAppVersion()
|
|
|
|
{
|
|
|
|
$version = PrettyVersions::getVersion(Versions::ROOT_PACKAGE_NAME);
|
|
|
|
|
|
|
|
return $version->getPrettyVersion();
|
|
|
|
}
|
2015-10-31 14:42:25 +00:00
|
|
|
}
|