refactor(remote)!: send service options only in service_params

A service option named like a dl option (e.g. --profile) is no longer dropped or mixed with the dl value; the server casts nested values and keeps cli defaults nested.
This commit is contained in:
imSp4rky
2026-10-30 16:16:56 -06:00
parent 47f88b1b65
commit 5206b3cab6
12 changed files with 139 additions and 71 deletions
+2 -8
View File
@@ -21,6 +21,7 @@ from unshackle.core.api.events import bus, publish_service_event
from unshackle.core.api.sanitize import sanitize_log
from unshackle.core.utils.click_types import AUDIO_CODEC_LIST, QUALITY_LIST, VIDEO_CODEC_LIST
from unshackle.core.utils.redact import REDACTED, URL_USERINFO_RE, redact_all, redact_path, redact_text
from unshackle.core.utils.service_params import service_options
log = logging.getLogger("download_manager")
@@ -574,14 +575,7 @@ def perform_download(
if "title" in service_init_params:
service_kwargs["title"] = title_id
for key, value in params.items():
if key in service_init_params and key not in ["service", "title_id", "profile", "service_params"]:
service_kwargs[key] = value
service_params = params.get("service_params")
if isinstance(service_params, dict):
for key, value in service_params.items():
if key in service_init_params and key != "title":
service_kwargs[key] = value
service_kwargs.update(service_options(params, service_init_params.keys() - {"title"}))
for param_name, param_info in service_init_params.items():
if param_name not in service_kwargs and param_name not in ["self", "ctx"]:
+30 -31
View File
@@ -15,7 +15,7 @@ from datetime import date as date_
from functools import partial
from http.cookiejar import CookieJar
from pathlib import Path
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, cast
from typing import AbstractSet, Any, Callable, Dict, Iterable, List, Optional, Set, cast
import click
from aiohttp import web
@@ -45,6 +45,7 @@ from unshackle.core.utils.click_types import AUDIO_CODEC_LIST, QUALITY_LIST, SUB
from unshackle.core.utils.collections import ci_get
from unshackle.core.utils.hdr10plus import probed_tracks
from unshackle.core.utils.redact import REDACTED, URL_USERINFO_RE, redact_all, redact_secrets, redact_text
from unshackle.core.utils.service_params import service_options
log = logging.getLogger("api")
@@ -246,30 +247,26 @@ def instantiate_service(
service_module: Any,
title: str,
data: Optional[Dict[str, Any]] = None,
transport_keys: Optional[set] = None,
transport_keys: AbstractSet[str] = frozenset(),
) -> Any:
"""Instantiate a service by invoking its click cli through Click.
"""Instantiate a service by invoking its click cli with the ``service_options`` in ``data``.
Click fills option defaults through ``param.get_default()`` and runs type coercion,
so we no longer have to inspect ``__init__`` or stitch defaults by hand. This function
pulls extra kwargs from the ``data`` entries whose names match a cli option name
and are not in the transport-key blocklist.
Click fills the option defaults, but ``ctx.invoke`` passes the given kwargs uncast, so each value
goes through its option's type here: JSON or a remote client's str-typed stub can send "1" for an int.
"""
cli_params = getattr(getattr(service_module, "cli", None), "params", []) or []
cli_param_names = {p.name for p in cli_params if hasattr(p, "name") and p.name}
transport_keys = transport_keys or set()
params_by_name = {p.name: p for p in cli_params if getattr(p, "name", None) and p.name != "title"}
extras: Dict[str, Any] = {}
if data:
for k, v in data.items():
if k in cli_param_names and k not in transport_keys and k != "title":
extras[k] = v
service_params = data.get("service_params")
if isinstance(service_params, dict):
for k, v in service_params.items():
if k in cli_param_names and k != "title":
extras[k] = v
elif service_params is None:
for k in cli_param_names & transport_keys & set(data):
for k, v in service_options(data, params_by_name, transport_keys).items():
try:
extras[k] = params_by_name[k].type_cast_value(parent_ctx, v)
except click.BadParameter as e:
raise APIError(
APIErrorCode.INVALID_PARAMETERS, f"Invalid service option '{sanitize_log(k)}': {e.message}"
) from e
if data.get("service_params") is None:
for k in params_by_name.keys() & transport_keys & data.keys():
if k not in ("service", "title_id", "proxy", "no_proxy") and data.get(k) is not None:
log.warning(
f"Ignoring flat '{sanitize_log(k)}' as a service option, send it under 'service_params'"
@@ -1044,9 +1041,7 @@ def choose_session_cdm(
return False, None
if data.get("server_cdm") is False and data.get("cdm_type"):
server_device_options = getattr(Services.load(service), "SERVER_DEVICE_OPTIONS", ())
service_params = data.get("service_params")
options = {**data, **service_params} if isinstance(service_params, dict) else data
if not any(options.get(option) for option in server_device_options):
if not any(service_options(data, server_device_options).values()):
return False, None
cap = server_cdm_max_height(request, service)
wanted = requested_height(data.get("quality"))
@@ -2456,16 +2451,19 @@ async def download_handler(data: Dict[str, Any], request: Optional[web.Request]
try:
service_module = None if server else Services.load(normalized_service)
service_specific_defaults = {}
service_params: Optional[Dict[str, Any]] = None
# Skip None defaults here: this dict overlays into job params; injecting
# None for keys like `profile` would clobber serve-config overrides.
# Missing required __init__ params are handled in download_manager.perform_download.
# Service defaults and options travel nested: flat, a service default would overwrite the dl value of
# the same name. Missing required __init__ params are handled in download_manager.perform_download.
# A remote service applies its own defaults on the remote server.
if hasattr(service_module, "cli") and hasattr(service_module.cli, "params"):
for param in service_module.cli.params:
if hasattr(param, "name") and param.default is not None and not isinstance(param.default, enum.Enum):
service_specific_defaults[param.name] = param.default
cli_params = getattr(getattr(service_module, "cli", None), "params", None)
if cli_params is not None:
defaults = {
param.name: param.default
for param in cli_params
if param.name and param.default is not None and not isinstance(param.default, enum.Enum)
}
service_params = {**defaults, **service_options(data, {param.name for param in cli_params})}
filtered_params = {k: v for k, v in data.items() if k not in ["service", "title_id"]}
# Overlay any dl-relevant keys from `serve:` config (e.g. downloads, workers) so the API
@@ -2478,10 +2476,11 @@ async def download_handler(data: Dict[str, Any], request: Optional[web.Request]
params_with_defaults = {
**DEFAULT_DOWNLOAD_PARAMS,
**serve_overrides,
**service_specific_defaults,
**service_dl,
**filtered_params,
}
if service_params is not None:
params_with_defaults["service_params"] = service_params
await asyncio.to_thread(enforce_download_gates, {**params_with_defaults, "service": service_tag}, request)
manager = get_download_manager()
+3 -2
View File
@@ -1958,8 +1958,9 @@ async def session_create(request: web.Request) -> web.Response:
description: |
False when the user picked the local CDM. With `cdm_type`, the server then builds
the service on the client's device, also for an API key with the server CDM.
A request that sets one of the service's `SERVER_DEVICE_OPTIONS`, in
`service_params` or at the top level, keeps the server CDM
A request that sets one of the service's `SERVER_DEVICE_OPTIONS` keeps the server
CDM. The server reads it from `service_params`, or from the top level when
the request has no `service_params`
cdm_type:
type: string
enum: [widevine, playready]
+5 -11
View File
@@ -55,6 +55,7 @@ from unshackle.core.tunnel import TunnelClient
from unshackle.core.utils import run_proxy
from unshackle.core.utils.collections import ci_get
from unshackle.core.utils.redact import redact_path, redact_text, safe_display_url
from unshackle.core.utils.service_params import service_options
from unshackle.core.vault import Vault
log = logging.getLogger("remote_service")
@@ -873,15 +874,9 @@ def find_remote_service(services: list[dict], name: str) -> Optional[dict]:
def remote_service_params(info: dict, data: Dict[str, Any]) -> Dict[str, Any]:
"""The service options to send: ``data["service_params"]``, else the ``data`` keys naming a cli option.
Only the remote service's cli option names pass, either way: RemoteService merges them into the
session-create body, where a key such as ``service`` or ``proxy`` would override the checked value.
"""
"""The remote service's cli options in ``data``, read by ``service_options``."""
names = {p.get("name") for p in info.get("cli_params") or [] if p.get("kind") == "option"}
names -= {"title", "service", "title_id", "profile", "proxy", "no_proxy"}
source = data.get("service_params") if isinstance(data.get("service_params"), dict) else data
return {k: v for k, v in source.items() if k in names and v is not None and v is not False}
return {k: v for k, v in service_options(data, names).items() if v is not None and v is not False}
def make_remote_service(
@@ -1385,9 +1380,8 @@ class RemoteService:
if dl_params:
create_data["dl_params"] = dl_params
if self._service_params:
create_data.update(self._service_params)
create_data["service_params"] = self._service_params
# sent even empty: without it the server reads service options from the flat dl keys
create_data["service_params"] = self._service_params
# sent with the server CDM too: a capped server hands the tracks above its cap to this device
cdm = self.local_cdm
+18
View File
@@ -0,0 +1,18 @@
from typing import Any, Collection, Dict, Iterable
FLAT_DL_KEYS = frozenset({"title", "service", "title_id", "profile", "proxy", "no_proxy"})
def service_options(
data: Dict[str, Any], names: Collection[str], flat_excluded: Iterable[str] = FLAT_DL_KEYS
) -> Dict[str, Any]:
"""A request's service options named in ``names``: from its ``service_params`` object, else its flat keys.
With ``service_params`` present every flat key is a dl value, so it never fills a service option of the same name.
Without it, the flat keys in ``flat_excluded`` stay dl values.
"""
nested = data.get("service_params")
if isinstance(nested, dict):
return {k: v for k, v in nested.items() if k in names}
excluded = set(flat_excluded)
return {k: v for k, v in data.items() if k in names and k not in excluded}