mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-19 07:25:11 +00:00
Compare commits
3
Commits
1b20b2bbf9
...
39ec591c48
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39ec591c48 | ||
|
|
e5c339934c | ||
|
|
f5bd24b5e9 |
@@ -182,7 +182,7 @@ For a service with `anonymous_search`, the server does not log in and ignores `c
|
||||
| `400` | `INVALID_PARAMETERS` | Missing `query`. |
|
||||
| `400` | `INVALID_SERVICE` | Unknown or disallowed service. |
|
||||
| `400` | `INVALID_PROXY` | Proxy could not be resolved. |
|
||||
| `502` | `SERVICE_ERROR` | Service failed to initialise, or does not support search. |
|
||||
| `422` | `SERVICE_ERROR` | Service failed to initialise, or does not support search. |
|
||||
|
||||
### `POST /api/list-titles`
|
||||
|
||||
@@ -1141,7 +1141,7 @@ Get the HLS segments a service drops (ads, bumpers, dub cards). The server runs
|
||||
| `200` | - | Unwanted segment URIs, or `null`. |
|
||||
| `400` | `INVALID_INPUT` | Missing `track_id`. |
|
||||
| `404` | `TRACK_NOT_FOUND` | An unknown track ID was requested. |
|
||||
| `500` | `SERVICE_ERROR` | The playlist request or the filter failed. |
|
||||
| `422` | `SERVICE_ERROR` | The playlist request or the filter failed. |
|
||||
|
||||
### `POST /api/session/{session_id}/samples`
|
||||
|
||||
@@ -1159,7 +1159,7 @@ The client sends at most 8 MiB of samples per request, and the route accepts a b
|
||||
| `413` | - | The body is larger than 16 MiB. |
|
||||
| `403` | `FORBIDDEN` | The API key has no `server_cdm` grant for the service. |
|
||||
| `404` | `TRACK_NOT_FOUND` | An unknown track ID was requested. |
|
||||
| `500` | `SERVICE_ERROR` | The service's decrypter failed. |
|
||||
| `422` | `SERVICE_ERROR` | The service's decrypter failed. |
|
||||
|
||||
### `POST /api/session/{session_id}/license`
|
||||
|
||||
@@ -1211,7 +1211,7 @@ A slow answer arrives as a [slow response](#slow-responses).
|
||||
| `400` | `INVALID_PARAMETERS` | Unsupported `drm_type`. |
|
||||
| `404` | `TRACK_NOT_FOUND` | Unknown track ID. |
|
||||
| `404` | `NO_CONTENT` | Server CDM produced no keys. |
|
||||
| `502` | `SERVICE_ERROR` | The service aborted during licensing. |
|
||||
| `422` | `SERVICE_ERROR` | The service aborted during licensing. |
|
||||
|
||||
### `POST /api/session/{session_id}/keys/bad`
|
||||
|
||||
@@ -1288,30 +1288,7 @@ The server serialises every structured error raised inside a handler to the same
|
||||
|
||||
### Error codes and default status
|
||||
|
||||
| Error code | HTTP status |
|
||||
| --- | --- |
|
||||
| `INVALID_INPUT` | 400 |
|
||||
| `INVALID_SERVICE` | 400 |
|
||||
| `INVALID_PROXY` | 400 |
|
||||
| `INVALID_PARAMETERS` | 400 |
|
||||
| `AUTH_FAILED` | 401 |
|
||||
| `FORBIDDEN` | 403 |
|
||||
| `SERVER_CDM_CAPPED` | 403 |
|
||||
| `GEOFENCE` | 403 |
|
||||
| `NOT_FOUND` | 404 |
|
||||
| `NO_CONTENT` | 404 |
|
||||
| `JOB_NOT_FOUND` | 404 |
|
||||
| `SESSION_NOT_FOUND` | 404 |
|
||||
| `TRACK_NOT_FOUND` | 404 |
|
||||
| `CONFLICT` | 409 |
|
||||
| `RATE_LIMITED` | 429 |
|
||||
| `INTERNAL_ERROR` | 500 |
|
||||
| `DOWNLOAD_ERROR` | 500 |
|
||||
| `WORKER_ERROR` | 500 |
|
||||
| `SERVICE_ERROR` | 502 |
|
||||
| `DRM_ERROR` | 502 |
|
||||
| `NETWORK_ERROR` | 503 |
|
||||
| `SERVICE_UNAVAILABLE` | 503 |
|
||||
The [error code catalogue](errors.md#error-code-catalogue) gives the default HTTP status of each error code.
|
||||
|
||||
When a generic (uncategorized) exception reaches a handler, unshackle inspects its message and type to pick a code. For example, messages mentioning "connection" or "timeout" become `NETWORK_ERROR` (503, retryable), "rate limit" or "429" become `RATE_LIMITED` (429, retryable), and anything unrecognized collapses to `INTERNAL_ERROR` (500).
|
||||
|
||||
|
||||
@@ -92,6 +92,10 @@ but the defaults hold for the overwhelming majority of responses.
|
||||
|
||||
### Client errors (4xx)
|
||||
|
||||
`SERVICE_ERROR` and `DRM_ERROR` are upstream failures, but they use `422`, not `502`.
|
||||
A proxy such as Cloudflare replaces the body of a `502` or `504` with its own page, and
|
||||
the JSON error is then lost.
|
||||
|
||||
| Code | Status | Meaning |
|
||||
|---|---|---|
|
||||
| `INVALID_INPUT` | 400 | A required field is missing, or the request body could not be parsed. Also used as the generic "your input was malformed" code. |
|
||||
@@ -108,6 +112,8 @@ but the defaults hold for the overwhelming majority of responses.
|
||||
| `SESSION_NOT_FOUND` | 404 | The remote-dl session does not exist or has expired. |
|
||||
| `TRACK_NOT_FOUND` | 404 | A referenced track ID is not present in the remote session. |
|
||||
| `CONFLICT` | 409 | The target resource is in a state that disallows the action (retrying a running job, prioritising a non-queued job, clearing cache during an active download). |
|
||||
| `SERVICE_ERROR` | 422 | The streaming service's API returned an error, or the service does not support the requested operation. |
|
||||
| `DRM_ERROR` | 422 | DRM or license acquisition failed. |
|
||||
| `RATE_LIMITED` | 429 | The streaming service is rate-limiting requests. Retryable. |
|
||||
|
||||
!!! warning "Not every `429` is `RATE_LIMITED`"
|
||||
@@ -126,8 +132,6 @@ but the defaults hold for the overwhelming majority of responses.
|
||||
| `INTERNAL_ERROR` | 500 | An unexpected server-side error. This is the fallback for any exception the server could not categorise. |
|
||||
| `DOWNLOAD_ERROR` | 500 | The download process itself failed. |
|
||||
| `WORKER_ERROR` | 500 | The download worker subprocess errored out. |
|
||||
| `SERVICE_ERROR` | 502 | The streaming service's API returned an error, or the service does not support the requested operation. |
|
||||
| `DRM_ERROR` | 502 | DRM or license acquisition failed. Retryable. |
|
||||
| `NETWORK_ERROR` | 503 | A network connectivity problem (connection, timeout, DNS, TLS). Retryable. |
|
||||
| `SERVICE_UNAVAILABLE` | 503 | The service is temporarily unavailable or under maintenance. Retryable. |
|
||||
|
||||
@@ -156,7 +160,7 @@ The classification is keyword-based, checked in order:
|
||||
| `geofence`, `geoblock`, `region`, `not available in`, `territory` | `GEOFENCE` (403) | no |
|
||||
| `not found`, `404`, `does not exist`, `invalid id` | `NOT_FOUND` (404) | no |
|
||||
| `rate limit`, `too many requests`, `429`, `throttle` | `RATE_LIMITED` (429) | yes |
|
||||
| `drm`, `license`, `widevine`, `playready`, `decrypt` | `DRM_ERROR` (502) | no |
|
||||
| `drm`, `license`, `widevine`, `playready`, `decrypt` | `DRM_ERROR` (422) | no |
|
||||
| `service unavailable`, `503`, `maintenance`, `temporarily unavailable` | `SERVICE_UNAVAILABLE` (503) | yes |
|
||||
| `invalid`, `malformed`, `validation` (or `ValueError`, `ValidationError`) | `INVALID_INPUT` (400) | no |
|
||||
| anything else | `INTERNAL_ERROR` (500) | no |
|
||||
@@ -262,16 +266,16 @@ For anyone writing a consumer of the API, a durable strategy is:
|
||||
body.
|
||||
2. **Branch on `error_code`.** For structured errors, this is the stable identifier
|
||||
to switch on. Do not use `message` as the identifier.
|
||||
3. **Respect `retryable`.** When it is `true`, back off and try the request again. The
|
||||
reference client retries `429`, `500`, `502`, `503`, and `504` with exponential
|
||||
backoff. When absent, fix the request rather than repeating it.
|
||||
3. **Respect `retryable`.** When it is `true`, back off and try the request again.
|
||||
When absent, fix the request rather than repeating it.
|
||||
4. **Fall back gracefully.** For an unrecognised code, show the `message` and the
|
||||
HTTP status.
|
||||
|
||||
!!! note "Reference consumer"
|
||||
unshackle's own remote-download client treats any status of `400` or above as
|
||||
fatal, and a `200` body with `"status": "error"` and an `error_code` too: it logs
|
||||
`Server error [<error_code>]: <message>` and exits. It also wraps
|
||||
requests in a retry adapter (five attempts, backing off on `429`, `500`, `502`,
|
||||
`503`, and `504`). This is a reasonable template for your own client's error
|
||||
`Server error [<error_code>]: <message>` and exits. It retries a request once,
|
||||
after one second, only when a `502`, `503` or `504` arrives with a body that is
|
||||
not JSON: that is a page from the proxy in front of the server, not an answer
|
||||
from the server. This is a reasonable template for your own client's error
|
||||
handling.
|
||||
|
||||
@@ -441,10 +441,12 @@ The `error_code` maps to an HTTP status. The ones you will meet most often:
|
||||
| `NOT_FOUND` | 404 | Title or resource does not exist |
|
||||
| `JOB_NOT_FOUND` | 404 | No job with that `job_id` |
|
||||
| `CONFLICT` | 409 | Illegal state transition (e.g. retry a running job) |
|
||||
| `SERVICE_ERROR` | 422 | The service raised an error |
|
||||
| `RATE_LIMITED` | 429 | Upstream throttling (retryable) |
|
||||
| `INTERNAL_ERROR` | 500 | Unexpected server-side failure |
|
||||
| `SERVICE_ERROR` | 502 | The service raised an error |
|
||||
| `NETWORK_ERROR` | 503 | Connection/timeout reaching the service (retryable) |
|
||||
|
||||
The [error code catalogue](errors.md#error-code-catalogue) lists every code.
|
||||
|
||||
!!! tip "Retry politely"
|
||||
Codes marked retryable (`NETWORK_ERROR`, `RATE_LIMITED`, `SERVICE_UNAVAILABLE`) reflect transient conditions. Back off and retry rather than hammering the server.
|
||||
|
||||
@@ -288,7 +288,8 @@ run. Give `profile` and `resolver` only to use a profile that you made yourself
|
||||
[How unshackle chooses a profile](#how-unshackle-chooses-a-profile)).
|
||||
|
||||
The returned proxy is HTTP on loopback,
|
||||
`http://127.0.0.1:{port}`. The locations themselves are read from Control D at startup
|
||||
`http://127.0.0.1:{port}`. The log shows the location and a masked resolver ID, for example
|
||||
`Using ControlD Proxy (YUL - Montreal): abcd***@dns.controld.com`. The locations themselves are read from Control D at startup
|
||||
(107 of them across 68 countries, at the time of writing), so there is nothing to list:
|
||||
|
||||
| Query | Meaning |
|
||||
|
||||
@@ -267,12 +267,14 @@ def account(catalogue: None, monkeypatch: pytest.MonkeyPatch) -> Iterator[Accoun
|
||||
monkeypatch.setattr("unshackle.core.proxies.controld.requests.request", made.request)
|
||||
controld.FORWARDERS.clear()
|
||||
controld.REGIONS.clear()
|
||||
controld.LOCATIONS.clear()
|
||||
yield made
|
||||
for server in controld.FORWARDERS.values():
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
controld.FORWARDERS.clear()
|
||||
controld.REGIONS.clear()
|
||||
controld.LOCATIONS.clear()
|
||||
|
||||
|
||||
def test_locations_come_from_control_d(account: Account) -> None:
|
||||
@@ -378,6 +380,13 @@ def test_a_server_runs_a_forwarder_for_a_resolver_a_client_sends(account: Accoun
|
||||
local_proxy("controld://evil.example/x@dns.controld.com")
|
||||
|
||||
|
||||
def test_the_log_names_the_location_and_masks_the_resolver(account: Account) -> None:
|
||||
provider = ControlD(token="secret", profile="abcd123", resolver="efgh456")
|
||||
provider.get_proxy("yul")
|
||||
provider.get_proxy("yul") # a profile already on the region keeps its location
|
||||
assert provider.last_connection_display() == "(YUL - Montreal): efgh***@dns.controld.com"
|
||||
|
||||
|
||||
def test_remote_sends_the_resolver_and_a_bare_region_skips_control_d(
|
||||
account: Account, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
@@ -391,6 +400,15 @@ def test_remote_sends_the_resolver_and_a_bare_region_skips_control_d(
|
||||
remote_service.resolve_remote_proxy_arg("us") # a loopback forwarder is useless to a server
|
||||
|
||||
|
||||
def test_a_server_gets_the_typed_proxy_only_when_this_machine_resolved_it_to_loopback() -> None:
|
||||
from unshackle.core.remote_service import server_proxy_arg
|
||||
|
||||
assert server_proxy_arg({"proxy": "http://127.0.0.1:4000", "proxy_arg": "controld:ca"}) == "controld:ca"
|
||||
assert server_proxy_arg({"proxy": "https://u:p@ca1.example:89", "proxy_arg": "nordvpn:ca"}) == (
|
||||
"https://u:p@ca1.example:89"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("query", ["ca", "us:seattle", "us-ny", "uk1", "yul", "jfk", "res_yyz", "nordvpn:ca"])
|
||||
def test_a_region_query_matches(query: str) -> None:
|
||||
assert re.fullmatch(rf"(?:[a-z]+:)?{REGION}", query, re.IGNORECASE)
|
||||
|
||||
@@ -87,6 +87,12 @@ def test_export_without_manifest_url_still_rebuilds_dash_manifest_data(tmp_path:
|
||||
assert set(tracks.videos[0].data.get("dash", {})) >= {"manifest", "adaptation_set", "representation"}
|
||||
|
||||
|
||||
def test_exported_track_its_manifest_no_longer_lists_is_dropped(tmp_path: Path) -> None:
|
||||
"""A track that matches no representation would get no manifest data and fail only at download."""
|
||||
service = make_service(tmp_path, None, {"v-hevc": exported_video("v-hevc", AVC_URL, "HEVC", 3840, 2160)})
|
||||
assert service.get_tracks(next(iter(service.get_titles()))).videos == []
|
||||
|
||||
|
||||
def test_exported_hevc_on_its_own_mpd_survives_the_reparse(tmp_path: Path) -> None:
|
||||
"""A ladder split across per-codec MPDs loses every codec but manifest_url's if only URL tracks merge back."""
|
||||
service = make_service(
|
||||
|
||||
@@ -54,19 +54,20 @@ URL_SUBTITLE = {
|
||||
}
|
||||
|
||||
|
||||
def make_service(tmp_path: Path, language: Optional[str], tracks: Optional[dict[str, Any]] = None) -> ImportService:
|
||||
export = {
|
||||
"version": 2,
|
||||
"service": "EXAMPLE",
|
||||
"titles": {
|
||||
"movie-1": {
|
||||
"meta": {"type": "movie", "id": "movie-1", "name": "Example Movie", "year": 2024, "language": language},
|
||||
"manifest_url": "https://example.invalid/m.mpd",
|
||||
"manifest_type": "DASH",
|
||||
"tracks": tracks or {},
|
||||
}
|
||||
},
|
||||
def make_service(
|
||||
tmp_path: Path,
|
||||
language: Optional[str],
|
||||
tracks: Optional[dict[str, Any]] = None,
|
||||
manifest_url: Optional[str] = "https://example.invalid/m.mpd",
|
||||
) -> ImportService:
|
||||
entry: dict[str, Any] = {
|
||||
"meta": {"type": "movie", "id": "movie-1", "name": "Example Movie", "year": 2024, "language": language},
|
||||
"manifest_type": "DASH",
|
||||
"tracks": tracks or {},
|
||||
}
|
||||
if manifest_url:
|
||||
entry["manifest_url"] = manifest_url
|
||||
export = {"version": 2, "service": "EXAMPLE", "titles": {"movie-1": entry}}
|
||||
path = tmp_path / "export.json"
|
||||
path.write_text(json.dumps(export), encoding="utf8")
|
||||
return ImportService(click.Context(click.Command("dl")), "EXAMPLE", "movie-1", str(path))
|
||||
@@ -95,6 +96,35 @@ def test_unlabelled_manifest_without_usable_language_blames_the_service(
|
||||
assert "may have expired" not in error.value.message
|
||||
|
||||
|
||||
def exported_video(language: str = "ja") -> dict[str, Any]:
|
||||
return {
|
||||
"type": "Video",
|
||||
"id": "v1",
|
||||
"url": "https://example.invalid/m.mpd",
|
||||
"language": language,
|
||||
"descriptor": "DASH",
|
||||
"codec": "AVC",
|
||||
"bitrate": 5000000,
|
||||
"width": 1920,
|
||||
"height": 1080,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("manifest_url", ["https://example.invalid/m.mpd", None], ids=["manifest-url", "per-track-url"])
|
||||
def test_unlabelled_manifest_takes_the_exported_track_language(tmp_path: Path, manifest_url: Optional[str]) -> None:
|
||||
"""A title with no language takes it from its exported tracks, for either import branch."""
|
||||
service = make_service(tmp_path, language=None, tracks={"v1": exported_video()}, manifest_url=manifest_url)
|
||||
tracks = get_tracks(service, MPD_NO_LANG)
|
||||
assert [(str(t.language), t.data.get("dash") is not None) for t in tracks.videos] == [("ja", True)]
|
||||
|
||||
|
||||
def test_original_language_track_gives_the_title_language(tmp_path: Path) -> None:
|
||||
"""The exporter marks the original-language tracks, which beat a video of another language."""
|
||||
audio = {"type": "Audio", "id": "a1", "language": "de", "is_original_lang": True}
|
||||
service = make_service(tmp_path, language=None, tracks={"v1": exported_video("en"), "a1": audio})
|
||||
assert str(next(iter(service.get_titles())).language) == "de"
|
||||
|
||||
|
||||
def test_exported_url_subtitles_replace_the_manifests(tmp_path: Path) -> None:
|
||||
"""Services drop the manifest's subtitles and side-load their own; importing both duplicates them."""
|
||||
tracks = get_tracks(make_service(tmp_path, "ja", {"sideloaded-en": URL_SUBTITLE}), MPD_WITH_LANG)
|
||||
|
||||
@@ -20,7 +20,7 @@ def test_search_returns_results(http_session, server_url: str, service_case) ->
|
||||
)
|
||||
if r.status_code in (400, 401, 403):
|
||||
pytest.skip(f"{service} search not available: {r.status_code} {r.text[:200]}")
|
||||
if r.status_code == 502 and "not supported" in r.text.lower():
|
||||
if r.status_code == 422 and "not supported" in r.text.lower():
|
||||
pytest.skip(f"{service} does not implement search")
|
||||
assert r.status_code == 200, r.text
|
||||
body = r.json()
|
||||
|
||||
@@ -43,8 +43,8 @@ def test_api_error_default_http_status_per_code() -> None:
|
||||
APIErrorCode.INTERNAL_ERROR: 500,
|
||||
APIErrorCode.DOWNLOAD_ERROR: 500,
|
||||
APIErrorCode.WORKER_ERROR: 500,
|
||||
APIErrorCode.SERVICE_ERROR: 502,
|
||||
APIErrorCode.DRM_ERROR: 502,
|
||||
APIErrorCode.SERVICE_ERROR: 422,
|
||||
APIErrorCode.DRM_ERROR: 422,
|
||||
APIErrorCode.NETWORK_ERROR: 503,
|
||||
APIErrorCode.SERVICE_UNAVAILABLE: 503,
|
||||
}
|
||||
@@ -52,6 +52,8 @@ def test_api_error_default_http_status_per_code() -> None:
|
||||
assert {c.name for c in cases} == {c.name for c in APIErrorCode}
|
||||
for code, expected in cases.items():
|
||||
assert APIError(code, "x").http_status == expected, code
|
||||
# the edge proxy replaces a 502 or 504 body, so no code may default to either
|
||||
assert not {502, 504} & set(cases.values())
|
||||
|
||||
|
||||
def test_api_error_explicit_http_status_overrides_default() -> None:
|
||||
@@ -164,7 +166,7 @@ def test_handle_api_exception_categorizes_generic() -> None:
|
||||
(Exception("Not available in your region"), 403, "GEOFENCE", False),
|
||||
(Exception("Title not found"), 404, "NOT_FOUND", False),
|
||||
(Exception("HTTP 429 too many requests"), 429, "RATE_LIMITED", True),
|
||||
(Exception("DRM license fetch failed"), 502, "DRM_ERROR", False),
|
||||
(Exception("DRM license fetch failed"), 422, "DRM_ERROR", False),
|
||||
(Exception("503 service unavailable"), 503, "SERVICE_UNAVAILABLE", True),
|
||||
(ValueError("malformed body"), 400, "INVALID_INPUT", False),
|
||||
(RuntimeError("totally novel failure xyz"), 500, "INTERNAL_ERROR", False),
|
||||
|
||||
@@ -1613,6 +1613,7 @@ class dl:
|
||||
with console.status("Loading Proxy Providers...", spinner="dots"):
|
||||
self.proxy_providers = load_proxy_providers()
|
||||
|
||||
ctx.params["proxy_arg"] = proxy
|
||||
if proxy and proxy.startswith("controld://"):
|
||||
proxy = ctx.params["proxy"] = resolve_proxy(proxy, [])
|
||||
if proxy:
|
||||
@@ -1647,9 +1648,6 @@ class dl:
|
||||
ctx.params["proxy_query"] = None
|
||||
ctx.params["proxy_provider"] = None
|
||||
|
||||
if self.remote_needs_public_proxy and ctx.params.get("proxy") and is_loopback(ctx.params["proxy"]):
|
||||
raise click.UsageError("That proxy is on your machine, so --remote cannot use it.")
|
||||
|
||||
ctx.obj = ContextData(
|
||||
config=self.service_config,
|
||||
cdm=self.cdm,
|
||||
|
||||
@@ -88,6 +88,7 @@ def search(
|
||||
with console.status("Loading Proxy Providers...", spinner="dots"):
|
||||
proxy_providers = load_proxy_providers()
|
||||
|
||||
ctx.params["proxy_arg"] = proxy
|
||||
if proxy:
|
||||
try:
|
||||
requested_provider, query = split_proxy_query(proxy, proxy_providers)
|
||||
|
||||
@@ -94,10 +94,10 @@ class APIError(Exception):
|
||||
APIErrorCode.SESSION_NOT_FOUND: 404,
|
||||
APIErrorCode.TRACK_NOT_FOUND: 404,
|
||||
APIErrorCode.CONFLICT: 409,
|
||||
APIErrorCode.SERVICE_ERROR: 422,
|
||||
APIErrorCode.DRM_ERROR: 422,
|
||||
APIErrorCode.RATE_LIMITED: 429,
|
||||
APIErrorCode.INTERNAL_ERROR: 500,
|
||||
APIErrorCode.SERVICE_ERROR: 502,
|
||||
APIErrorCode.DRM_ERROR: 502,
|
||||
APIErrorCode.NETWORK_ERROR: 503,
|
||||
APIErrorCode.SERVICE_UNAVAILABLE: 503,
|
||||
APIErrorCode.DOWNLOAD_ERROR: 500,
|
||||
|
||||
@@ -5123,7 +5123,7 @@ def handle_proxy_license(
|
||||
)
|
||||
|
||||
# A service raises when the upstream licence server rejects the challenge.
|
||||
# Surface it as a structured licence error, not an uncaught 500 the edge turns into a 502.
|
||||
# Surface it as a structured licence error, not an uncaught 500.
|
||||
try:
|
||||
if drm_type == "widevine" and certificate:
|
||||
license_blob = service.get_widevine_service_certificate(
|
||||
|
||||
@@ -102,7 +102,8 @@ def resolve_import_manifest_data(
|
||||
Exports can be without ``manifest_url`` (for example, when the service never set
|
||||
``title.tracks.manifest_url``) or carry one MPD per adaptation set. ``Track.from_dict``
|
||||
does not serialise manifest XML, so each exported track's ``url`` is re-fetched and
|
||||
matched to a locally parsed representation before download.
|
||||
matched to a locally parsed representation before download. A track that matches none is
|
||||
dropped with a warning, since without its manifest data it fails only when it downloads.
|
||||
"""
|
||||
if manifest_type not in MANIFEST_DATA_KEYS:
|
||||
return
|
||||
@@ -143,6 +144,13 @@ def resolve_import_manifest_data(
|
||||
if matched and matched.data.get(data_key):
|
||||
track.data.update(matched.data)
|
||||
|
||||
unmatched = {str(track.id) for track in pending if not track.data.get(data_key)}
|
||||
for track_id in unmatched:
|
||||
log.warning(f"Skipping exported track {track_id}: its {manifest_type} manifest no longer lists it")
|
||||
tracks.select_video(lambda t: str(t.id) not in unmatched)
|
||||
tracks.select_audio(lambda t: str(t.id) not in unmatched)
|
||||
tracks.select_subtitles(lambda t: str(t.id) not in unmatched)
|
||||
|
||||
|
||||
def parse_codec(codec: type[E], parse: Callable[[str], E], value: Any) -> Optional[E]:
|
||||
"""The codec a row names by enum name (``hevc``) or as ``parse`` reads it, else None."""
|
||||
@@ -316,7 +324,7 @@ class ImportService:
|
||||
if self.titles is not None:
|
||||
return self.titles
|
||||
titles_list = [
|
||||
build_title(entry.get("meta", {}), self.service_tag, fallback_id=title_id)
|
||||
build_title(self.title_meta(entry), self.service_tag, fallback_id=title_id)
|
||||
for title_id, entry in self.titles_data.items()
|
||||
]
|
||||
self.titles = (
|
||||
@@ -324,6 +332,23 @@ class ImportService:
|
||||
)
|
||||
return self.titles
|
||||
|
||||
@staticmethod
|
||||
def title_meta(entry: dict[str, Any]) -> dict[str, Any]:
|
||||
"""The title meta, with the language of an exported track when the title has none.
|
||||
|
||||
A service can set no title language yet label its tracks. The manifest re-parse and
|
||||
``-l orig`` need a title language, so an original-language track gives it, else a video.
|
||||
"""
|
||||
meta = entry.get("meta") or {}
|
||||
if fallback_language(meta.get("language")):
|
||||
return meta
|
||||
source = min(
|
||||
(d for d in (entry.get("tracks") or {}).values() if fallback_language(d.get("language"))),
|
||||
key=lambda d: (not d.get("is_original_lang"), d.get("type") != "Video"),
|
||||
default=None,
|
||||
)
|
||||
return {**meta, "language": source["language"]} if source else meta
|
||||
|
||||
def get_titles_cached(self, title_id: Optional[str] = None) -> Titles_T:
|
||||
"""Apply the service's title_map to titles reconstructed from the export sidecar."""
|
||||
title_map = (config.services.get(self.service_tag) or {}).get("title_map") or {}
|
||||
|
||||
@@ -16,6 +16,7 @@ import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.utils.redact import REDACTED
|
||||
|
||||
log = logging.getLogger("ControlD")
|
||||
|
||||
@@ -261,6 +262,7 @@ class LocalProxy(socketserver.ThreadingTCPServer):
|
||||
# process-wide across provider instances; separate processes share only the account's unshackle-<region> names
|
||||
FORWARDERS: dict[str, LocalProxy] = {}
|
||||
REGIONS: dict[str, str] = {} # profile -> region, oldest claim first
|
||||
LOCATIONS: dict[str, str] = {} # profile -> the location code it redirects through
|
||||
STATE = threading.Lock()
|
||||
|
||||
|
||||
@@ -337,6 +339,7 @@ class ControlD(Proxy):
|
||||
self.configured = {x["profile"]: resolver_id(x["resolver"]) for x in pairs}
|
||||
self.max_profiles = max_profiles
|
||||
self.resolvers = dict(self.configured)
|
||||
self.last_profile: Optional[str] = None
|
||||
|
||||
self.catalogue = self.get_locations()
|
||||
|
||||
@@ -459,6 +462,8 @@ class ControlD(Proxy):
|
||||
server.resolver.cache.clear()
|
||||
REGIONS.pop(profile, None)
|
||||
REGIONS[profile] = query
|
||||
LOCATIONS[profile] = location
|
||||
self.last_profile = profile
|
||||
return resolver
|
||||
|
||||
def get_proxy(self, query: str) -> Optional[str]:
|
||||
@@ -467,6 +472,15 @@ class ControlD(Proxy):
|
||||
resolver = self.claim(query.lower())
|
||||
return forwarder(resolver).uri if resolver else None
|
||||
|
||||
def last_connection_display(self) -> Optional[str]:
|
||||
"""Name the location of the last claim, with the resolver ID masked, in place of the loopback URI."""
|
||||
location = LOCATIONS.get(self.last_profile or "")
|
||||
if not location:
|
||||
return None
|
||||
city = next((x.get("city") for x in self.catalogue if x["PK"] == location), None)
|
||||
label = f"{location} - {city}" if city else location
|
||||
return f"({label}): {self.resolvers[self.last_profile][:4]}{REDACTED}@dns.controld.com"
|
||||
|
||||
def get_remote_proxy(self, query: str) -> Optional[str]:
|
||||
"""Get the `controld://` proxy URI for this region, for a server on another machine."""
|
||||
with STATE:
|
||||
|
||||
@@ -920,6 +920,12 @@ def resolve_remote_proxy_arg(proxy_arg: str, providers: Optional[list] = None) -
|
||||
raise click.ClickException(str(e))
|
||||
|
||||
|
||||
def server_proxy_arg(params: Dict[str, Any]) -> str:
|
||||
"""The --proxy value to resolve for a server: the one typed when it resolved to a proxy on this machine."""
|
||||
proxy = params["proxy"]
|
||||
return (params.get("proxy_arg") or proxy) if is_loopback(proxy) else proxy
|
||||
|
||||
|
||||
_CACHE_HEX_ID_RE = re.compile(r"[0-9a-f]{32,}", re.IGNORECASE)
|
||||
|
||||
|
||||
@@ -1159,7 +1165,8 @@ class RemoteService:
|
||||
if params.get("no_proxy"):
|
||||
data["no_proxy"] = True
|
||||
elif params.get("proxy"):
|
||||
data["proxy"] = resolve_remote_proxy_arg(params["proxy"], getattr(self.ctx.obj, "proxy_providers", None))
|
||||
providers = getattr(self.ctx.obj, "proxy_providers", None)
|
||||
data["proxy"] = resolve_remote_proxy_arg(server_proxy_arg(params), providers)
|
||||
self.require_reachable_proxy(data["proxy"])
|
||||
response = self.client.post("/api/search", data)
|
||||
self.save_returned_cache(response.get("cache") or {}, profile)
|
||||
@@ -1226,7 +1233,7 @@ class RemoteService:
|
||||
"rejects the remote session"
|
||||
)
|
||||
elif not no_proxy and proxy:
|
||||
resolved_proxy = resolve_remote_proxy_arg(proxy, providers)
|
||||
resolved_proxy = resolve_remote_proxy_arg(server_proxy_arg(self.ctx.parent.params), providers)
|
||||
if resolved_proxy:
|
||||
create_data["proxy"] = resolved_proxy
|
||||
query = (self.ctx.parent.params.get("proxy_query") if self.ctx.parent else None) or proxy
|
||||
|
||||
Reference in New Issue
Block a user