forked from kenzuya/unshackle
style: turn on ruff UP and B rules and fix every finding
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from enum import Enum
|
||||
from http.cookiejar import CookieJar
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Optional, TypeVar, Union
|
||||
from typing import Any, TypeVar
|
||||
from uuid import UUID
|
||||
|
||||
import click
|
||||
@@ -36,7 +37,7 @@ PARSERS: dict[str, type[DASH] | type[HLS] | type[ISM]] = {"DASH": DASH, "HLS": H
|
||||
MANIFEST_DATA_KEYS = {"DASH": "dash", "ISM": "ism"}
|
||||
|
||||
|
||||
def fallback_language(language: Any) -> Optional[Any]:
|
||||
def fallback_language(language: Any) -> Any | None:
|
||||
"""The title language if the parsers would accept it, else None so they raise.
|
||||
|
||||
DASH drops an und/invalid fallback but ISM does not, and a truthy 'und' there silently
|
||||
@@ -48,7 +49,7 @@ def fallback_language(language: Any) -> Optional[Any]:
|
||||
return language
|
||||
|
||||
|
||||
def hls_audio_language(manifest: HLS) -> Optional[str]:
|
||||
def hls_audio_language(manifest: HLS) -> str | None:
|
||||
"""The language of the default audio rendition, else of the first audio rendition, else None.
|
||||
|
||||
An HLS master playlist gives no language for a video variant, so the parser needs a
|
||||
@@ -59,7 +60,7 @@ def hls_audio_language(manifest: HLS) -> Optional[str]:
|
||||
return str(audio[0].language) if audio else None
|
||||
|
||||
|
||||
def hls_playlist_kids(track: Any, session: Optional[requests.Session] = None) -> Optional[set[UUID]]:
|
||||
def hls_playlist_kids(track: Any, session: requests.Session | None = None) -> set[UUID] | None:
|
||||
"""Return the KIDs that the Widevine and PlayReady keys of the track's media playlist name.
|
||||
|
||||
Return None when the playlist has no key or only AES-128 keys: the download gets an AES-128
|
||||
@@ -86,7 +87,7 @@ def hls_playlist_kids(track: Any, session: Optional[requests.Session] = None) ->
|
||||
return None
|
||||
|
||||
|
||||
def content_period_filter(manifest: Any, kept: Any, title: Any) -> Optional[Callable[[Any], bool]]:
|
||||
def content_period_filter(manifest: Any, kept: Any, title: Any) -> Callable[[Any], bool] | None:
|
||||
"""A DASH period_filter that drops every period not in ``kept``, the export's ``content_periods`` for this manifest.
|
||||
|
||||
None when there is nothing to filter, or when no kept id is in the re-fetched manifest: the server can name its
|
||||
@@ -123,13 +124,13 @@ def manifest_fetch_error(manifest_type: str, title: Any, error: Exception) -> cl
|
||||
|
||||
def resolve_import_manifest_data(
|
||||
tracks: Tracks,
|
||||
manifest_type: Optional[str],
|
||||
manifest_type: str | None,
|
||||
*,
|
||||
session: requests.Session,
|
||||
language: Any,
|
||||
title: Title_T,
|
||||
saved: Optional[dict[str, Any]] = None,
|
||||
content_periods: Optional[dict[str, list[str]]] = None,
|
||||
saved: dict[str, Any] | None = None,
|
||||
content_periods: dict[str, list[str]] | None = None,
|
||||
) -> None:
|
||||
"""Fill ``track.data`` for DASH/ISM tracks rebuilt from export dicts.
|
||||
|
||||
@@ -167,11 +168,11 @@ def resolve_import_manifest_data(
|
||||
except MissingLanguageError:
|
||||
raise click.ClickException(
|
||||
f"No language for '{title}': the {manifest_type} manifest has none and Title.language is unset"
|
||||
)
|
||||
) from None
|
||||
except ValueError as e:
|
||||
raise click.ClickException(f"Failed to parse the {manifest_type} manifest for '{title}': {e}")
|
||||
raise click.ClickException(f"Failed to parse the {manifest_type} manifest for '{title}': {e}") # noqa: B904
|
||||
except Exception as e:
|
||||
raise manifest_fetch_error(manifest_type, title, e)
|
||||
raise manifest_fetch_error(manifest_type, title, e) # noqa: B904
|
||||
|
||||
local_tracks = [*parsed.videos, *parsed.audio, *parsed.subtitles]
|
||||
for track in pending:
|
||||
@@ -189,7 +190,7 @@ def resolve_import_manifest_data(
|
||||
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]:
|
||||
def parse_codec(codec: type[E], parse: Callable[[str], E], value: Any) -> E | None:
|
||||
"""The codec a row names by enum name (``hevc``) or as ``parse`` reads it, else None."""
|
||||
if not value:
|
||||
return None
|
||||
@@ -201,7 +202,7 @@ def parse_codec(codec: type[E], parse: Callable[[str], E], value: Any) -> Option
|
||||
return None
|
||||
|
||||
|
||||
def video_range(value: Any) -> Optional[Video.Range]:
|
||||
def video_range(value: Any) -> Video.Range | None:
|
||||
"""The range a row names, by enum name (``hdr10p``) or value (``HDR10+``), else None for SDR."""
|
||||
name = str(value or "").upper()
|
||||
if name in Video.Range.__members__:
|
||||
@@ -209,7 +210,7 @@ def video_range(value: Any) -> Optional[Video.Range]:
|
||||
return next((r for r in Video.Range if r.value.upper() == name), None)
|
||||
|
||||
|
||||
def int_or_none(value: Any) -> Optional[int]:
|
||||
def int_or_none(value: Any) -> int | None:
|
||||
"""The integer a row field holds, else None."""
|
||||
try:
|
||||
return int(value)
|
||||
@@ -232,14 +233,14 @@ class ImportService:
|
||||
server_cdm = True
|
||||
local_cdm = None
|
||||
|
||||
def __init__(self, ctx: click.Context, service_tag: str, title: str, import_file: Optional[str]) -> None:
|
||||
def __init__(self, ctx: click.Context, service_tag: str, title: str, import_file: str | None) -> None:
|
||||
self.__class__.__name__ = service_tag
|
||||
self.service_tag = service_tag
|
||||
self.title_id = title
|
||||
self.ctx = ctx
|
||||
self.log = logging.getLogger(service_tag)
|
||||
self.credential: Optional[Credential] = None
|
||||
self.current_region: Optional[str] = None
|
||||
self.credential: Credential | None = None
|
||||
self.current_region: str | None = None
|
||||
self.title_cache = None
|
||||
|
||||
if not import_file:
|
||||
@@ -251,15 +252,15 @@ class ImportService:
|
||||
try:
|
||||
self.doc = mediaexport.read(export_path)
|
||||
except mediaexport.ExportError as e:
|
||||
raise click.ClickException(f"Cannot import {export_path.name}: {e}")
|
||||
raise click.ClickException(f"Cannot import {export_path.name}: {e}") from e
|
||||
for refused in self.doc.refused:
|
||||
self.log.warning(f"Skipping exported title {refused.raw.get('id')!r}: {refused.reason}")
|
||||
if not self.doc.titles:
|
||||
raise click.ClickException(f"Cannot import {export_path.name}: it has no title unshackle can use")
|
||||
|
||||
self.titles_data: dict[str, Any] = {e.id: self.legacy_entry(e) for e in self.doc.titles}
|
||||
self.region: Optional[str] = self.doc.region or None
|
||||
self.titles: Optional[Titles_T] = None
|
||||
self.region: str | None = self.doc.region or None
|
||||
self.titles: Titles_T | None = None
|
||||
self.tracks_by_title: dict[str, Tracks] = {}
|
||||
|
||||
self.server_cdm_type = "playready" if self.exported_drm_system() == "playready" else "widevine"
|
||||
@@ -312,7 +313,7 @@ class ImportService:
|
||||
|
||||
@staticmethod
|
||||
def build_session(
|
||||
ctx: click.Context, region: Optional[str] = None, headers: Optional[dict[str, str]] = None
|
||||
ctx: click.Context, region: str | None = None, headers: dict[str, str] | None = None
|
||||
) -> requests.Session:
|
||||
"""HTTP session for re-fetching the manifest.
|
||||
|
||||
@@ -338,7 +339,7 @@ class ImportService:
|
||||
proxy = resolve_proxy_arg(proxy_query)
|
||||
except Exception as e:
|
||||
if explicit:
|
||||
raise click.ClickException(f"Failed to resolve proxy '{proxy_query}': {e}")
|
||||
raise click.ClickException(f"Failed to resolve proxy '{proxy_query}': {e}") from None
|
||||
log.warning(f"No proxy for export region '{region}', continuing without one: {e}")
|
||||
proxy = None
|
||||
|
||||
@@ -353,7 +354,7 @@ class ImportService:
|
||||
def title(self) -> str:
|
||||
return self.title_id
|
||||
|
||||
def authenticate(self, cookies: Optional[CookieJar] = None, credential: Optional[Credential] = None) -> None:
|
||||
def authenticate(self, cookies: CookieJar | None = None, credential: Credential | None = None) -> None:
|
||||
self.credential = credential
|
||||
|
||||
def get_titles(self) -> Titles_T:
|
||||
@@ -385,7 +386,7 @@ class ImportService:
|
||||
)
|
||||
return {**meta, "language": source["language"]} if source else meta
|
||||
|
||||
def get_titles_cached(self, title_id: Optional[str] = None) -> Titles_T:
|
||||
def get_titles_cached(self, title_id: str | None = None) -> Titles_T:
|
||||
"""Apply the service's title_map to titles reconstructed from the export sidecar."""
|
||||
title_map = (ci_get(config.services, self.service_tag) or {}).get("title_map") or {}
|
||||
return remap_titles(self.get_titles(), title_map)
|
||||
@@ -425,12 +426,12 @@ class ImportService:
|
||||
try:
|
||||
parsed = snapshot.parse(entry.get("snapshot"), str(manifest_url), language)
|
||||
except MissingLanguageError:
|
||||
raise click.ClickException(no_language)
|
||||
raise click.ClickException(no_language) from None
|
||||
if parsed is None:
|
||||
try:
|
||||
manifest = parser.from_url(url=manifest_url, session=self.session)
|
||||
except Exception as e:
|
||||
raise manifest_fetch_error(manifest_type, title, e)
|
||||
raise manifest_fetch_error(manifest_type, title, e) # noqa: B904
|
||||
if language is None and isinstance(manifest, HLS):
|
||||
language = hls_audio_language(manifest)
|
||||
if language is None:
|
||||
@@ -445,9 +446,11 @@ class ImportService:
|
||||
else manifest.to_tracks(language=language)
|
||||
)
|
||||
except MissingLanguageError:
|
||||
raise click.ClickException(no_language)
|
||||
raise click.ClickException(no_language) from None
|
||||
except Exception as e:
|
||||
raise click.ClickException(f"Failed to parse the {manifest_type} manifest for '{title}': {e}")
|
||||
raise click.ClickException( # noqa: B904
|
||||
f"Failed to parse the {manifest_type} manifest for '{title}': {e}"
|
||||
)
|
||||
for parsed_track in parsed:
|
||||
tracks.add(parsed_track)
|
||||
parsed_ids = {str(t.id) for t in tracks}
|
||||
@@ -560,7 +563,7 @@ class ImportService:
|
||||
)
|
||||
)
|
||||
continue
|
||||
track: Union[Video, Audio]
|
||||
track: Video | Audio
|
||||
try:
|
||||
if kind == "video":
|
||||
track = Video(
|
||||
@@ -593,7 +596,7 @@ class ImportService:
|
||||
out.append(track)
|
||||
return out
|
||||
|
||||
def key_pool(self, title_id: Optional[str] = None) -> dict[UUID, str]:
|
||||
def key_pool(self, title_id: str | None = None) -> dict[UUID, str]:
|
||||
"""Exported KID:KEY pairs as {UUID: key_hex}, for one title or across every title."""
|
||||
entry = self.doc.get(title_id) if title_id else None
|
||||
keys = entry.keys if entry else self.doc.key_pool()
|
||||
@@ -618,7 +621,7 @@ class ImportService:
|
||||
out.append({"system": system, "pssh_b64": d.pssh, "kids": list(entry.keys)})
|
||||
return out
|
||||
|
||||
def exported_kids(self, title_id: str, track_id: str) -> Optional[set[str]]:
|
||||
def exported_kids(self, title_id: str, track_id: str) -> set[str] | None:
|
||||
"""Return the KIDs (hex) the export's row for the track lists, or None when it lists none."""
|
||||
entry = self.doc.get(title_id)
|
||||
row = next((r for r in (entry.tracks if entry else []) if str(r.get("id", "")) == track_id), None)
|
||||
@@ -634,7 +637,7 @@ class ImportService:
|
||||
entry = self.doc.get(title_id)
|
||||
return {UUID(hex=kid): key for kid, key in (entry.keys_for(track_id) if entry else {}).items()}
|
||||
|
||||
def rebuild_drm(self, track_dict: dict[str, Any], title_id: str) -> Optional[list[Any]]:
|
||||
def rebuild_drm(self, track_dict: dict[str, Any], title_id: str) -> list[Any] | None:
|
||||
"""Rebuild a DRM object (from stored PSSH, falling back to a stub) with the exported keys.
|
||||
|
||||
Keys and DRM are title-level in the shared format; a legacy v2 track dict that still
|
||||
@@ -692,7 +695,7 @@ class ImportService:
|
||||
"""Always False: an import licenses nothing."""
|
||||
return False
|
||||
|
||||
def license_track(self, track: AnyTrack, drm: Any, kid: Optional[UUID] = None) -> Optional[dict[UUID, str]]:
|
||||
def license_track(self, track: AnyTrack, drm: Any, kid: UUID | None = None) -> dict[UUID, str] | None:
|
||||
"""Raise the error that names the KIDs of ``track`` neither the export nor a vault holds."""
|
||||
raise self.missing_key(track)
|
||||
|
||||
@@ -729,7 +732,7 @@ class ImportService:
|
||||
continue
|
||||
row_kids = self.exported_kids(title_id, str(track.id))
|
||||
if row_kids:
|
||||
declared: Optional[set[UUID]] = {UUID(hex=kid) for kid in row_kids}
|
||||
declared: set[UUID] | None = {UUID(hex=kid) for kid in row_kids}
|
||||
keys = self.exported_keys(title_id, str(track.id))
|
||||
else:
|
||||
declared = self.track_kids(track, self.session)
|
||||
@@ -749,12 +752,12 @@ class ImportService:
|
||||
self.server_cdm_type = drm_obj.__class__.__name__.lower()
|
||||
|
||||
@classmethod
|
||||
def track_is_encrypted(cls, track: Any, session: Optional[requests.Session] = None) -> bool:
|
||||
def track_is_encrypted(cls, track: Any, session: requests.Session | None = None) -> bool:
|
||||
"""True if the track carries DRM, its manifest declares protection, or its init segment does."""
|
||||
return cls.track_kids(track, session) is not None
|
||||
|
||||
@staticmethod
|
||||
def track_kids(track: Any, session: Optional[requests.Session] = None) -> Optional[set[UUID]]:
|
||||
def track_kids(track: Any, session: requests.Session | None = None) -> set[UUID] | None:
|
||||
"""Return the KIDs an encrypted track declares, or None when the track is not encrypted.
|
||||
|
||||
An empty set means that the track is encrypted but declares no KID. The KIDs come from
|
||||
@@ -820,12 +823,10 @@ class ImportService:
|
||||
]
|
||||
)
|
||||
|
||||
def get_widevine_service_certificate(self, **_: Any) -> Optional[str]:
|
||||
def get_widevine_service_certificate(self, **_: Any) -> str | None:
|
||||
return None
|
||||
|
||||
def get_clearkey_license(
|
||||
self, *, challenge: bytes, title: Title_T, track: AnyTrack
|
||||
) -> Optional[Union[bytes, str, dict]]:
|
||||
def get_clearkey_license(self, *, challenge: bytes, title: Title_T, track: AnyTrack) -> bytes | str | dict | None:
|
||||
raise self.missing_key(track)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user