mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
docs(service): make the Service docstring the one table of class variables and attributes
This commit is contained in:
@@ -57,19 +57,9 @@ Two kinds of members matter: **class variables** that give the details of the se
|
||||
|
||||
### Class variables
|
||||
|
||||
Declared at the top of your service class, these are static descriptors:
|
||||
|
||||
| Class variable | Type | Purpose |
|
||||
|---|---|---|
|
||||
| `ALIASES` | `tuple[str, ...]` | Alternate tags the service answers to, in any case (resolved by `Services.get_tag`). |
|
||||
| `GEOFENCE` | `tuple[str, ...]` | ISO region codes the service requires; empty means no geofence. unshackle treats the first entry as the "main region" for automatic proxy selection. |
|
||||
| `GEOBLOCK` | `tuple[str, ...]` | ISO region codes where the service refuses to work; every other region is allowed. Use it for a service that works worldwide except in its home market. |
|
||||
| `VAULT_TAG` | `Optional[str]` | Overrides the key-vault namespace; defaults to the service's own tag. |
|
||||
| `AUTH_METHODS` | `Optional[tuple[str, ...]]` | Accepted auth methods (`"cookies"` / `"credentials"`). When `None`, the REST `/services` endpoint infers them from `authenticate()`. |
|
||||
| `ANONYMOUS_SEARCH` | `bool` | `search()` needs no login and no state from `authenticate()`. A search then skips the cookie load, `authenticate()` and the cookie save. Default `False`. |
|
||||
| `ANIME` | `bool` | The catalogue is anime, so metadata lookups prefer AniList. A title's own `anime` flag overrides it. |
|
||||
| `METADATA_PROVIDER` | `Optional[str]` | The metadata provider the catalogue resolves through, such as `"tmdb"`. Lookups try it first. It only reorders `metadata_providers` and never adds a provider the user left out. A title's own `metadata_provider` overrides it. |
|
||||
| `DAILY` | `bool` | The catalogue is daily/date-based, so unshackle names episodes by air date. A title's own `daily` flag overrides it. |
|
||||
Declared at the top of your service class, class variables such as `ALIASES`, `GEOFENCE` and
|
||||
`NO_SUBTITLES` describe the service to the framework. The attributes table of
|
||||
[`Service`][unshackle.core.service.Service] in the Service API gives each one, with its type and purpose.
|
||||
|
||||
### Required methods
|
||||
|
||||
@@ -87,9 +77,7 @@ These are abstract. A service will not load unless it defines all three:
|
||||
- **`search()`**: raises `NotImplementedError` by default. Write it so that the
|
||||
`search` command can operate. unshackle calls `authenticate()` first, unless the
|
||||
service sets `ANONYMOUS_SEARCH`.
|
||||
- **DRM license hooks**: `get_widevine_service_certificate`, `get_widevine_license`,
|
||||
`get_playready_license` (delegates to Widevine by default), and
|
||||
`get_clearkey_license` (returns `None` by default). See [DRM hooks](#drm-hooks) below.
|
||||
- **Licence callbacks**: see [DRM hooks](#drm-hooks) below.
|
||||
- **`request_input(prompt, qr=None)`**: prompts the user. It goes through the remote-dl input
|
||||
bridge, the REST download job relay, or the rich console.
|
||||
- **`wait_for_approval(message, check, timeout, interval=3.0, qr=None)`**: shows a message, then
|
||||
@@ -106,18 +94,9 @@ These are abstract. A service will not load unless it defines all three:
|
||||
### What `__init__` wires up for you
|
||||
|
||||
You rarely override `__init__`. The base constructor takes the Click context and sets
|
||||
up the objects your methods rely on:
|
||||
|
||||
| Attribute | What it is |
|
||||
|---|---|
|
||||
| `self.config` | The merged service config (see [Request and HTTP session objects](#request-and-http-session-objects)). |
|
||||
| `self.log` | A `logging.Logger` named after your class. |
|
||||
| `self.session` | A ready-to-use HTTP session (headers, retries, and proxy applied). Send every request through it: with a proxy active, a request from service code that does not go through the proxy logs a warning. A separate HTTP session that goes direct on purpose sets `proxy_exempt = True`. |
|
||||
| `self.cache` | A generic key/value `Cacher` namespaced to the service. |
|
||||
| `self.title_cache` | A region- and account-aware `TitleCacher`. |
|
||||
| `self.cache_dir` | `config.directories.cache / <ServiceName>`. |
|
||||
| `self.credential` | The active `Credential`, or `None` until authenticated. |
|
||||
| `self.track_request` | A `TrackRequest` describing the requested codecs, ranges, and best-available flag (see below). |
|
||||
up the objects your methods rely on, such as `self.session`, `self.cache` and `self.track_request`.
|
||||
The attributes table of [`Service`][unshackle.core.service.Service] lists each one.
|
||||
[Request and HTTP session objects](#request-and-http-session-objects) explains the main ones.
|
||||
|
||||
The constructor also resolves proxies: it reads `--proxy`, which `dl` and `search` have already
|
||||
resolved through the per-service `proxy_map`, and, if `GEOFENCE` is set and you did not give an explicit proxy, does a live
|
||||
@@ -129,12 +108,11 @@ outside `GEOFENCE` logs a warning and the run continues.
|
||||
See [Proxies and VPN](../guide/proxies-and-vpn.md).
|
||||
|
||||
!!! note "`TrackRequest`"
|
||||
`self.track_request` is a small dataclass built from the CLI: `codecs`
|
||||
(from `--vcodec`, empty means "accept any"), `ranges` (from `--range`, default
|
||||
`[Video.Range.SDR]`), `best_available`, and `generate_dv` (from `--generate-dv`).
|
||||
Use it in `get_tracks` to decide which variants to fetch. A service that fetches
|
||||
`self.track_request` is a [`TrackRequest`][unshackle.core.service.TrackRequest] built from the
|
||||
CLI. Use it in `get_tracks` to decide which variants to fetch. A service that fetches
|
||||
the HYBRID variants itself must also fetch HDR10P when `generate_dv` is set and
|
||||
the title has no DV track, because unshackle prefers an HEVC HDR10+ base.
|
||||
`get_tracks_for_variants` does this for you.
|
||||
|
||||
## Titles and the `Title` hierarchy
|
||||
|
||||
@@ -240,9 +218,7 @@ Each subclass adds its own required fields on top of `id_` and `service`:
|
||||
changes nothing.
|
||||
|
||||
A `Song` is an ordinary `Title` on the one generic download path. unshackle has no
|
||||
music-specific service API and no separate music download loop. The
|
||||
`GROUP_AUDIO_DOWNLOADS` flag, `get_music_track_options` and `get_music_collection_label`
|
||||
no longer exist, and a music service does not need them.
|
||||
music-specific service API and no separate path for music.
|
||||
|
||||
Core picks the container of a `Song` from the codec in the downloaded file, so a music
|
||||
service does not rename the track path. Read
|
||||
@@ -443,7 +419,7 @@ least one and no track is left.
|
||||
from the `<c>` fragment timeline, and rebuilds a `moov`/`ftyp` init box from `CodecPrivateData`
|
||||
(supported for AVC, HEVC, DV-over-HEVC, AAC, EC-3, and TTML). A track id covers the manifest
|
||||
URL, so a title you build from one ISM manifest per codec or range keeps every variant:
|
||||
`Tracks.add` no longer reads the second manifest's tracks as duplicates of the first.
|
||||
`Tracks.add` does not read the tracks of the second manifest as duplicates of the first.
|
||||
|
||||
## DRM hooks
|
||||
|
||||
@@ -467,25 +443,31 @@ service to talk to the platform's license server. unshackle recognizes six DRM c
|
||||
### The service-side hooks
|
||||
|
||||
The framework supplies the CDM and the decryption backend. Your service gives the network
|
||||
calls that must carry the CDM's challenge. Override the hooks that your platform
|
||||
needs:
|
||||
calls that must carry the CDM's challenge. Override the licence callbacks that your platform
|
||||
needs: `get_widevine_service_certificate`, `get_widevine_license`, `get_playready_license` and
|
||||
`get_clearkey_license`. Each one gets `challenge`, `title` and `track` as keyword arguments, and
|
||||
returns the licence as the server gives it, without decoding it. The base `get_playready_license` calls
|
||||
`get_widevine_license`, and the base `get_clearkey_license` returns `None`, so that the framework
|
||||
POSTs the challenge to the manifest `Laurl`. The [Service API](../reference/api/service.md) gives the
|
||||
signature and the return type of each callback.
|
||||
|
||||
| Hook | Return | Purpose |
|
||||
|---|---|---|
|
||||
| `get_widevine_service_certificate(title, track, challenge)` | cert bytes | Optional privacy-mode service certificate. |
|
||||
| `get_widevine_license(title, track, challenge)` | license bytes | POST the Widevine challenge to the license server, return the response. |
|
||||
| `get_playready_license(title, track, challenge)` | license | Same for PlayReady; defaults to delegating to Widevine. |
|
||||
| `get_clearkey_license(title, track, challenge)` | dict / str / bytes / `None` | For `ClearKeyCENC`; return `None` to let the framework POST to the manifest `Laurl`. |
|
||||
| `get_fairplay_bridge_license(title, track, challenge, drm_system)` | license | Optional, not on the base class. Licenses a FairPlay-only HLS track; see [The FairPlay bridge hook](#the-fairplay-bridge-hook). |
|
||||
A licence callback can also declare optional keyword arguments. `get_widevine_license` can ask for
|
||||
`pssh` and `session_id`. `get_playready_license` can ask for `pssh_b64` and `session_id`.
|
||||
`session_id` is the open CDM session. A callback gets only the arguments that its signature declares.
|
||||
When a `--remote` client licenses with its own CDM, the callback on the server gets `pssh` or
|
||||
`pssh_b64` for the header that the client licenses, but no `session_id`.
|
||||
|
||||
You do not open CDM sessions or operate shaka-packager yourself. The `Widevine`/`PlayReady`
|
||||
classes drive the CDM handshake and call the decryption backend. Your hook is only the
|
||||
classes drive the CDM handshake and call the decryption backend. Your callback is only the
|
||||
"send this challenge, give me back the license" step.
|
||||
|
||||
### The FairPlay bridge hook
|
||||
|
||||
unshackle has no FairPlay CDM and never sends a FairPlay challenge. For a FairPlay-only track it can send a PlayReady or Widevine challenge for the KIDs of the init segments to the licence server of the platform, which works only when that server issues the same content key to PlayReady or Widevine. The bridge runs only for a service that defines
|
||||
`get_fairplay_bridge_license`. The base class does not define it.
|
||||
unshackle has no FairPlay CDM and never sends a FairPlay challenge. For a FairPlay-only HLS track it can
|
||||
send a PlayReady or Widevine challenge for the KIDs of the init segments to the licence server of the
|
||||
platform. This works only when that server issues the same content key to PlayReady or Widevine. The
|
||||
bridge runs only for a service that defines `get_fairplay_bridge_license`. The base class does not
|
||||
define it.
|
||||
|
||||
```python
|
||||
def get_fairplay_bridge_license(
|
||||
@@ -497,11 +479,12 @@ def get_fairplay_bridge_license(
|
||||
```
|
||||
|
||||
- `drm_system` is `"playready"` or `"widevine"`. The hook must accept it.
|
||||
- The hook can also declare the arguments of the licence callback of that system: `pssh` for Widevine,
|
||||
`pssh_b64` for PlayReady, and `session_id`. It gets only the arguments that it declares.
|
||||
- The hook can also declare the optional arguments of the licence callback of that system (see
|
||||
[The service-side hooks](#the-service-side-hooks)). It gets only the arguments that it declares.
|
||||
- The key IDs are on the DRM object of the track (`track.drm[0].kids`) when the hook runs. A service that
|
||||
needs its own request shape builds the request in the hook.
|
||||
- The challenge of every other track goes to `get_playready_license` or `get_widevine_license`, as before.
|
||||
- The hook gets only the challenges of FairPlay-only tracks. The challenge of every other track goes to
|
||||
`get_playready_license` or `get_widevine_license`.
|
||||
|
||||
The DRM system of the challenge is the `drm_preference` of the track, else the system of the configured
|
||||
CDM. For a licence server that issues the content key to one system only, set `drm_preference` on the
|
||||
@@ -513,22 +496,14 @@ for track in tracks.videos + tracks.audio:
|
||||
track.drm_preference = "playready"
|
||||
```
|
||||
|
||||
Without the hook, no challenge goes to the licence server for a FairPlay-only track, and `dl` leaves
|
||||
such a track out of the selection:
|
||||
The init segment (`EXT-X-MAP`) of the track must name a real key ID in its `tenc` box. When the `tenc`
|
||||
key ID is all zeros and the playlist has one key URI, the `pssh` boxes of the init segment can name it.
|
||||
unshackle never reads a key ID from the `skd://` URI.
|
||||
|
||||
- After the selection and the deferred DRM load, `dl` checks each selected video and audio track that has
|
||||
no DRM object. An HLS track costs one request for its media playlist. A DASH track is FairPlay-only when
|
||||
its `ContentProtection` elements name only DRM systems that unshackle cannot license and its init segment
|
||||
has no Widevine or PlayReady `pssh`.
|
||||
- `dl` removes each FairPlay-only track and selects again, so the run takes the next best track that
|
||||
licenses. It logs one line for each title: `Left out 2 FairPlay-only tracks: EXAMPLE has no FairPlay bridge hook`.
|
||||
- When a quality, range or codec flag matches only FairPlay-only tracks, the second selection skips the
|
||||
title with its usual reason, and `--best-available` takes its usual fallback. When each selected video
|
||||
or each selected audio track is FairPlay-only, `dl` skips the title.
|
||||
- `--list` shows every track and checks none. An import is not checked, because its export holds the
|
||||
content keys.
|
||||
- With the hook, `dl` still leaves out FairPlay-only DASH tracks, because the bridge works for HLS only.
|
||||
The line then reads `Left out 1 FairPlay-only track: DASH has no FairPlay bridge`.
|
||||
Without the hook, `dl` leaves each FairPlay-only track out of the selection and takes the next best
|
||||
track that licenses. It logs one line for each title:
|
||||
`Left out 2 FairPlay-only tracks: EXAMPLE has no FairPlay bridge hook`. With the hook, `dl` still leaves
|
||||
out FairPlay-only DASH tracks, because the bridge works for HLS only.
|
||||
|
||||
A licence server that does not issue the content key refuses the challenge, and can flag the device. Test
|
||||
the route with a licence-only run (`--cdm-only --skip-dl`) before a service relies on it.
|
||||
@@ -554,43 +529,8 @@ that needs a process on the server also works for a remote client.
|
||||
|
||||
### A playlist whose key tag unshackle cannot license
|
||||
|
||||
A media playlist can name only a key system that unshackle does not read, such as an `skd://` key
|
||||
over cbcs media, while the licence server of the platform also issues the same content key over
|
||||
PlayReady or Widevine. `HLS.download_track` licenses such a track when all of these conditions are true:
|
||||
|
||||
- The service has the [FairPlay bridge hook](#the-fairplay-bridge-hook).
|
||||
- The track has no DRM object.
|
||||
- Every `EXT-X-KEY` of its media playlist is `SAMPLE-AES` or `SAMPLE-AES-CTR` for a DRM system
|
||||
unshackle cannot license, or `METHOD=NONE`. A segment under `METHOD=NONE` must have an init segment
|
||||
that no encrypted segment uses: a decrypter that reads the encrypted init segment decrypts the clear
|
||||
samples too. A tag with `KEYFORMAT="identity"`, or with no `KEYFORMAT`, names no DRM system: each
|
||||
encrypted segment also needs a tag of a DRM system.
|
||||
- The `drm_preference` of the track, or else the configured CDM, is PlayReady or Widevine.
|
||||
- The init segment (`EXT-X-MAP`) of the track names a key ID in its `tenc` box. When the `tenc` key ID is all
|
||||
zeros, the playlist must have one key URI, and each init segment under the key tag must give the same
|
||||
key ID: its `tenc` key ID, or else the one key ID that its `pssh` boxes name. A clear init segment,
|
||||
such as the one of a clear lead or tail, does not count. MPEG-TS media has no init segment.
|
||||
- The init segment does not change without an `EXT-X-DISCONTINUITY`.
|
||||
|
||||
The download reads the key ID from the init segment, builds a DRM object for that DRM system with
|
||||
`drm_stub`, and sends the challenge to the hook. Over `--remote` the tracks reply tells the client that
|
||||
the service on the server has the hook. The server reads the same key ID at the licence step, accepts
|
||||
the DRM object of the client and sends the challenge to the hook of its service. A server that cannot
|
||||
read the key ID refuses the challenge. A licence that gives no content key stops the track before it
|
||||
downloads a segment. With the server CDM, the batch licence names the key IDs that the server read, so an
|
||||
answer without their content keys stops the track in the same way. A content key that does not decode
|
||||
the downloaded track stops the track with an error, and unshackle keeps no output file.
|
||||
|
||||
unshackle reads one key ID from each init segment. So it licenses a playlist that changes its content key
|
||||
correctly only when each content key has its own init segment with a real `tenc` key ID, after an
|
||||
`EXT-X-DISCONTINUITY`. When the first key ID is real, the download also reads the `tenc` key ID of
|
||||
each other init segment of the encrypted segments, and licenses each real key ID in its own licence step. A
|
||||
PlayReady DRM object then has one header for each key ID, because the header of `drm_stub` holds
|
||||
one key ID. A key ID that gets no content key stops the track before it downloads a segment. An
|
||||
init segment that gives no real key ID does not count.
|
||||
|
||||
A service that must choose the DRM system itself, or one that knows the key ID, puts the DRM object
|
||||
on the track:
|
||||
on the track and needs no [FairPlay bridge hook](#the-fairplay-bridge-hook):
|
||||
|
||||
1. Read the key ID from the init segment of the encrypted segments. `HLS.get_track_kid_from_init` returns it
|
||||
for the track, a reader of init segments (`HLS.init_reader(playlist, session)`) and that init segment.
|
||||
@@ -600,19 +540,10 @@ on the track:
|
||||
3. Let your licence callback send the challenge to the server that issues that key.
|
||||
|
||||
`drm_stub` builds the header from the key ID alone, so this works with a licence server that goes
|
||||
by the key ID, not with one that checks a header a packager signed. A platform can issue the
|
||||
content key of one package of a title and refuse another. When a track carries a Widevine or PlayReady DRM
|
||||
object and every `EXT-X-KEY` of its playlist is `SAMPLE-AES` or `SAMPLE-AES-CTR` for a key system
|
||||
unshackle cannot license, or `METHOD=NONE` under the same condition, `HLS.drop_unsupported_keys` removes the
|
||||
tags from the parsed playlist.
|
||||
The download then licenses and decrypts the track with the DRM object of the track. This works for
|
||||
one content key per track: the key changes of the playlist go with the tags. This limit also
|
||||
applies to a DRM object that the download builds. Any other track stops with
|
||||
`None of the key systems are supported` before it downloads a segment.
|
||||
|
||||
A DASH track with no DRM object stops with the same error when its init segment has an encrypted
|
||||
sample entry. A DASH track with no init segment stops after the download when the file has one, and
|
||||
unshackle deletes the file. A service sample decrypter turns off both checks.
|
||||
by the key ID, not with one that checks a header a packager signed. When every `EXT-X-KEY` of the
|
||||
playlist names a key system that unshackle cannot license, unshackle ignores those tags and licenses
|
||||
and decrypts the track with its DRM object, for one content key per track. Any other track that has
|
||||
only such tags stops with `None of the key systems are supported` before it downloads a segment.
|
||||
|
||||
### How unshackle chooses a CDM and finds keys
|
||||
|
||||
@@ -656,8 +587,8 @@ prepared, all derived from the Click context (`ctx`) that `dl` passes in.
|
||||
- **`self.session`**: a `requests.Session` with `config.headers` applied, a retry policy
|
||||
(`total=5`, backoff, retry on 429/5xx), and any resolved proxy already mounted. Pass this
|
||||
HTTP session into the manifest parsers and DRM calls so headers, cookies, and proxies flow
|
||||
everywhere. (unshackle also ships an rnet-based TLS-fingerprinting HTTP session that the
|
||||
newer `session()` factory uses and that the `network` config key configures.)
|
||||
everywhere. To impersonate a browser TLS fingerprint, override `get_session()` to return an
|
||||
rnet HTTP session from `session()` in `unshackle.core.session`, which the `network` config key configures.
|
||||
- **`self.credential`**: the active `Credential` (`username:password[:extra]`), populated
|
||||
by `authenticate`. unshackle uses its `sha1` as the account hash for cache keys. A login
|
||||
with cookies only uses a hash of the path of its cookie file.
|
||||
@@ -669,46 +600,8 @@ prepared, all derived from the Click context (`ctx`) that `dl` passes in.
|
||||
to stale data within `title_cache_max_retention` when an API call fails, and `--no-cache`
|
||||
bypasses it. If you put the cookies of a different account into the same cookie file, use
|
||||
`--reset-cache` once.
|
||||
- **`self.track_request`**: the `TrackRequest` describing what the user asked for
|
||||
(codecs, ranges, best-available), for use inside `get_tracks`.
|
||||
|
||||
!!! example "A minimal service skeleton"
|
||||
```python title="services/EXMP/__init__.py"
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.manifests import DASH
|
||||
from unshackle.core.titles import Movie, Movies
|
||||
|
||||
|
||||
class EXMP(Service):
|
||||
ALIASES = ("example",)
|
||||
GEOFENCE = ("us",)
|
||||
|
||||
def get_titles(self) -> Movies:
|
||||
data = self.session.get(f"https://api.example.com/title/{self.title}").json()
|
||||
return Movies([
|
||||
Movie(
|
||||
id_=data["id"],
|
||||
service=self.__class__,
|
||||
name=data["name"],
|
||||
year=data.get("year"),
|
||||
language=data.get("language"),
|
||||
)
|
||||
])
|
||||
|
||||
def get_tracks(self, title) -> "Tracks":
|
||||
mpd_url = self.session.get(...).json()["manifest"]
|
||||
mpd = self.session.get(mpd_url).text
|
||||
return DASH.from_text(mpd, mpd_url).to_tracks(language=title.language)
|
||||
|
||||
def get_chapters(self, title):
|
||||
return []
|
||||
|
||||
def get_widevine_license(self, *, challenge, title, track):
|
||||
return self.session.post("https://lic.example.com/wv", data=challenge).content
|
||||
```
|
||||
|
||||
The directory name (`EXMP`) must match the class name exactly. That stem is the
|
||||
service tag. Put your service in one of the `directories.services` paths.
|
||||
[Creating a Service](creating-a-service.md#minimal-service-skeleton) gives a complete minimal service.
|
||||
|
||||
## Where to go next
|
||||
|
||||
|
||||
Reference in New Issue
Block a user