feat(dl): run user post-download scripts with metadata variables

New post_scripts config and repeatable --postscript flag run a command per output, season/album folder, or run, substituting filename-template variables into pre-tokenized argv. The REST API rejects hook commands.

Closes #150
This commit is contained in:
imSp4rky
2026-08-18 08:26:14 -06:00
parent 511f96d8db
commit 89acfab279
17 changed files with 1154 additions and 23 deletions
+4
View File
@@ -360,6 +360,10 @@ def perform_download(
]
config.directories.cache = config.directories.cache / "_jobs" / cred_hash
for banned in ("postscript", "post_script", "post_scripts"):
if params.pop(banned, None) is not None:
log.warning(f"Ignoring '{banned}' in API download parameters; post-scripts are config-only")
vcodec_raw = params.get("vcodec")
if vcodec_raw:
if isinstance(vcodec_raw, str):
+4
View File
@@ -942,6 +942,10 @@ def validate_download_parameters(data: Dict[str, Any]) -> Optional[str]:
Returns:
None if valid, error message string if invalid
"""
for banned in ("postscript", "post_script", "post_scripts"):
if data.get(banned):
return f"'{banned}' is not accepted over the API. Define post_scripts in unshackle.yaml."
if "vcodec" in data and data["vcodec"]:
err = check_codec(data["vcodec"], VALID_VCODECS, "vcodec")
if err:
+4 -1
View File
@@ -594,7 +594,10 @@ async def download(request: web.Request) -> web.Response:
Download content based on provided parameters.
---
summary: Download content
description: Download video content based on specified parameters
description: >-
Download video content based on specified parameters.
'postscript', 'post_script' and 'post_scripts' are never accepted and return 400;
post-download hooks are configured only in unshackle.yaml.
requestBody:
required: true
content:
+1
View File
@@ -131,6 +131,7 @@ class Config:
# mask local base dirs (install root/venv/home) in logged paths; False shows full paths
self.redact_paths: bool = kwargs.get("redact_paths", True)
self.continue_downloads: bool = kwargs.get("continue_downloads", False)
self.post_scripts: list = kwargs.get("post_scripts") or []
self.language_tags: dict = kwargs.get("language_tags") or {}
self.tag_rules: list = kwargs.get("tag_rules") or []
+1 -1
View File
@@ -132,7 +132,7 @@ def sanitize_filename(filename: str, spacer: str = ".") -> str:
filename = unidecode(filename)
filename = re.sub(r"\[\(+", "[", filename)
filename = re.sub(r"\)+\]", "]", filename)
filename = "".join(c for c in filename if unicodedata.category(c) != "Mn") # hidden characters
filename = "".join(c for c in filename if unicodedata.category(c) not in ("Mn", "Cc"))
filename = filename.replace("/", " & ").replace(";", " & ") # e.g. multi-episode filenames
if spacer == ".":
filename = re.sub(r" - ", spacer, filename) # title separators to spacer (avoids .-. pattern)
+216
View File
@@ -0,0 +1,216 @@
"""Run user-defined scripts after a download completes.
A command is tokenized once, then variables are substituted into the resulting argv
tokens and the process is spawned with ``shell=False``. Substituting after tokenizing
is what makes a title like ``Bob"; rm -rf ~`` harmless: a value can never become a new
token or a shell operator. The cost is that the command has no shell features and no
interpreter lookup, so users name the interpreter themselves (``python upload.py``).
Scripts are fire-and-forget: unshackle spawns them, logs the command, and moves on. It
does not wait, capture output, time them out, or let a script's exit code change its own.
"""
from __future__ import annotations
import logging
import os
import re
import shlex
import subprocess
from pathlib import Path
from typing import Any, Iterator, Optional, Sequence
from unshackle.core.config import config
from unshackle.core.utilities import log_event
from unshackle.core.utils.redact import redact_path, redact_text
log = logging.getLogger("post-script")
VARIABLE_RE = re.compile(r"\{([a-z_]+)\}")
EVENTS = ("success", "failure")
MODES = ("file", "season", "run")
SIDECAR_SEPARATOR = "\n"
_warned_entries: set[str] = set()
def tokenize(command: str) -> list[str]:
"""Split a command string into argv tokens, before any variable is substituted."""
if os.name == "nt":
def unquote(token: str) -> str:
if len(token) > 1 and token[0] == token[-1] and token[0] in "\"'":
return token[1:-1]
match = re.match(r"^([^\"']*=)([\"'])(.*)\2$", token)
return f"{match.group(1)}{match.group(3)}" if match else token
return [unquote(token) for token in shlex.split(command, posix=False)]
return shlex.split(command)
def substitute(tokens: Sequence[str], context: dict[str, str]) -> list[str]:
"""Replace every ``{variable}`` inside each token; unknown or empty ones become ''."""
unknown: set[str] = set()
def replace(match: re.Match[str]) -> str:
name = match.group(1)
if name not in context:
unknown.add(name)
return context.get(name, "")
argv = [VARIABLE_RE.sub(replace, token) for token in tokens]
if unknown:
log.warning("Unknown post-script variables treated as empty: %s", ", ".join(sorted(unknown)))
return argv
def _data_injected_flag(tokens: Sequence[str], argv: Sequence[str]) -> Optional[str]:
"""A token that only became a ``-flag`` after substitution.
Tokenizing before substituting stops a value becoming a *new* token, but a bare
``{title}`` token whose value is service-controlled (``--upload-file=x``) still becomes a
whole option token to the user's script. Flag it when the leading ``-`` came from the
value, not the template. A literal flag like ``--opt={var}`` starts with ``-`` in the
template itself and is the user's own, so it is never flagged.
"""
for template, token in zip(tokens, argv):
if token.startswith("-") and not template.startswith("-"):
return token
return None
def build_context(
title: Any,
media_info: Optional[Any] = None,
*,
filepath: Optional[Path] = None,
folder: Optional[Path] = None,
sidecars: Sequence[Path] = (),
service: str = "",
ids: Optional[dict[str, Any]] = None,
error: str = "",
) -> dict[str, str]:
"""Build the variable set for one output file.
Metadata comes from the same naming context that produced the file's name, so
``{quality}`` and ``{hdr}`` always describe *this* file. That is what lets
``-q 1080,2160 -r HDR10,SDR`` label its four outputs correctly. Season and episode
are handed over as plain numbers rather than the filename's ``S01E05`` form: the
spacer and padding belong to the filename template, not to the data.
"""
context: dict[str, str] = {}
if media_info is not None:
try:
for key, value in title.build_template_context(media_info, show_service=True).items():
context[key] = "" if value is None else str(value)
except Exception as e: # noqa: BLE001 - a naming quirk must never stop the download
log.debug("Could not build post-script metadata context: %s", e)
context["vcodec"] = context.get("video", "")
context["acodec"] = context.get("audio", "")
season = getattr(title, "season", None)
number = getattr(title, "number", None)
context["season"] = "" if season is None else str(season)
context["episode"] = "" if number is None else str(number)
context["episode_name"] = str(getattr(title, "name", "") or "") if season is not None else ""
context["title_raw"] = str(getattr(title, "title", None) or getattr(title, "name", "") or "")
context.setdefault("title", context["title_raw"].replace("$", "S"))
context.setdefault("year", str(getattr(title, "year", "") or ""))
context["title_id"] = str(getattr(title, "id", "") or "")
context["service"] = service
context["filepath"] = str(filepath) if filepath else ""
context["filename"] = filepath.name if filepath else ""
context["ext"] = filepath.suffix if filepath else ""
context["folder"] = str(folder or (filepath.parent if filepath else ""))
context["sidecars"] = SIDECAR_SEPARATOR.join(str(path) for path in sidecars)
ids = ids or {}
for name in ("tmdb", "imdb", "tvdb"):
value = ids.get(name)
context[name] = "" if value is None else str(value)
context["error"] = error
return context
def _entries(event: str, mode: str, overrides: Sequence[str] = ()) -> Iterator[str]:
"""Commands configured for this event and mode; --postscript replaces the config list."""
if overrides:
if event == "success" and mode == "file":
yield from overrides
return
raw = config.post_scripts
if isinstance(raw, (str, dict)):
raw = [raw]
elif not isinstance(raw, list):
if raw:
log.warning("Ignoring malformed post_scripts config: %r", raw)
raw = []
for entry in raw:
if isinstance(entry, str):
entry = {"command": entry}
if not isinstance(entry, dict):
log.warning("Ignoring malformed post_scripts entry: %r", entry)
continue
command = str(entry.get("command") or "").strip()
if not command:
continue
entry_event = str(entry.get("event") or "success")
entry_mode = str(entry.get("mode") or "file")
if entry_event not in EVENTS or entry_mode not in MODES:
if command not in _warned_entries:
_warned_entries.add(command)
log.warning(
"Ignoring post_scripts entry with unknown event/mode %r/%r (valid: %s / %s)",
entry_event,
entry_mode,
"|".join(EVENTS),
"|".join(MODES),
)
continue
if entry_event != event or entry_mode != mode:
continue
yield command
def dispatch(event: str, mode: str, context: dict[str, str], overrides: Sequence[str] = ()) -> None:
"""Spawn every post-script configured for `event` and `mode`. Never raises."""
for command in _entries(event, mode, overrides):
try:
tokens = tokenize(command)
except ValueError as e: # shlex: unbalanced quote in the command template
log.warning("Skipping malformed post-script command (%s): %s", e, redact_path(command))
continue
argv = substitute(tokens, context)
if not argv:
continue
injected = _data_injected_flag(tokens, argv)
if injected is not None:
log.warning(
"Skipping post-script: a substituted value expands to the option-like token %r; "
"prefix the variable (--opt={var}) so service metadata cannot forge a flag",
redact_text(redact_path(injected)),
)
continue
safe_command = redact_text(redact_path(shlex.join(argv)))
log.debug("Running post-script: %s", safe_command)
log_event(
"post_script_dispatch",
message=f"Running post-script for {event}/{mode}",
context={"event": event, "mode": mode, "command": safe_command, "argc": len(argv)},
)
detach: dict[str, Any] = (
{"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP} # type: ignore[attr-defined]
if os.name == "nt"
else {"start_new_session": True}
)
try:
subprocess.Popen(argv, stdin=subprocess.DEVNULL, **detach)
except (OSError, ValueError) as e:
log.warning("Post-script failed to start (%s): %s", e, redact_text(redact_path(argv[0])))