refactor(core): move the title, track and DRM wire form into core/wire.py

This commit is contained in:
imSp4rky
2026-11-24 14:37:51 -07:00
parent e385234a92
commit 55138f95c2
36 changed files with 2250 additions and 907 deletions
+1 -2
View File
@@ -12,13 +12,12 @@ import pytest
import requests
from Cryptodome.Cipher import Blowfish
from unshackle.core.api.handlers import serialize_audio_track, serialize_drm
from unshackle.core.config import config
from unshackle.core.constants import DOWNLOAD_CANCELLED
from unshackle.core.drm import BlowfishStripe
from unshackle.core.remote_service import build_tracks, reconstruct_drm
from unshackle.core.tracks import Audio, Track
from unshackle.core.tracks import track as track_module
from unshackle.core.wire import build_tracks, reconstruct_drm, serialize_audio_track, serialize_drm
KEY = bytes.fromhex("00112233445566778899aabbccddeeff")
CHUNK = BlowfishStripe.CHUNK
+1 -1
View File
@@ -20,9 +20,9 @@ import pytest
# lxml.etree: XML parser used to build ContentProtection fixtures for DASH.get_drm
from lxml import etree
from unshackle.core.drm import drm_from_dict
from unshackle.core.drm.clearkey_cenc import ClearKeyCENC
from unshackle.core.manifests.dash import DASH
from unshackle.core.wire import drm_from_dict
KID = UUID("9eb4050d-e44b-4802-932e-27d75083e266")
KEY = bytes.fromhex("ccd0064c43f7e9fcbaa9b12af3fd1f40")
+1 -1
View File
@@ -5,8 +5,8 @@ from __future__ import annotations
from types import SimpleNamespace
from unshackle.commands.dl import dl
from unshackle.core.remote_service import apply_title_fields
from unshackle.core.titles.episode import Episode, Series
from unshackle.core.wire import apply_title_fields
class DummyService:
+1 -2
View File
@@ -7,10 +7,9 @@ from datetime import date
import pytest
from unshackle.commands.dl import dl
from unshackle.core.api.handlers import serialize_title
from unshackle.core.config import Config, config
from unshackle.core.remote_service import build_title
from unshackle.core.titles.episode import Episode
from unshackle.core.wire import build_title, serialize_title
SERIES = "{title}.{year?}.{season_episode}.{episode_name?}.{quality?}-{tag?}"
SPORTS = "{title}.{year?}.{date?}.{episode_name}.{quality?}-{tag?}"
+1 -1
View File
@@ -19,12 +19,12 @@ import click
import pytest
from unshackle.commands.dl import dl
from unshackle.core.drm import drm_from_dict
from unshackle.core.drm.clearkey import ClearKey
from unshackle.core.drm.clearkey_cenc import ClearKeyCENC
from unshackle.core.import_service import ImportService
from unshackle.core.titles import Movie
from unshackle.core.tracks import Audio, Chapter, Subtitle, Video
from unshackle.core.wire import drm_from_dict
KID = UUID(hex="00000000000000000000000000000001")
+3 -2
View File
@@ -13,6 +13,7 @@ from unshackle.core.remote_service import RemoteService
from unshackle.core.tracks import Audio, Video
from unshackle.core.utils import hdr10plus
from unshackle.core.utils.hdr10plus import HDR10PLUS_T35, TS_PACKET, fragment_has_t35
from unshackle.core.wire import track_from_dict, track_to_dict
def box(kind: bytes, body: bytes) -> bytes:
@@ -127,7 +128,7 @@ def test_a_dv_track_with_hdr10plus_lists_as_dv_plus_hdr10p() -> None:
assert "[H.265, DV]" in str(video)
video.hdr10plus = True
assert "[H.265, DV+HDR10P]" in str(video)
assert Video.from_dict(video.to_dict()).hdr10plus
assert track_from_dict(track_to_dict(video)).hdr10plus
def test_on_start_runs_only_when_there_is_a_track_to_probe(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -229,7 +230,7 @@ def test_a_quoted_dl_workers_value_works_like_the_cli(monkeypatch: pytest.Monkey
def test_a_dv_track_with_an_hdr10_base_lists_as_dv_plus_hdr10() -> None:
video = Video(url="https://x/v.mp4", language="en", codec=Video.Codec.HEVC, range_=Video.Range.DV, hdr10_base=True)
assert "[H.265, DV+HDR10]" in str(video)
assert Video.from_dict(video.to_dict()).hdr10_base
assert track_from_dict(track_to_dict(video)).hdr10_base
def ts_packet(pid: int, section: bytes) -> bytes:
+1 -1
View File
@@ -3,9 +3,9 @@ from types import SimpleNamespace
from langcodes import Language
from unshackle.commands.dl import apply_enrich, load_tracks
from unshackle.core.remote_service import apply_title_fields
from unshackle.core.titles import Episode, Movie, Song
from unshackle.core.tracks import Chapters, Tracks
from unshackle.core.wire import apply_title_fields
SERVICE = type("SVC", (), {})
+4 -3
View File
@@ -16,6 +16,7 @@ from unshackle.core.manifests import snapshot as snapshot_module
from unshackle.core.manifests.snapshot import capture, parse, restore
from unshackle.core.tracks import Tracks
from unshackle.core.tracks.track import Track
from unshackle.core.wire import track_from_dict, track_to_dict
URL = "https://cdn.example/title/manifest.mpd"
@@ -116,7 +117,7 @@ def round_trip(tracks: Tracks, level: int = -1) -> Tracks:
assert {"manifests", "track_manifests"} <= set(snapshot)
client = Tracks()
for track in tracks:
client.add(Track.from_dict(track.to_dict()))
client.add(track_from_dict(track_to_dict(track)))
restore(client, snapshot)
return client
@@ -174,7 +175,7 @@ def test_a_filtered_period_that_reuses_a_kept_id_keeps_the_content():
@pytest.mark.parametrize("snapshot", ["garbage", {"manifests": "x"}, {"manifests": [1]}, {"track_manifests": {"a": 1}}])
def test_a_malformed_snapshot_restores_nothing(snapshot):
client = Tracks()
client.add(Track.from_dict(service_tracks().videos[0].to_dict()))
client.add(track_from_dict(track_to_dict(service_tracks().videos[0])))
restore(client, snapshot)
assert not client.videos[0].data.get("dash")
assert parse(snapshot, URL, "en") is None
@@ -193,7 +194,7 @@ def test_the_inflate_budget_covers_every_entry_of_one_restore(monkeypatch, caplo
assert parse(saved, URL, "en") is not None
client = Tracks()
client.add(Track.from_dict(service_tracks().videos[0].to_dict()))
client.add(track_from_dict(track_to_dict(service_tracks().videos[0])))
with caplog.at_level(logging.WARNING, logger="snapshot"):
restore(client, saved)
assert client.videos[0].data.get("dash")
+2 -1
View File
@@ -16,10 +16,11 @@ from pyplayready.system.wrmheader import WRMHeader
from pywidevine.pssh import PSSH as WV_PSSH
from unshackle.core.api.handlers import pssh_kids
from unshackle.core.drm import PlayReady, Widevine, drm_from_dict
from unshackle.core.drm import PlayReady, Widevine
from unshackle.core.manifests.dash import DASH
from unshackle.core.manifests.hls import HLS
from unshackle.core.manifests.ism import ISM
from unshackle.core.wire import drm_from_dict
KID = UUID("00112233-4455-6677-8899-aabbccddeeff")
KID_B64 = base64.b64encode(KID.bytes_le).decode()
+2 -1
View File
@@ -20,6 +20,7 @@ from unshackle.core.titles.movie import Movie
from unshackle.core.tracks import Audio, Chapter, Chapters, Track, Tracks, Video
from unshackle.core.tracks import tracks as tracks_module
from unshackle.core.tracks.tracks import pick_video
from unshackle.core.wire import track_from_dict, track_to_dict
VIDEO_IDS = itertools.count()
@@ -59,7 +60,7 @@ def test_hls_marks_stereo_tracks_from_the_layout_or_the_codec() -> None:
def test_a_stereo_track_lists_as_3d_and_keeps_the_flag_through_to_dict() -> None:
video = Video(url="https://x/v.mp4", language="en", codec=Video.Codec.HEVC, range_=Video.Range.DV, stereo=True)
assert str(video).startswith("VID | [H.265, DV, 3D] |")
assert Video.from_dict(video.to_dict()).stereo
assert track_from_dict(track_to_dict(video)).stereo
def stereo_video(tmp_path: Path, stereo: bool = True, **kwargs: Any) -> Video:
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -8,8 +8,9 @@ import pytest
from pyplayready.system.pssh import PSSH as PlayReadyPSSH
from pywidevine.pssh import PSSH as WidevinePSSH
from unshackle.core.drm import PlayReady, Widevine, drm_from_dict, key_args
from unshackle.core.drm import PlayReady, Widevine, key_args
from unshackle.core.drm.key_args import ZERO_KID
from unshackle.core.wire import drm_from_dict
VIDEO_KID = UUID("00112233-4455-6677-8899-aabbccddeeff")
AUDIO_KID = UUID("ffeeddcc-bbaa-9988-7766-554433221100")
@@ -4,7 +4,7 @@ from typing import Optional
import pytest
from unshackle.core.api.handlers import original_audio_ids
from unshackle.core.wire import original_audio_ids
class FakeTrack:
+1 -2
View File
@@ -2,10 +2,9 @@
import pytest
from unshackle.core.api.handlers import serialize_title
from unshackle.core.remote_service import build_title
from unshackle.core.titles.availability import availability_metadata
from unshackle.core.titles.episode import Episode
from unshackle.core.wire import build_title, serialize_title
pytestmark = pytest.mark.unit
+1 -3
View File
@@ -16,8 +16,8 @@ import pytest
from unshackle.core.api import handlers
from unshackle.core.api.errors import APIError
from unshackle.core.api.handlers import serialize_title
from unshackle.core.titles.episode import Episode
from unshackle.core.wire import build_title, serialize_title
pytestmark = pytest.mark.unit
@@ -116,8 +116,6 @@ def test_part_stays_out_of_the_name_field() -> None:
def test_remote_build_title_round_trip() -> None:
from unshackle.core.remote_service import build_title
rebuilt = build_title(serialize_title(ep(1, 4, 2)), "FAKE", "fallback-id")
assert (rebuilt.season, rebuilt.number, rebuilt.part, rebuilt.name) == (1, 4, 2, "Pilot")
+3 -3
View File
@@ -1,7 +1,7 @@
"""The title fields that cross the remote wire: air_date, absolute, anime and daily.
Covers serialize_title's conditional keys, the service-class ANIME/DAILY stamp, and the
serialize_title / title_to_meta round trips through remote_service.build_title.
serialize_title / title_to_meta round trips through wire.build_title.
"""
from __future__ import annotations
@@ -16,11 +16,11 @@ from langcodes import Language
from unshackle.commands.dl import dl
from unshackle.core.api import handlers
from unshackle.core.api.handlers import serialize_title, stamp_service_flags
from unshackle.core.remote_service import build_title
from unshackle.core.api.handlers import stamp_service_flags
from unshackle.core.titles.episode import Episode
from unshackle.core.titles.movie import Movie
from unshackle.core.titles.music import Song
from unshackle.core.wire import build_title, serialize_title
pytestmark = pytest.mark.unit
+7 -5
View File
@@ -14,11 +14,6 @@ from unshackle.core.api.handlers import (
listed_titles,
sanitize_log,
search_handler,
serialize_audio_track,
serialize_drm,
serialize_subtitle_track,
serialize_title,
serialize_video_track,
validate_download_parameters,
validate_service,
)
@@ -27,6 +22,13 @@ from unshackle.core.titles.episode import Episode
from unshackle.core.titles.movie import Movie
from unshackle.core.tracks import Audio, Subtitle, Video
from unshackle.core.tracks.track import Track
from unshackle.core.wire import (
serialize_audio_track,
serialize_drm,
serialize_subtitle_track,
serialize_title,
serialize_video_track,
)
pytestmark = pytest.mark.unit
@@ -6,10 +6,12 @@ from enum import Enum
import pytest
from unshackle.core.api.handlers import serialize_audio_track, serialize_subtitle_track, serialize_video_track
from unshackle.core.manifests.snapshot import match_track
from unshackle.core.remote_service import (
RemoteService,
from unshackle.core.remote_service import RemoteService
from unshackle.core.titles.episode import Episode
from unshackle.core.titles.movie import Movie
from unshackle.core.tracks import Audio, Subtitle, Video
from unshackle.core.wire import (
build_title,
build_tracks,
deserialize_audio,
@@ -17,10 +19,12 @@ from unshackle.core.remote_service import (
deserialize_video,
enum_get,
reconstruct_drm,
serialize_audio_track,
serialize_subtitle_track,
serialize_video_track,
track_from_dict,
track_to_dict,
)
from unshackle.core.titles.episode import Episode
from unshackle.core.titles.movie import Movie
from unshackle.core.tracks import Audio, Subtitle, Video
pytestmark = pytest.mark.unit
@@ -64,14 +68,12 @@ def test_a_stereo_track_stays_3d_over_remote() -> None:
def test_a_track_note_shows_over_remote_and_export_and_is_not_an_edition() -> None:
from unshackle.core.tracks.track import Track
v = Video(url="https://x/v.mp4", language="ja", codec=Video.Codec.AVC, note="h264mpl40-dash")
a = Audio(url="https://x/a.mp4", language="ja", codec=Audio.Codec.AAC, note="heaac-2-dash")
s = Subtitle(url="https://x/s.vtt", language="en", codec=Subtitle.Codec.WebVTT, note="webvtt-lssdh")
copies = [deserialize_video(serialize_video_track(v)), deserialize_audio(serialize_audio_track(a))]
copies += [deserialize_subtitle(serialize_subtitle_track(s))]
copies += [Track.from_dict(t.to_dict()) for t in (v, a, s)]
copies += [track_from_dict(track_to_dict(t)) for t in (v, a, s)]
for copy in copies:
assert copy.note and str(copy).endswith(f"| {copy.note}")
assert copy.edition == []
@@ -299,7 +299,7 @@ def test_reconstruct_drm_keeps_the_default_kid_of_a_pssh_without_kids():
from pywidevine.pssh import PSSH
from unshackle.core.drm import real_kids
from unshackle.core.remote_service import reconstruct_drm
from unshackle.core.wire import reconstruct_drm
content_id_only = PSSH.new(system_id=PSSH.SystemId.Widevine, init_data=b"\x22\x03abc")
(drm,) = reconstruct_drm([{"type": "widevine", "pssh": content_id_only.dumps(), "kids": [str(KID)]}])
+2 -1
View File
@@ -11,9 +11,10 @@ import requests
import responses
from langcodes import Language
from unshackle.core.remote_service import RemoteClient, RemoteError, RemoteService, build_title, quiet
from unshackle.core.remote_service import RemoteClient, RemoteError, RemoteService, quiet
from unshackle.core.tracks import Audio
from unshackle.core.tracks.track import Track
from unshackle.core.wire import build_title
pytestmark = pytest.mark.unit
+2 -1
View File
@@ -10,6 +10,7 @@ from __future__ import annotations
import pytest
from unshackle.core.tracks import Audio
from unshackle.core.wire import track_from_dict, track_to_dict
@pytest.mark.parametrize("layout", ["5.1.4", "7.1.2", "9.1.6"])
@@ -48,7 +49,7 @@ def test_a_value_that_is_no_layout_still_raises(value: str) -> None:
def test_an_immersive_layout_round_trips_as_a_dict() -> None:
"""A remote download rebuilds every track from the dict the server sent."""
track = Audio(id_="a1", url="https://example.test/a", language="en", channels="5.1.4", bitrate=448000)
assert Audio.from_dict(track.to_dict()).channels == "5.1.4"
assert track_from_dict(track_to_dict(track)).channels == "5.1.4"
@pytest.mark.parametrize(