forked from kenzuya/unshackle
refactor(api): give every served service its dl options from the run plan
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
"""The dl option table: which dl options the REST API takes, their API defaults, and what a remote client forwards."""
|
||||
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Literal, get_args
|
||||
|
||||
import click
|
||||
|
||||
from unshackle.core.utils.click_types import AUDIO_CODEC_LIST
|
||||
from unshackle.core.utils.service_params import cast_service_options
|
||||
|
||||
log = logging.getLogger("api")
|
||||
|
||||
# Config keys accepted as natural names for params whose Python name dodges a builtin.
|
||||
DL_OPTION_ALIASES = {"range": "range_", "list": "list_"}
|
||||
DL_OPTION_API_NAMES = {click_name: api_name for api_name, click_name in DL_OPTION_ALIASES.items()}
|
||||
@@ -174,47 +170,3 @@ def remote_dl_params(params: Mapping[str, Any]) -> dict[str, Any]:
|
||||
elif params.get(key) is not None:
|
||||
sent[key] = [c.name if option.remote == "audio_codecs" else str(c) for c in params[key]]
|
||||
return sent
|
||||
|
||||
|
||||
def forwarded_dl_params(data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Read the dl track selection that a remote-dl client sends under ``dl_params``.
|
||||
|
||||
Services read these values from ``ctx.parent.params`` to pick the manifests they
|
||||
fetch. They travel in their own object so that they never collide with a service option that
|
||||
has the same name. An absent or malformed value gets the dl default, so an old client that
|
||||
sends no ``dl_params`` gets the same result as a ``dl`` run with no selection flags.
|
||||
``acodec`` holds codec names or aliases, which become ``Audio.Codec`` members the same way
|
||||
``dl -a`` converts them; an unknown name is dropped.
|
||||
"""
|
||||
raw = data.get("dl_params")
|
||||
if not isinstance(raw, dict):
|
||||
raw = {}
|
||||
|
||||
def str_list(key: str) -> list[str] | None:
|
||||
value = raw.get(key)
|
||||
if isinstance(value, list) and all(isinstance(v, str) for v in value):
|
||||
return list(value)
|
||||
if value is not None:
|
||||
log.warning(f"Ignoring dl_params.{key}: it must be an array of strings")
|
||||
return None
|
||||
|
||||
params: dict[str, Any] = {}
|
||||
for key, option in FORWARDED_DL_PARAMS:
|
||||
if option.remote == "flag":
|
||||
params[key] = raw.get(key) is True
|
||||
elif option.remote == "langs":
|
||||
names = str_list(key)
|
||||
params[key] = names if names is not None else list(option.default)
|
||||
else:
|
||||
names = str_list(key)
|
||||
# the dl CLI default is [], and some services test membership in it
|
||||
params[key] = list(option.default)
|
||||
for name in names or []:
|
||||
try:
|
||||
params[key].extend(AUDIO_CODEC_LIST.convert(name))
|
||||
except click.BadParameter:
|
||||
# imported here: unshackle.core.api imports this module
|
||||
from unshackle.core.api.sanitize import sanitize_log
|
||||
|
||||
log.warning(f"Ignoring unknown dl_params.{key} value: {sanitize_log(name)}")
|
||||
return params
|
||||
|
||||
Reference in New Issue
Block a user