make URL options have higher proirity than preset and default options

This commit is contained in:
ArabCoders 2025-03-07 18:44:44 +03:00
parent 414b52de9b
commit 981405a44d
2 changed files with 63 additions and 22 deletions

View file

@ -121,8 +121,14 @@ class Download:
.add(
{
"color": "no_color",
"paths": {"home": self.download_dir, "temp": self.temp_path},
"outtmpl": {"default": self.template, "chapter": self.template_chapter},
"paths": {
"home": self.download_dir,
"temp": self.temp_path,
},
"outtmpl": {
"default": self.template,
"chapter": self.template_chapter,
},
"noprogress": True,
"break_on_existing": True,
"ignoreerrors": False,
@ -173,6 +179,11 @@ class Download:
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)
if isinstance(self.info_dict, dict) and len(self.info_dict) > 1:

View file

@ -1,3 +1,4 @@
import logging
from pathlib import Path
from .config import Config
@ -5,10 +6,15 @@ from .Presets import Presets
from .Singleton import Singleton
from .Utils import IGNORED_KEYS, calc_download_path, merge_dict
LOG = logging.getLogger("YTDLPOpts")
class YTDLPOpts(metaclass=Singleton):
item_opts: dict = {}
preset_opts: dict = {}
_item_opts: dict = {}
"""The item options."""
_preset_opts: dict = {}
"""The preset options."""
_instance = None
"""The instance of the class."""
@ -30,15 +36,37 @@ class YTDLPOpts(metaclass=Singleton):
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():
if key in IGNORED_KEYS and from_user:
continue
self.item_opts[key] = value
self._item_opts[key] = value
return self
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)
if not preset or "default" == name:
return self
@ -52,32 +80,42 @@ class YTDLPOpts(metaclass=Singleton):
with open(file, "w") as f:
f.write(preset.cookies)
self.preset_opts["cookiefile"] = str(file)
self._preset_opts["cookiefile"] = str(file)
if preset.format:
self.preset_opts["format"] = preset.format
self._preset_opts["format"] = preset.format
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:
self.preset_opts["paths"] = {
self._preset_opts["paths"] = {
"home": calc_download_path(base_path=self._config.download_path, folder=preset.folder),
"temp": self._config.temp_path,
}
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:
for key, value in preset.args.items():
if key in IGNORED_KEYS:
continue
self.preset_opts[key] = value
self._preset_opts[key] = value
return self
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["paths"] = {"home": self._config.download_path, "temp": self._config.temp_path}
default_opts["outtmpl"] = {
@ -85,18 +123,10 @@ class YTDLPOpts(metaclass=Singleton):
"chapter": self._config.output_template_chapter,
}
if "format" in default_opts or "format" in self.item_opts:
return merge_dict(default_opts, self.item_opts)
data = merge_dict(merge_dict(self.preset_opts, default_opts), self.item_opts)
data = merge_dict(self._item_opts, merge_dict(self._preset_opts, default_opts))
if not keep:
self.presets_opts = {}
self.item_opts = {}
if "impersonate" in data:
from yt_dlp.networking.impersonate import ImpersonateTarget
data["impersonate"] = ImpersonateTarget.from_str(data["impersonate"])
self._item_opts = {}
return data