fix(api): convert quality to heights for remote sessions

A string or zero -q from a REST client reached the service as raw text; it now gets the same checked height list as dl.
This commit is contained in:
imSp4rky
2026-11-16 14:44:47 -07:00
parent f1e4081313
commit 9693cbd39c
2 changed files with 38 additions and 6 deletions
@@ -48,3 +48,31 @@ def test_a_rest_job_converts_quality_and_passes_cdm_only(tmp_path: Path, monkeyp
download_manager.perform_download("job-1", "EXAMPLE1", "t1", {"quality": "1080p", "cdm_only": True})
assert exc.value.args == ([1080], [1080], True)
def session_params(monkeypatch: pytest.MonkeyPatch, data: dict) -> dict:
"""The params a remote session's service reads from its parent ctx, with the service machinery stubbed."""
seen: dict = {}
def fake_instantiate(parent_ctx: click.Context, *args: object) -> object:
seen["params"] = parent_ctx.params
return object()
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
monkeypatch.setattr(handlers.Services, "load", lambda service: None)
monkeypatch.setattr(handlers, "instantiate_service", fake_instantiate)
handlers.create_service_instance("EXAMPLE", "title", data, None, [], None)
return seen["params"]
@pytest.mark.parametrize(("quality", "heights"), [("1080p", [1080]), (["2160", "342"], [2160, 342]), (None, [])])
def test_a_remote_session_gets_quality_as_heights(monkeypatch: pytest.MonkeyPatch, quality: Any, heights: list) -> None:
assert session_params(monkeypatch, {"quality": quality})["quality"] == heights
@pytest.mark.parametrize("quality", ["tall", "0"])
def test_a_remote_session_refuses_a_malformed_quality(monkeypatch: pytest.MonkeyPatch, quality: str) -> None:
with pytest.raises(handlers.APIError) as exc:
session_params(monkeypatch, {"quality": quality})
assert exc.value.error_code == handlers.APIErrorCode.INVALID_INPUT
+10 -6
View File
@@ -1251,16 +1251,20 @@ def capped_error(message: str, cap: Optional[int] = None) -> APIError:
return APIError(APIErrorCode.SERVER_CDM_CAPPED, message, details=details)
def requested_height(quality: Any) -> Optional[int]:
"""The tallest height a client asked for with ``-q``; None when it asked for the best there is."""
from unshackle.core.utils.click_types import QUALITY_LIST
def requested_heights(quality: Any) -> list[int]:
"""The heights a client asked for with ``-q``, as the dl option gives them; empty for the best there is."""
try:
heights = QUALITY_LIST.convert(quality)
heights = QUALITY_LIST.convert(quality or [])
except click.BadParameter as e:
raise APIError(APIErrorCode.INVALID_INPUT, f"quality: {e.message}")
if any(height <= 0 for height in heights):
raise APIError(APIErrorCode.INVALID_INPUT, f"quality: every height must be above 0, not {quality!r}")
return heights
def requested_height(quality: Any) -> Optional[int]:
"""The tallest height a client asked for with ``-q``; None when it asked for the best there is."""
heights = requested_heights(quality)
return max(heights) if heights else None
@@ -3909,7 +3913,7 @@ def create_service_instance(
extra_params = {
"range_": range_values,
"vcodec": vcodec_values,
"quality": data.get("quality"),
"quality": requested_heights(data.get("quality")),
"best_available": data.get("best_available", False),
"generate_dv": data.get("generate_dv", False),
**forwarded_dl_params(data),