mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-17 22:45:06 +00:00
fix(dl): apply per-service dl config overrides for all options
services.<TAG>.dl values only applied when the key was also set in the global dl: section (equality check against config.dl missed Click's declared defaults). Gate on Click's ParameterSource instead: CLI/env > service dl > global dl > defaults, converted via each option's own type. - record parameter sources on serve's hand-built context so client values are never clobbered - accept range/list as natural keys for range_/list_ - harden QualityList (YAML int) and SlowDelayRange (YAML bool) converts
This commit is contained in:
+52
-31
@@ -25,6 +25,7 @@ from uuid import UUID
|
||||
|
||||
import click
|
||||
import yaml
|
||||
from click.core import ParameterSource
|
||||
from langcodes import Language
|
||||
from pymediainfo import MediaInfo
|
||||
from rich.console import Group
|
||||
@@ -80,6 +81,42 @@ class SkippedSubtitle(TypedDict):
|
||||
title: str
|
||||
|
||||
|
||||
# Config keys accepted as natural names for params whose Python name dodges a builtin.
|
||||
DL_OPTION_ALIASES = {"range": "range_", "list": "list_"}
|
||||
|
||||
|
||||
def normalize_dl_config(dl_config: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Map aliased config keys (e.g. ``range``) to their Click parameter names (``range_``)."""
|
||||
return {DL_OPTION_ALIASES.get(key, key): value for key, value in dl_config.items()}
|
||||
|
||||
|
||||
def apply_service_dl_overrides(ctx: click.Context, service_dl_config: dict[str, Any], log: logging.Logger) -> None:
|
||||
"""Apply ``services.<TAG>.dl`` config onto ``ctx.params``. Explicit CLI/env values win;
|
||||
defaults and global ``dl:`` default_map values are replaced."""
|
||||
params_by_name = {param.name: param for param in ctx.command.params if param.name}
|
||||
for name, value in normalize_dl_config(service_dl_config).items():
|
||||
param = params_by_name.get(name)
|
||||
if param is None:
|
||||
log.warning(f"Ignoring unknown dl option '{name}' in service config")
|
||||
continue
|
||||
if name not in ctx.params:
|
||||
log.debug(f"Skipping service dl override '{name}': not present in this context")
|
||||
continue
|
||||
if value is None:
|
||||
continue
|
||||
source = ctx.get_parameter_source(name)
|
||||
if source not in (ParameterSource.DEFAULT, ParameterSource.DEFAULT_MAP):
|
||||
continue
|
||||
try:
|
||||
ctx.params[name] = param.type_cast_value(ctx, value)
|
||||
log.debug(f"Applied service dl override '{name}': {ctx.params[name]}")
|
||||
except Exception as e:
|
||||
log.warning(
|
||||
f"Failed to apply service dl override '{name}'={value!r}: {e}. "
|
||||
f"Check that the value is valid for this parameter."
|
||||
)
|
||||
|
||||
|
||||
def download_tracks_in_passes(
|
||||
tracks: Iterable[AnyTrack],
|
||||
max_concurrent: int,
|
||||
@@ -326,7 +363,9 @@ class dl:
|
||||
@click.command(
|
||||
short_help="Download, Decrypt, and Mux tracks for titles from a Service.",
|
||||
cls=Services,
|
||||
context_settings=dict(**context_settings, default_map=config.dl, token_normalize_func=Services.get_tag),
|
||||
context_settings=dict(
|
||||
**context_settings, default_map=normalize_dl_config(config.dl), token_normalize_func=Services.get_tag
|
||||
),
|
||||
)
|
||||
@click.option(
|
||||
"-p", "--profile", type=str, default=None, help="Profile to use for Credentials and Cookies (if available)."
|
||||
@@ -687,37 +726,19 @@ class dl:
|
||||
|
||||
self.service = Services.get_tag(ctx.invoked_subcommand)
|
||||
self.vault_service = Services.get_vault_tag(self.service)
|
||||
service_dl_config = config.services.get(self.service, {}).get("dl", {})
|
||||
if service_dl_config:
|
||||
param_types = {param.name: param.type for param in ctx.command.params if param.name}
|
||||
apply_service_dl_overrides(ctx, config.services.get(self.service, {}).get("dl", {}), self.log)
|
||||
|
||||
for param_name, service_value in service_dl_config.items():
|
||||
if param_name not in ctx.params:
|
||||
continue
|
||||
|
||||
current_value = ctx.params[param_name]
|
||||
global_default = config.dl.get(param_name)
|
||||
param_type = param_types.get(param_name)
|
||||
|
||||
try:
|
||||
if param_type and global_default is not None:
|
||||
global_default = param_type.convert(global_default, None, ctx)
|
||||
except Exception as e:
|
||||
self.log.debug(f"Failed to convert global default for '{param_name}': {e}")
|
||||
|
||||
if current_value == global_default or (current_value is None and global_default is None):
|
||||
try:
|
||||
converted_value = service_value
|
||||
if param_type and service_value is not None:
|
||||
converted_value = param_type.convert(service_value, None, ctx)
|
||||
|
||||
ctx.params[param_name] = converted_value
|
||||
self.log.debug(f"Applied service-specific '{param_name}' override: {converted_value}")
|
||||
except Exception as e:
|
||||
self.log.warning(
|
||||
f"Failed to apply service-specific '{param_name}' override: {e}. "
|
||||
f"Check that the value '{service_value}' is valid for this parameter."
|
||||
)
|
||||
# Refresh locals Click bound before the overrides ran.
|
||||
no_proxy = ctx.params.get("no_proxy", no_proxy)
|
||||
profile = ctx.params.get("profile", profile)
|
||||
proxy = ctx.params.get("proxy", proxy)
|
||||
repack = ctx.params.get("repack", repack)
|
||||
tag = ctx.params.get("tag", tag)
|
||||
tmdb_id = ctx.params.get("tmdb_id", tmdb_id)
|
||||
imdb_id = ctx.params.get("imdb_id", imdb_id)
|
||||
animeapi_id = ctx.params.get("animeapi_id", animeapi_id)
|
||||
enrich = ctx.params.get("enrich", enrich)
|
||||
output_dir = ctx.params.get("output_dir", output_dir)
|
||||
|
||||
self.profile = profile
|
||||
self.proxy_requested = bool(proxy)
|
||||
|
||||
@@ -292,6 +292,14 @@ def _perform_download(
|
||||
"no_cache": params.get("no_cache", False),
|
||||
"reset_cache": params.get("reset_cache", False),
|
||||
}
|
||||
# Hand-built context: record parameter sources so service dl overrides
|
||||
# apply to defaults but never clobber client-sent values.
|
||||
from click.core import ParameterSource
|
||||
|
||||
for param_name in ctx.params:
|
||||
ctx.set_parameter_source(
|
||||
param_name, ParameterSource.COMMANDLINE if param_name in params else ParameterSource.DEFAULT
|
||||
)
|
||||
|
||||
dl_instance = dl(
|
||||
ctx=ctx,
|
||||
|
||||
@@ -254,16 +254,19 @@ class QualityList(click.ParamType):
|
||||
name = "quality_list"
|
||||
|
||||
def convert(
|
||||
self, value: Union[str, list[str]], param: Optional[click.Parameter] = None, ctx: Optional[click.Context] = None
|
||||
self,
|
||||
value: Union[str, int, list[Union[str, int]]],
|
||||
param: Optional[click.Parameter] = None,
|
||||
ctx: Optional[click.Context] = None,
|
||||
) -> list[int]:
|
||||
if not value:
|
||||
return []
|
||||
if not isinstance(value, list):
|
||||
value = value.split(",")
|
||||
value = str(value).split(",")
|
||||
resolutions = []
|
||||
for resolution in value:
|
||||
try:
|
||||
resolutions.append(int(resolution.lower().rstrip("p")))
|
||||
resolutions.append(int(str(resolution).lower().rstrip("p")))
|
||||
except TypeError:
|
||||
self.fail(
|
||||
f"Expected string for int() conversion, got {resolution!r} of type {type(resolution).__name__}",
|
||||
@@ -370,9 +373,13 @@ class SlowDelayRange(click.ParamType):
|
||||
|
||||
name = "delay_range"
|
||||
|
||||
def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context]) -> tuple[int, int]:
|
||||
def convert(
|
||||
self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context]
|
||||
) -> Optional[tuple[int, int]]:
|
||||
if isinstance(value, tuple):
|
||||
return value
|
||||
if isinstance(value, bool):
|
||||
return (60, 120) if value else None
|
||||
|
||||
match = re.match(r"^(\d+)-(\d+)$", str(value))
|
||||
if not match:
|
||||
|
||||
@@ -414,6 +414,13 @@ audio:
|
||||
# Atmos still trumps codec priority. Valid names: AAC, AC3, EC3, AC4, OPUS, OGG, DTS, ALAC, FLAC.
|
||||
# codec_priority: [FLAC, ALAC, AC4, EC3, DTS, AC3, OPUS, AAC, OGG]
|
||||
|
||||
# Global defaults for the dl command. Any dl option can be set here.
|
||||
# Use the option name with dashes as underscores (--v-lang -> v_lang).
|
||||
# range and list work as-is (range_/list_ also accepted).
|
||||
#
|
||||
# Any of these can also be overridden per service under services.<TAG>.dl
|
||||
# (see the services section below). Precedence, highest first:
|
||||
# CLI arguments > services.<TAG>.dl > this dl section > built-in defaults
|
||||
dl:
|
||||
sub_format: srt
|
||||
downloads: 4
|
||||
@@ -421,6 +428,14 @@ dl:
|
||||
lang:
|
||||
- en
|
||||
- fr
|
||||
v_lang: [en] # --v-lang: video language(s), if different from audio
|
||||
s_lang: [en, de] # --s-lang: subtitle language(s)
|
||||
range: SDR # --range: SDR, HDR10, HDR10+, DV
|
||||
quality: [1080] # --quality: resolution(s), int or list
|
||||
slow: false # --slow: true = random 60-120s delay between titles, or "MIN-MAX" string
|
||||
# Nesting a SERVICE TAG here provides defaults for that service's OWN command
|
||||
# options (e.g. a service-defined --bitrate flag) -- NOT for the dl options
|
||||
# above. To override dl options per service, use services.<TAG>.dl instead.
|
||||
EXAMPLE:
|
||||
bitrate: CBR
|
||||
|
||||
@@ -624,12 +639,20 @@ services:
|
||||
# Other services or no service specified will still use random selection
|
||||
|
||||
# NEW: Configuration overrides (can be combined with profiles and certificates)
|
||||
# Override dl command defaults for this service
|
||||
# Override dl command defaults for this service. ANY dl option works here,
|
||||
# same key naming as the global dl section (v_lang, range_, list_, ...).
|
||||
# These beat the global dl section; explicit CLI arguments beat both.
|
||||
dl:
|
||||
downloads: 4 # Limit concurrent track downloads (global default: 6)
|
||||
workers: 8 # Reduce workers per track (global default: 16)
|
||||
lang: ["en", "es-419"] # Different language priority for this service
|
||||
v_lang: [en] # Video language differs from audio on this service
|
||||
s_lang: [all] # Grab every subtitle language here
|
||||
range: HDR10 # Prefer HDR10 on this service (CLI -r still wins)
|
||||
quality: 720 # This service rate-limits 1080p; int or list both work
|
||||
sub_format: srt # Force SRT subtitle format
|
||||
slow: true # Pace title downloads (60-120s); or "20-40" for a custom range
|
||||
repack: false # Even if the global dl section sets repack: true
|
||||
|
||||
# Override subtitle processing for this service
|
||||
subtitle:
|
||||
@@ -671,7 +694,8 @@ services:
|
||||
# - Only specified keys are overridden, others use global defaults
|
||||
# - Reserved keys (profiles, api_key, certificate, etc.) are NOT treated as overrides
|
||||
# - Any dict-type config option can be overridden (dl, subtitle, muxing, headers, etc.)
|
||||
# - CLI arguments always take priority over service-specific config
|
||||
# - Any dl option can be overridden; dashes become underscores (--v-lang -> v_lang)
|
||||
# - Precedence: CLI arguments > service dl override > global dl section > built-in defaults
|
||||
|
||||
# External proxy provider services
|
||||
proxy_providers:
|
||||
|
||||
Reference in New Issue
Block a user