diff --git a/docs/dev/rest-api/remote-sessions.md b/docs/dev/rest-api/remote-sessions.md index 21034179..e671de3d 100644 --- a/docs/dev/rest-api/remote-sessions.md +++ b/docs/dev/rest-api/remote-sessions.md @@ -446,7 +446,8 @@ file on the server travels as base64, and the client writes it to its own temp d "server_cdm": false, "server_cdm_type": "widevine", "sample_decrypt_tracks": [], - "fairplay_bridge": false + "fairplay_bridge": false, + "license_first": false } ``` @@ -461,6 +462,7 @@ The remote session fields (`TracksReply`): | `server_cdm_type` | The DRM system that a server-CDM licence takes first for the tracks of this title. | | `sample_decrypt_tracks` | The IDs of the tracks that the service decrypts itself, through the `samples` route. | | `fairplay_bridge` | `true` when the service licenses FairPlay-only tracks through a bridge on the server. | +| `license_first` | The `LICENSE_FIRST` value of the service. When `true`, the client licenses every track of a title before any track downloads, as with `--license-first`. | | Status | Error code | Meaning | | --- | --- | --- | diff --git a/docs/guide/cli-reference.md b/docs/guide/cli-reference.md index 34499d20..0f71d474 100644 --- a/docs/guide/cli-reference.md +++ b/docs/guide/cli-reference.md @@ -216,7 +216,7 @@ Keep only certain track types, or skip certain track types. | `--vaults-only` | Get no licence. A content key that no key vault, export or earlier track gives fails the title. With both flags, the last one wins. | | `--cdm ` | Use the named CDM device for this run, and ignore the `cdm` config mapping, its quality and Widevine/PlayReady entries included. Over `--remote`, that device licenses the remote session, not the server CDM. | | `--skip-dl` | Skip the download but still retrieve keys. | -| `--license-first` | Get the content keys of every track of a title before any track of that title downloads. A failed licence then stops the run before that title downloads data. Over `--remote` it applies on the client. No effect with `--skip-dl`. | +| `--license-first` | Get the content keys of every track of a title before any track of that title downloads. A failed licence then stops the run before that title downloads data. Over `--remote` it applies on the client. No effect with `--skip-dl`. A service can turn it on for every run with [`LICENSE_FIRST`](../reference/api/service.md). | | `--all-drm` | License each track with both Widevine and PlayReady. Needs a CDM for each DRM system. Key Vaults are read first; `--cdm-only` sends a challenge for each system. Not for use with `--cdm`. | | `--export` | Write an [export](downloading.md#keys-vaults-and-export) of each title's tracks, DRM init data and content keys to the exports directory. | diff --git a/docs/guide/downloading.md b/docs/guide/downloading.md index 9064bcb8..935fe1e2 100644 --- a/docs/guide/downloading.md +++ b/docs/guide/downloading.md @@ -68,6 +68,7 @@ prints every title the service returned and stops before `--wanted`/`--latest-ep filtering. `--skip-dl` runs the licence step but skips the segment download. `--license-first` runs the licence step of every track of a title before any segment of that title downloads, so a failed licence stops the run before that title downloads data. The download then sends no second licence. +A service can turn it on for every run with [`LICENSE_FIRST`](../reference/api/service.md). ## Choosing quality diff --git a/docs/reference/configuration/download.md b/docs/reference/configuration/download.md index c4c60ccf..bd21776f 100644 --- a/docs/reference/configuration/download.md +++ b/docs/reference/configuration/download.md @@ -56,7 +56,7 @@ Common config keys. This is a useful subset, and every `dl` flag works: | `slow` | str | *(unset)* | Inter-title delay, e.g. `"20-40"`. | | `best_available` | bool | `false` | Warn instead of failing when a requested resolution, range, video language, or audio language is missing, or when one DRM system does not license under `--all-drm`. The CLI flag is `--best-available`, also spelled `--warn-only`. | | `skip_missing` | bool | `false` | Skip a title that misses a required language (`require_audio`, `require_video`, `require_subs`) or a track that a filter asks for, and continue with the next title. A run with one title, or a run that skips every title, still fails. The CLI flag is `--skip-missing`. For REST API jobs, set it in the [`serve`](services.md#serve) block. | -| `license_first` | bool | `false` | Get the content keys of every track of a title before any track of that title downloads. A failed licence stops the run before that title downloads data. The CLI flag is `--license-first`. For REST API jobs, set it in the [`serve`](services.md#serve) block or send `license_first` in the request. | +| `license_first` | bool | `false` | Get the content keys of every track of a title before any track of that title downloads. A failed licence stops the run before that title downloads data. The CLI flag is `--license-first`. A `false` value does not turn off the [`LICENSE_FIRST`](../api/service.md) of a service. For REST API jobs, set it in the [`serve`](services.md#serve) block or send `license_first` in the request. | | `generate_dv` | bool | `false` | With `-r HYBRID`, make Dolby Vision from the HDR10+ metadata or the decoded video when a title has no DV track. With `-r HDR10` or `-r HDR10P`, make it for that base and ignore a DV track. See [Dolby Vision without a DV track](../../guide/downloading.md#dolby-vision-without-a-dv-track). | | `proxy` | str | *(unset)* | Default proxy URI or 2-letter country. | | `no_folder` | bool | `false` | Do not create a per-title folder. | diff --git a/tests/commands/test_license_first.py b/tests/commands/test_license_first.py index 44e94ab8..a8d2f708 100644 --- a/tests/commands/test_license_first.py +++ b/tests/commands/test_license_first.py @@ -128,6 +128,22 @@ def test_without_the_flag_a_track_downloads_before_a_later_licence_fails( assert run.fetched == ["Video_video.mp4"], result.output +def test_a_service_with_license_first_licenses_every_track_with_no_flag( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + from unshackle.core import services + + monkeypatch.setattr(services.ensure_loaded("EXAMPLE"), "LICENSE_FIRST", True) + run = Run(monkeypatch, tmp_path, refuse="audio") + + result = run() + + assert "Licence refused for track audio" in result.output + assert result.exit_code == 1 + assert run.licences == ["video", "audio"] + assert run.fetched == [] + + def test_the_download_pass_sends_no_second_licence(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: run = Run(monkeypatch, tmp_path) diff --git a/tests/remote/unit/test_remote_loopback.py b/tests/remote/unit/test_remote_loopback.py index a8a89fe0..af958af2 100644 --- a/tests/remote/unit/test_remote_loopback.py +++ b/tests/remote/unit/test_remote_loopback.py @@ -46,6 +46,7 @@ class StubService: A test puts extra service work in ``hooks``: "titles" runs inside get_titles, on the service thread. """ + LICENSE_FIRST = False logins = 0 hooks: dict[str, Callable[[StubService], None]] = {} @@ -290,3 +291,18 @@ def test_server_request_records_never_reach_the_remote_client( assert all("body" not in e["response"] for e in entries) own = [e for e in entries if e["request"]["host"] == server_host and e["request"]["path"].endswith("/titles")] assert own and all((tmp_path / "server" / e["response"]["body_file"]).is_file() for e in own) + + +@pytest.mark.parametrize("forced", [True, False]) +def test_the_client_reports_the_license_first_of_the_served_service( + server: SimpleNamespace, monkeypatch: pytest.MonkeyPatch, forced: bool +) -> None: + monkeypatch.setattr(StubService, "LICENSE_FIRST", forced) + remote = make_client(server.url) + try: + remote.authenticate() + remote.get_tracks(remote.get_titles()[0]) + finally: + remote.close() + + assert remote.LICENSE_FIRST is forced diff --git a/tests/remote/unit/test_server_cdm_type_override.py b/tests/remote/unit/test_server_cdm_type_override.py index 6af6841a..58ced6f0 100644 --- a/tests/remote/unit/test_server_cdm_type_override.py +++ b/tests/remote/unit/test_server_cdm_type_override.py @@ -263,6 +263,8 @@ def test_the_tracks_response_names_the_system_of_the_loaded_device(monkeypatch, monkeypatch.setattr(store_mod.bus, "publish", lambda topic, data: None) class StubService: + LICENSE_FIRST = False + session = SimpleNamespace(headers={}, cookies=[]) ctx = SimpleNamespace(obj=SimpleNamespace(cdm=SimpleNamespace(is_playready=system == "playready"))) @@ -308,6 +310,7 @@ def _reply_then_licence(monkeypatch, track, drm_type, loaded=None): "get_chapters": lambda self, title: [], "decrypts_samples": lambda self, track: False, "fairplay_bridge": lambda self: None, + "LICENSE_FIRST": False, "get_widevine_service_certificate": lambda self, **k: None, "get_widevine_license": lambda self, **k: b"", "get_playready_license": lambda self, **k: b"", diff --git a/tests/remote/unit/test_session_plugins.py b/tests/remote/unit/test_session_plugins.py index 0b0422ff..b05d9d51 100644 --- a/tests/remote/unit/test_session_plugins.py +++ b/tests/remote/unit/test_session_plugins.py @@ -116,6 +116,8 @@ def tracks_reply(client_plugins: Any) -> tuple[Any, Any]: """The answer of the tracks route for a title with an EXAMPLE plugin track (its body or its APIError), and the session.""" class StubService: + LICENSE_FIRST = False + session = SimpleNamespace(headers={}, cookies=[]) def fairplay_bridge(self) -> None: diff --git a/tests/remote/unit/test_session_reporting.py b/tests/remote/unit/test_session_reporting.py index 97de57c8..8b6fdbf4 100644 --- a/tests/remote/unit/test_session_reporting.py +++ b/tests/remote/unit/test_session_reporting.py @@ -110,6 +110,8 @@ def test_tracks_handler_marks_the_title_live_before_it_fetches(monkeypatch: pyte monkeypatch.setattr(store_mod.bus, "publish", lambda topic, data: published.append((topic, data))) class StubService: + LICENSE_FIRST = False + session = SimpleNamespace(headers={}, cookies=[]) def fairplay_bridge(self) -> None: diff --git a/tests/test_client_session_auth.py b/tests/test_client_session_auth.py index e23bd314..5cd69544 100644 --- a/tests/test_client_session_auth.py +++ b/tests/test_client_session_auth.py @@ -97,6 +97,8 @@ def call_session_handler(handler, body, service, **entry_fields): class StubService: + LICENSE_FIRST = False + def __init__(self, tracks=(), cookies=()): self.session = SimpleNamespace(headers={}, cookies=list(cookies)) self.tracks = list(tracks) diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index 65378914..d0eeba43 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -2452,7 +2452,7 @@ class dl: progress_callables=tracks_progress_callables, disk_check=disk_check, ) - if license_first and not skip_dl: + if (license_first or service.LICENSE_FIRST) and not skip_dl: license_before_download( title.tracks, downloads, diff --git a/unshackle/core/api/handlers.py b/unshackle/core/api/handlers.py index a26e081f..5de7a6ee 100644 --- a/unshackle/core/api/handlers.py +++ b/unshackle/core/api/handlers.py @@ -3274,6 +3274,7 @@ async def session_tracks_handler( str(t.id) for t in [*tracks.videos, *tracks.audio] if service_instance.decrypts_samples(t) ], fairplay_bridge=service_instance.fairplay_bridge() is not None, + license_first=service_instance.LICENSE_FIRST, ) reply = await asyncio.to_thread( diff --git a/unshackle/core/api/routes.py b/unshackle/core/api/routes.py index e5bebcfa..32ee63e0 100644 --- a/unshackle/core/api/routes.py +++ b/unshackle/core/api/routes.py @@ -1263,7 +1263,8 @@ async def download(request: web.Request) -> web.Response: default: false description: >- Get the content keys of every track of a title before any track of the title downloads. A licence - failure stops the run before that title downloads data. No effect with `skip_dl`. + failure stops the run before that title downloads data. No effect with `skip_dl`. A service with + `LICENSE_FIRST` always does this. all_drm: type: boolean default: false diff --git a/unshackle/core/remote_protocol.py b/unshackle/core/remote_protocol.py index 6a985001..d6e4f804 100644 --- a/unshackle/core/remote_protocol.py +++ b/unshackle/core/remote_protocol.py @@ -291,6 +291,7 @@ class TracksReply(TypedDict, total=False): server_cdm_type: str sample_decrypt_tracks: list[str] fairplay_bridge: bool + license_first: bool class SegmentsRequest(TypedDict): diff --git a/unshackle/core/remote_service.py b/unshackle/core/remote_service.py index df048bf1..c0bc8718 100644 --- a/unshackle/core/remote_service.py +++ b/unshackle/core/remote_service.py @@ -1502,6 +1502,7 @@ class RemoteService(TitleSource): self.server_cdm_type = session_fields.get("server_cdm_type", "widevine") self._sample_decrypt_tracks.update(session_fields.get("sample_decrypt_tracks") or []) self._fairplay_bridge = bool(session_fields.get("fairplay_bridge")) + self.LICENSE_FIRST = bool(session_fields.get("license_first")) for track in [*tracks.videos, *tracks.audio, *tracks.subtitles]: if track.descriptor == Track.Descriptor.HLS: diff --git a/unshackle/core/service.py b/unshackle/core/service.py index 2b42b3cc..a5549ac3 100644 --- a/unshackle/core/service.py +++ b/unshackle/core/service.py @@ -162,6 +162,8 @@ class Service(TitleSource): METADATA_PROVIDER: The metadata provider that lookups try first, such as `"tmdb"`. It never adds a provider that `metadata_providers` leaves out. `Title.metadata_provider` overrides it. DAILY: The catalogue is date-based, so dl names episodes by air date. `Title.daily` overrides it. + LICENSE_FIRST: Every run of the service licenses each track of a title before any track downloads, as with + `dl --license-first`, locally and over `--remote`. The user cannot turn it off; `--skip-dl` skips it. config (dict): The `config.yaml` of the service with the `services.` block of `unshackle.yaml` merged over it. log (logging.Logger): A logger named after the service class. diff --git a/unshackle/core/title_source.py b/unshackle/core/title_source.py index 0f1acf05..080af918 100644 --- a/unshackle/core/title_source.py +++ b/unshackle/core/title_source.py @@ -132,6 +132,7 @@ class TitleSource(metaclass=ABCMeta): ANIME: bool = False METADATA_PROVIDER: str | None = None DAILY: bool = False + LICENSE_FIRST: bool = False ctx: click.Context log: logging.Logger diff --git a/unshackle/services/EXAMPLE/__init__.py b/unshackle/services/EXAMPLE/__init__.py index 613096ed..1a952f2f 100644 --- a/unshackle/services/EXAMPLE/__init__.py +++ b/unshackle/services/EXAMPLE/__init__.py @@ -83,6 +83,9 @@ class EXAMPLE(Service): # Lookups ask it first, ahead of AniList. It never adds one that the user's metadata_providers # leaves out. Set it when one index matches the names this service gives better than the default order. METADATA_PROVIDER = None + # LICENSE_FIRST: set True when the platform flags an account that downloads without a licence, or + # a licence fails often. Every run then licenses all tracks of a title before any track downloads. + LICENSE_FIRST = False # ANONYMOUS_SEARCH: set True only when search() needs no login and no state from # authenticate(). A search then skips the cookie load, authenticate() and the cookie save. ANONYMOUS_SEARCH = False