make URL options have higher proirity than preset and default options
This commit is contained in:
parent
414b52de9b
commit
981405a44d
2 changed files with 63 additions and 22 deletions
|
|
@ -121,8 +121,14 @@ class Download:
|
||||||
.add(
|
.add(
|
||||||
{
|
{
|
||||||
"color": "no_color",
|
"color": "no_color",
|
||||||
"paths": {"home": self.download_dir, "temp": self.temp_path},
|
"paths": {
|
||||||
"outtmpl": {"default": self.template, "chapter": self.template_chapter},
|
"home": self.download_dir,
|
||||||
|
"temp": self.temp_path,
|
||||||
|
},
|
||||||
|
"outtmpl": {
|
||||||
|
"default": self.template,
|
||||||
|
"chapter": self.template_chapter,
|
||||||
|
},
|
||||||
"noprogress": True,
|
"noprogress": True,
|
||||||
"break_on_existing": True,
|
"break_on_existing": True,
|
||||||
"ignoreerrors": False,
|
"ignoreerrors": False,
|
||||||
|
|
@ -173,6 +179,11 @@ class Download:
|
||||||
|
|
||||||
LOG.debug("Params before passing to yt-dlp.", extra=params)
|
LOG.debug("Params before passing to yt-dlp.", extra=params)
|
||||||
|
|
||||||
|
if "impersonate" in params:
|
||||||
|
from yt_dlp.networking.impersonate import ImpersonateTarget
|
||||||
|
|
||||||
|
params["impersonate"] = ImpersonateTarget.from_str(params["impersonate"])
|
||||||
|
|
||||||
cls = yt_dlp.YoutubeDL(params=params)
|
cls = yt_dlp.YoutubeDL(params=params)
|
||||||
|
|
||||||
if isinstance(self.info_dict, dict) and len(self.info_dict) > 1:
|
if isinstance(self.info_dict, dict) and len(self.info_dict) > 1:
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
|
@ -5,10 +6,15 @@ from .Presets import Presets
|
||||||
from .Singleton import Singleton
|
from .Singleton import Singleton
|
||||||
from .Utils import IGNORED_KEYS, calc_download_path, merge_dict
|
from .Utils import IGNORED_KEYS, calc_download_path, merge_dict
|
||||||
|
|
||||||
|
LOG = logging.getLogger("YTDLPOpts")
|
||||||
|
|
||||||
|
|
||||||
class YTDLPOpts(metaclass=Singleton):
|
class YTDLPOpts(metaclass=Singleton):
|
||||||
item_opts: dict = {}
|
_item_opts: dict = {}
|
||||||
preset_opts: dict = {}
|
"""The item options."""
|
||||||
|
|
||||||
|
_preset_opts: dict = {}
|
||||||
|
"""The preset options."""
|
||||||
|
|
||||||
_instance = None
|
_instance = None
|
||||||
"""The instance of the class."""
|
"""The instance of the class."""
|
||||||
|
|
@ -30,15 +36,37 @@ class YTDLPOpts(metaclass=Singleton):
|
||||||
|
|
||||||
return YTDLPOpts._instance
|
return YTDLPOpts._instance
|
||||||
|
|
||||||
def add(self, config: dict, from_user: bool = False):
|
def add(self, config: dict, from_user: bool = False) -> "YTDLPOpts":
|
||||||
|
"""
|
||||||
|
Add the options to the item options.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config (dict): The options to add
|
||||||
|
from_user (bool): If the options are from the user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
YTDLPOpts: The instance of the class
|
||||||
|
|
||||||
|
"""
|
||||||
for key, value in config.items():
|
for key, value in config.items():
|
||||||
if key in IGNORED_KEYS and from_user:
|
if key in IGNORED_KEYS and from_user:
|
||||||
continue
|
continue
|
||||||
self.item_opts[key] = value
|
self._item_opts[key] = value
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def preset(self, name: str, with_cookies: bool = False) -> "YTDLPOpts":
|
def preset(self, name: str, with_cookies: bool = False) -> "YTDLPOpts":
|
||||||
|
"""
|
||||||
|
Add the preset options to the item options.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): The name of the preset
|
||||||
|
with_cookies (bool): If the cookies should be added
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
YTDLPOpts: The instance of the class
|
||||||
|
|
||||||
|
"""
|
||||||
preset = Presets.get_instance().get(name=name)
|
preset = Presets.get_instance().get(name=name)
|
||||||
if not preset or "default" == name:
|
if not preset or "default" == name:
|
||||||
return self
|
return self
|
||||||
|
|
@ -52,32 +80,42 @@ class YTDLPOpts(metaclass=Singleton):
|
||||||
with open(file, "w") as f:
|
with open(file, "w") as f:
|
||||||
f.write(preset.cookies)
|
f.write(preset.cookies)
|
||||||
|
|
||||||
self.preset_opts["cookiefile"] = str(file)
|
self._preset_opts["cookiefile"] = str(file)
|
||||||
|
|
||||||
if preset.format:
|
if preset.format:
|
||||||
self.preset_opts["format"] = preset.format
|
self._preset_opts["format"] = preset.format
|
||||||
|
|
||||||
if preset.template:
|
if preset.template:
|
||||||
self.preset_opts["outtmpl"] = {"default": preset.template, "chapter": self._config.output_template_chapter}
|
self._preset_opts["outtmpl"] = {"default": preset.template, "chapter": self._config.output_template_chapter}
|
||||||
|
|
||||||
if preset.folder:
|
if preset.folder:
|
||||||
self.preset_opts["paths"] = {
|
self._preset_opts["paths"] = {
|
||||||
"home": calc_download_path(base_path=self._config.download_path, folder=preset.folder),
|
"home": calc_download_path(base_path=self._config.download_path, folder=preset.folder),
|
||||||
"temp": self._config.temp_path,
|
"temp": self._config.temp_path,
|
||||||
}
|
}
|
||||||
|
|
||||||
if preset.postprocessors and isinstance(preset.postprocessors, list) and len(preset.postprocessors) > 0:
|
if preset.postprocessors and isinstance(preset.postprocessors, list) and len(preset.postprocessors) > 0:
|
||||||
self.preset_opts["postprocessors"] = preset.postprocessors
|
self._preset_opts["postprocessors"] = preset.postprocessors
|
||||||
|
|
||||||
if preset.args and isinstance(preset.args, dict) and len(preset.args) > 0:
|
if preset.args and isinstance(preset.args, dict) and len(preset.args) > 0:
|
||||||
for key, value in preset.args.items():
|
for key, value in preset.args.items():
|
||||||
if key in IGNORED_KEYS:
|
if key in IGNORED_KEYS:
|
||||||
continue
|
continue
|
||||||
self.preset_opts[key] = value
|
self._preset_opts[key] = value
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_all(self, keep: bool = False) -> dict:
|
def get_all(self, keep: bool = False) -> dict:
|
||||||
|
"""
|
||||||
|
Get all the options.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
keep (bool): If the options should be kept
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: The options
|
||||||
|
|
||||||
|
"""
|
||||||
default_opts = self._config.ytdl_options
|
default_opts = self._config.ytdl_options
|
||||||
default_opts["paths"] = {"home": self._config.download_path, "temp": self._config.temp_path}
|
default_opts["paths"] = {"home": self._config.download_path, "temp": self._config.temp_path}
|
||||||
default_opts["outtmpl"] = {
|
default_opts["outtmpl"] = {
|
||||||
|
|
@ -85,18 +123,10 @@ class YTDLPOpts(metaclass=Singleton):
|
||||||
"chapter": self._config.output_template_chapter,
|
"chapter": self._config.output_template_chapter,
|
||||||
}
|
}
|
||||||
|
|
||||||
if "format" in default_opts or "format" in self.item_opts:
|
data = merge_dict(self._item_opts, merge_dict(self._preset_opts, default_opts))
|
||||||
return merge_dict(default_opts, self.item_opts)
|
|
||||||
|
|
||||||
data = merge_dict(merge_dict(self.preset_opts, default_opts), self.item_opts)
|
|
||||||
|
|
||||||
if not keep:
|
if not keep:
|
||||||
self.presets_opts = {}
|
self.presets_opts = {}
|
||||||
self.item_opts = {}
|
self._item_opts = {}
|
||||||
|
|
||||||
if "impersonate" in data:
|
|
||||||
from yt_dlp.networking.impersonate import ImpersonateTarget
|
|
||||||
|
|
||||||
data["impersonate"] = ImpersonateTarget.from_str(data["impersonate"])
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue