Compare commits

...
4 Commits
Author SHA1 Message Date
imSp4rky 42d7fd8303 fix(import): scope the merge-back and detect ISM protection
Only merge back tracks from another manifest, so a drifted id no longer duplicates a rung or wipes the main manifest's own subtitles. track_is_encrypted now recognises an ISM ProtectionHeader.
2026-07-29 10:48:34 -06:00
sp4rk.y cebfd7bc80 Merge pull request #145 from ApeDevOne/dev
fix(import): restore missing DASH manifest data and HEVC tracks
2026-07-29 10:13:09 -06:00
imSp4rky bcf0815cf4 fix(tracks): size the no-proxy download session's connection pool 2026-07-29 08:44:27 -06:00
RocketBoy bece8fb746 Implement manifest data resolution for DASH/ISM tracks along with Import Fix for json exports with no "manifest_url"
Added a new function to resolve import manifest data for DASH/ISM tracks, enhancing the handling of track data population from export dictionaries.
2026-07-29 18:23:02 +05:30
4 changed files with 527 additions and 11 deletions
+184
View File
@@ -0,0 +1,184 @@
"""Export/import round trips for the two manifest types that are not DASH.
``get_tracks`` splits on manifest type, and the two halves need opposite things. ISM takes the
re-parse path and needs ``track.data["ism"]["segments"]`` restored, because
``ISM.download_track`` reads it. HLS takes the rebuild-from-dicts path and needs nothing
restored, because ``HLS.download_track`` re-fetches the variant from ``track.url``. That makes
the stored per-track URL load-bearing for HLS in a way it is not for ISM.
These drive the real ``dl.write_export`` rather than hand-written export dicts, so a change to
the export format fails here instead of silently invalidating the fixtures.
"""
from __future__ import annotations
import json
from pathlib import Path
from types import SimpleNamespace
import pytest
from unshackle.commands.dl import dl
from unshackle.core.import_service import ImportService
from unshackle.core.manifests import HLS, ISM
ISM_URL = "https://cdn.example/x.ism/manifest"
ISM_MANIFEST = """<?xml version="1.0"?>
<SmoothStreamingMedia MajorVersion="2" MinorVersion="0" Duration="60000000" TimeScale="10000000">
<StreamIndex Type="video" Name="video" Language="en"
Url="QualityLevels({Bitrate})/Fragments(video={start_time})">
<QualityLevel Index="0" Bitrate="3000000" FourCC="H264" MaxWidth="1280" MaxHeight="720"
CodecPrivateData="00000001674D401F"/>
<c t="0" d="20000000" r="3"/>
</StreamIndex>
<StreamIndex Type="audio" Name="audio" Language="en"
Url="QualityLevels({Bitrate})/Fragments(audio={start_time})">
<QualityLevel Index="0" Bitrate="128000" AudioTag="255" SamplingRate="48000"
Channels="2" BitsPerSample="16" CodecPrivateData="1190"/>
<c t="0" d="20000000" r="3"/>
</StreamIndex>
</SmoothStreamingMedia>
"""
HLS_URL = "https://cdn.example/master.m3u8"
HLS_MANIFEST = """#EXTM3U
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aud",LANGUAGE="en",NAME="English",DEFAULT=YES,URI="audio_en.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="sub",LANGUAGE="en",NAME="English",URI="sub_en.m3u8"
#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2",AUDIO="aud",SUBTITLES="sub"
720.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=6000000,RESOLUTION=1920x1080,CODECS="avc1.640028,mp4a.40.2",AUDIO="aud",SUBTITLES="sub"
1080.m3u8
"""
def export_tracks(tmp_path: Path, tracks, manifest_url: str | None) -> Path:
"""Write a real export sidecar for a parsed ladder, as a finished download would."""
from unshackle.core.titles import Movie
class StubService:
pass
title = Movie(id_="movie-1", service=StubService, name="Example Movie", year=2024, language="en")
for track in [*tracks.videos, *tracks.audio, *tracks.subtitles]:
title.tracks.add(track)
title.tracks.manifest_url = manifest_url
runner = dl.__new__(dl)
runner.service = "EXAMPLE"
export = tmp_path / "export.json"
for track in [*title.tracks.videos, *title.tracks.audio, *title.tracks.subtitles]:
runner.write_export(export, title, track)
return export
def import_tracks(export: Path):
svc = ImportService(SimpleNamespace(parent=None, params={}), "EXAMPLE", "movie-1", str(export))
return svc, svc.get_tracks(next(iter(svc.get_titles())))
@pytest.fixture
def ism_offline(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(ISM, "from_url", lambda url, session=None, **kw: ISM.from_text(ISM_MANIFEST, url))
@pytest.mark.parametrize("manifest_url", [ISM_URL, None], ids=["with-manifest-url", "without-manifest-url"])
def test_ism_round_trip_restores_segment_urls(tmp_path: Path, ism_offline: None, manifest_url: str | None) -> None:
"""ISM.download_track reads data["ism"]["segments"], which to_dict cannot carry across an export.
Without a manifest_url the export's own track URLs are the only way back to the manifest,
which is the case the re-parse branch alone never covered.
"""
original = ISM.from_text(ISM_MANIFEST, ISM_URL).to_tracks(language="en")
expected = {t.id: t.data["ism"]["segments"] for t in [*original.videos, *original.audio]}
_, tracks = import_tracks(export_tracks(tmp_path, original, manifest_url))
rebuilt = {t.id: t.data.get("ism", {}).get("segments") for t in [*tracks.videos, *tracks.audio]}
assert rebuilt == expected, "every ISM track must come back with the fragment URLs it was exported with"
ISM_ENCRYPTED = """<?xml version="1.0"?>
<SmoothStreamingMedia MajorVersion="2" MinorVersion="0" Duration="60000000" TimeScale="10000000">
<Protection><ProtectionHeader SystemID="{9A04F079-9840-4286-AB92-E65BE0885F95}">dGVzdA==</ProtectionHeader></Protection>
<StreamIndex Type="video" Name="video" Language="en" Url="QualityLevels({Bitrate})/Fragments(video={start_time})">
<QualityLevel Index="0" Bitrate="3000000" FourCC="H264" MaxWidth="1280" MaxHeight="720"
CodecPrivateData="00000001674D401F"/>
<QualityLevel Index="1" Bitrate="6000000" FourCC="H264" MaxWidth="1920" MaxHeight="1080"
CodecPrivateData="00000001674D401F"/>
<c t="0" d="20000000" r="3"/>
</StreamIndex>
</SmoothStreamingMedia>
"""
def test_encrypted_ism_rung_gets_the_exported_keys(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""resolve_server_keys must recognise an ISM manifest's protection, not only a DASH one.
ISM.download_track reads only ``track.drm``, so a rung that misses key injection downloads
encrypted and muxes without error. DASH survives the same omission because it re-derives its
DRM from the manifest elements at download time.
"""
monkeypatch.setattr(ISM, "from_url", lambda url, session=None, **kw: ISM.from_text(ISM_ENCRYPTED, url))
parsed = ISM.from_text(ISM_ENCRYPTED, ISM_URL).to_tracks(language="en")
# export the licensed rung only, as a finished download would
licensed = max(parsed.videos, key=lambda t: t.bitrate or 0)
export = export_tracks(tmp_path, parsed, ISM_URL)
doc = json.loads(export.read_text(encoding="utf8"))
doc["titles"]["movie-1"]["tracks"][licensed.id]["keys"] = {"0" * 32: "ab" * 16}
export.write_text(json.dumps(doc), encoding="utf8")
svc, tracks = import_tracks(export)
movie = next(iter(svc.get_titles()))
movie.tracks = tracks
svc.resolve_server_keys(movie)
other = min(tracks.videos, key=lambda t: t.bitrate or 0)
assert svc.track_is_encrypted(other), "an ISM ProtectionHeader means the rung is encrypted"
assert other.drm and other.drm[0].content_keys, "the unlicensed rung never received the exported key"
def test_hls_round_trip_keeps_every_variant_url(tmp_path: Path) -> None:
"""HLS re-fetches each variant from track.url at download time, so the stored URL has to survive."""
original = HLS.from_text(HLS_MANIFEST, HLS_URL).to_tracks(language="en")
expected = {t.id: str(t.url) for t in [*original.videos, *original.audio, *original.subtitles]}
_, tracks = import_tracks(export_tracks(tmp_path, original, HLS_URL))
rebuilt = {t.id: str(t.url) for t in [*tracks.videos, *tracks.audio, *tracks.subtitles]}
assert rebuilt == expected, "the whole exported ladder must survive, each with its own variant URL"
assert {t.descriptor.name for t in tracks} == {"HLS"}
def test_hls_import_never_refetches_the_master_playlist(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A master playlist can hand out a fresh token per fetch, so import must not depend on it.
This is why HLS is absent from MANIFEST_DATA_KEYS. Re-fetching would add nothing and give a
long-expired export another way to fail.
"""
original = HLS.from_text(HLS_MANIFEST, HLS_URL).to_tracks(language="en")
export = export_tracks(tmp_path, original, HLS_URL)
def explode(*args: object, **kwargs: object) -> None:
raise AssertionError("import re-fetched an HLS manifest")
monkeypatch.setattr(HLS, "from_url", explode)
monkeypatch.setattr(HLS, "from_text", explode)
_, tracks = import_tracks(export)
assert len(list(tracks)) == 4
def test_hls_tracks_tolerate_the_empty_manifest_data(tmp_path: Path) -> None:
"""HLS.download_track assigns into track.data["hls"] without creating it first.
Track.data is a defaultdict, so the assignment auto-vivifies rather than raising. Pin that,
because a plain dict here would turn every HLS import into a KeyError mid-download.
"""
original = HLS.from_text(HLS_MANIFEST, HLS_URL).to_tracks(language="en")
_, tracks = import_tracks(export_tracks(tmp_path, original, HLS_URL))
track = tracks.videos[0]
assert not track.data.get("hls"), "to_dict cannot carry the m3u8 objects, and does not need to"
track.data["hls"]["segment_durations"] = [1, 2, 3] # hls.py, in download_track
assert track.data["hls"]["segment_durations"] == [1, 2, 3]
+226
View File
@@ -0,0 +1,226 @@
"""Tests for the manifest data an import must rebuild before a DASH track can download.
``Track.to_dict`` cannot serialise manifest XML, so a rebuilt DASH track carries no
``track.data["dash"]``. ``DASH.download_track`` reads that mapping unconditionally, so any
DASH track the import hands back without it raises on the first segment. Two exports reach
that state: one with no top-level ``manifest_url``, and one whose ``manifest_url`` covers
only part of the ladder (a separate MPD per codec).
"""
from __future__ import annotations
import json
from functools import partial
from pathlib import Path
from typing import Any, Optional
import click
import pytest
from unshackle.core.import_service import ImportService
from unshackle.core.manifests import DASH
MPD_TEMPLATE = """<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT10M">
<Period id="1">
<AdaptationSet contentType="video" mimeType="video/mp4" lang="en">
<Representation id="{rep_id}" codecs="{codecs}" bandwidth="{bandwidth}" width="{width}" height="{height}">
<BaseURL>v.mp4</BaseURL><SegmentBase indexRange="0-1"/>
</Representation>
</AdaptationSet>
</Period>
</MPD>
"""
AVC_URL = "https://example.invalid/avc/dash/m.mpd"
HEVC_URL = "https://example.invalid/hevc/dash/m.mpd"
MANIFESTS = {
AVC_URL: MPD_TEMPLATE.format(rep_id="v-avc", codecs="avc1.640028", bandwidth=5000000, width=1920, height=1080),
HEVC_URL: MPD_TEMPLATE.format(
rep_id="v-hevc", codecs="hvc1.2.4.L153.B0", bandwidth=6000000, width=3840, height=2160
),
}
def exported_video(track_id: str, url: str, codec: str, width: int, height: int) -> dict[str, Any]:
return {
"type": "Video",
"id": track_id,
"url": url,
"language": "en",
"descriptor": "DASH",
"codec": codec,
"bitrate": 5000000 if codec == "AVC" else 6000000,
"width": width,
"height": height,
}
def make_service(tmp_path: Path, manifest_url: Optional[str], tracks: dict[str, Any]) -> ImportService:
entry: dict[str, Any] = {
"meta": {"type": "movie", "id": "movie-1", "name": "Example Movie", "year": 2024, "language": "en"},
"manifest_type": "DASH",
"tracks": tracks,
}
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))
@pytest.fixture(autouse=True)
def local_manifests(monkeypatch: pytest.MonkeyPatch) -> None:
"""Serve each MPD from memory, so a test never depends on the network."""
monkeypatch.setattr(DASH, "from_url", lambda url, session=None, **kw: DASH.from_text(MANIFESTS[url], url))
def test_export_without_manifest_url_still_rebuilds_dash_manifest_data(tmp_path: Path) -> None:
"""No top-level manifest_url means the export's own track URLs are the only MPDs to fetch."""
service = make_service(tmp_path, None, {"v-avc": exported_video("v-avc", AVC_URL, "AVC", 1920, 1080)})
tracks = service.get_tracks(next(iter(service.get_titles())))
assert [t.id for t in tracks.videos] == ["v-avc"]
# DASH.download_track reads all three of these; a missing one is a KeyError mid-download.
assert set(tracks.videos[0].data.get("dash", {})) >= {"manifest", "adaptation_set", "representation"}
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(
tmp_path,
AVC_URL,
{"v-hevc": exported_video("v-hevc", HEVC_URL, "HEVC", 3840, 2160)},
)
tracks = service.get_tracks(next(iter(service.get_titles())))
codecs = {t.codec.name for t in tracks.videos if t.codec}
assert "HEVC" in codecs, "the exported HEVC track was dropped, so -v h.265 has nothing to select"
hevc = next(t for t in tracks.videos if t.codec and t.codec.name == "HEVC")
assert set(hevc.data.get("dash", {})) >= {"manifest", "adaptation_set", "representation"}
# bound to its own manifest's representation, not just to any populated mapping
assert hevc.data["dash"]["representation"].get("id") == "v-hevc"
SUB_MPD_URL = "https://example.invalid/subs/de.mpd"
MAIN_WITH_SUB = """<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT10M">
<Period id="1">
<AdaptationSet contentType="video" mimeType="video/mp4" lang="en">
<Representation id="v-avc" codecs="avc1.640028" bandwidth="5000000" width="1920" height="1080">
<BaseURL>v.mp4</BaseURL><SegmentBase indexRange="0-1"/></Representation>
</AdaptationSet>
<AdaptationSet contentType="text" mimeType="text/vtt" lang="en">
<Representation id="s-en" bandwidth="1000">
<BaseURL>en.vtt</BaseURL><SegmentBase indexRange="0-1"/></Representation>
</AdaptationSet>
</Period>
</MPD>
"""
FOREIGN_SUB_MPD = """<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT10M">
<Period id="1">
<AdaptationSet contentType="text" mimeType="text/vtt" lang="de">
<Representation id="s-de" bandwidth="1000">
<BaseURL>de.vtt</BaseURL><SegmentBase indexRange="0-1"/></Representation>
</AdaptationSet>
</Period>
</MPD>
"""
def test_foreign_subtitle_adds_to_the_manifests_own(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Only a side-loaded (URL) subtitle replaces the re-parsed set.
A subtitle that lives on its own MPD is an addition. Nothing about it says the service
considered the main manifest's copy the worse one, and clearing on it deletes languages the
export never meant to drop.
"""
manifests = {AVC_URL: MAIN_WITH_SUB, SUB_MPD_URL: FOREIGN_SUB_MPD}
monkeypatch.setattr(DASH, "from_url", lambda url, session=None, **kw: DASH.from_text(manifests[url], url))
foreign = DASH.from_text(FOREIGN_SUB_MPD, SUB_MPD_URL).to_tracks(language="de").subtitles[0]
service = make_service(
tmp_path,
AVC_URL,
{
foreign.id: {
"type": "Subtitle",
"id": foreign.id,
"url": SUB_MPD_URL,
"language": "de",
"descriptor": "DASH",
"codec": "WebVTT",
}
},
)
tracks = service.get_tracks(next(iter(service.get_titles())))
assert sorted(str(t.language) for t in tracks.subtitles) == ["de", "en"], (
"the main manifest's own subtitles were wiped by a subtitle from another MPD"
)
TWO_RUNG_MPD = """<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT10M">
<Period id="1">
<AdaptationSet contentType="video" mimeType="video/mp4" lang="en">
<Representation id="lo" codecs="avc1.640028" bandwidth="3000000" width="1920" height="1080">
<BaseURL>lo.mp4</BaseURL><SegmentBase indexRange="0-1"/></Representation>
<Representation id="hi" codecs="avc1.640028" bandwidth="{top}" width="1920" height="1080">
<BaseURL>hi.mp4</BaseURL><SegmentBase indexRange="0-1"/></Representation>
</AdaptationSet>
</Period>
</MPD>
"""
@pytest.mark.parametrize(
"exported_url",
[AVC_URL, AVC_URL + "?token=EXPIRED"],
ids=["same-url", "signed-url"],
)
def test_reencoded_top_rung_does_not_readd_the_exported_one(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, exported_url: str
) -> None:
"""Track ids hash the bitrate, so re-encoding a rung makes the exported id look like a foreign MPD's.
Merging it back on that basis duplicates the rung, and the attribute fallback then binds the
copy to the first representation of the same codec and resolution: the 3 Mbps one. The result
downloads `lo` under a 6 Mbps name.
The signed variant is the same manifest reached by two strings: a service may fetch a signed
URL while registering the bare one as Title.tracks.manifest_url, so comparing them whole
reopens the duplicate.
"""
exported = DASH.from_text(TWO_RUNG_MPD.format(top=6000000), exported_url).to_tracks(language="en")
top = next(t for t in exported.videos if t.bitrate == 6000000)
monkeypatch.setattr(
DASH, "from_url", lambda url, session=None, **kw: DASH.from_text(TWO_RUNG_MPD.format(top=6500000), url)
)
service = make_service(tmp_path, AVC_URL, {top.id: exported_video(top.id, exported_url, "AVC", 1920, 1080)})
tracks = service.get_tracks(next(iter(service.get_titles())))
assert sorted(t.bitrate for t in tracks.videos) == [3000000, 6500000], (
"the re-parsed ladder is the only source for this manifest's rungs"
)
bound = {t.data["dash"]["representation"].get("id") for t in tracks.videos}
assert bound == {"lo", "hi"}, "two tracks were bound to the same representation"
def test_clearkey_license_hook_is_bindable(tmp_path: Path) -> None:
"""dl.py binds service.get_clearkey_license for every track, DRM or not, before downloading.
Bind it the way dl.py does rather than asking hasattr: a non-callable attribute satisfies
hasattr and still fails at the partial with a TypeError.
"""
service = make_service(tmp_path, AVC_URL, {})
bound = partial(service.get_clearkey_license, title=None, track=None)
assert callable(bound)
with pytest.raises(RuntimeError, match="keys come from the export"):
bound(challenge=b"")
+106 -10
View File
@@ -16,7 +16,7 @@ from unshackle.core.constants import AnyTrack
from unshackle.core.credential import Credential
from unshackle.core.drm import drm_from_dict
from unshackle.core.manifests import DASH, HLS, ISM
from unshackle.core.remote_service import RemoteService, _build_title, _resolve_proxy
from unshackle.core.remote_service import RemoteService, _build_title, _match_track, _resolve_proxy
from unshackle.core.titles import Episode, Movies, Series, Title_T, Titles_T, remap_titles
from unshackle.core.tracks import Audio, Chapter, Chapters, Tracks, Video
from unshackle.core.tracks.attachment import Attachment
@@ -25,6 +25,7 @@ from unshackle.core.tracks.track import Track
log = logging.getLogger("import")
PARSERS = {"DASH": DASH, "HLS": HLS, "ISM": ISM}
MANIFEST_DATA_KEYS = {"DASH": "dash", "ISM": "ism"}
def _fallback_language(language: Any) -> Optional[Any]:
@@ -39,6 +40,61 @@ def _fallback_language(language: Any) -> Optional[Any]:
return language
def _resolve_import_manifest_data(
tracks: Tracks,
manifest_type: Optional[str],
*,
session: requests.Session,
language: Any,
title: Title_T,
) -> None:
"""Populate ``track.data`` for DASH/ISM tracks rebuilt from export dicts.
Exports may omit ``manifest_url`` (e.g. 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.
"""
if manifest_type not in MANIFEST_DATA_KEYS:
return
parser = PARSERS[manifest_type]
data_key = MANIFEST_DATA_KEYS[manifest_type]
pending = [
track
for track in [*tracks.videos, *tracks.audio, *tracks.subtitles]
if track.descriptor.name == manifest_type and not track.data.get(data_key)
]
if not pending:
return
fallback_lang = _fallback_language(language)
for url in {str(track.url) for track in pending if track.url}:
try:
manifest = parser.from_url(url=url, session=session)
parsed = manifest.to_tracks(language=fallback_lang)
except ValueError as e:
if "Language information could not be derived" in str(e):
raise click.ClickException(
f"No language for '{title}': the {manifest_type} manifest has none and the "
f"service did not set Title.language."
)
raise click.ClickException(f"Failed to parse the {manifest_type} manifest for '{title}'. ({e})")
except Exception as e:
raise click.ClickException(
f"Failed to fetch the {manifest_type} manifest for '{title}'. "
f"The manifest URL may have expired since export. ({e})"
)
local_tracks = [*parsed.videos, *parsed.audio, *parsed.subtitles]
for track in pending:
if track.data.get(data_key) or str(track.url) != url:
continue
matched = _match_track(track, local_tracks)
if matched and matched.data.get(data_key):
track.data.update(matched.data)
class ImportService:
"""Reconstructs a download from an export JSON.
@@ -144,11 +200,12 @@ class ImportService:
def get_tracks(self, title: Title_T) -> Tracks:
"""Reconstruct the title's tracks from the export.
DASH/ISM: re-fetch and re-parse the manifest and return the full ladder (the importer
picks quality with normal dl flags; keys are injected by KID later), then merge in the
exported URL tracks. A service side-loads its own subtitles when the manifest's are the
worse copy, so any direct-URL subtitle in the export means the whole re-parsed subtitle
set is dropped in favour of the exported one.
DASH/ISM: re-fetch and re-parse ``manifest_url`` for the full ladder on that MPD (the importer
picks quality with normal dl flags; keys are injected by KID later), then merge in exported
DASH/ISM tracks from other MPDs (e.g. a separate HEVC manifest) and direct-URL side-loads.
A service side-loads its own subtitles when the manifest's are the worse copy, so any
direct-URL subtitle in the export means the whole re-parsed subtitle set is dropped in
favour of the exported one.
HLS/URL: rebuild from the stored per-track dicts, since the variant is re-fetched from
track.url at download time and a master playlist can hand out a new token on every fetch.
"""
@@ -186,8 +243,23 @@ class ImportService:
raise click.ClickException(f"Failed to parse the {manifest_type} manifest for '{title}'. ({e})")
for track in parsed:
tracks.add(track)
track_dicts = [t for t in tracks_map.values() if t.get("descriptor", "URL") == "URL"]
if any(t.get("type") == "Subtitle" for t in track_dicts):
parsed_ids = {str(t.id) for t in tracks}
url_dicts = [t for t in tracks_map.values() if t.get("descriptor", "URL") == "URL"]
# Merge back only tracks from another manifest, such as a separate HEVC one; the
# re-parse already covers manifest_url's own. Query stripped when comparing, since a
# service can sign the URL it fetches and register the bare one for the same manifest.
manifest_base = str(manifest_url).split("?")[0]
foreign_dicts = [
t
for t in tracks_map.values()
if t.get("descriptor") == manifest_type
and str(t.get("url")).split("?")[0] != manifest_base
and str(t.get("id")) not in parsed_ids
]
track_dicts = [*url_dicts, *foreign_dicts]
# Only a side-loaded subtitle replaces the re-parsed set, since the service chose its
# own copy over the manifest's. A subtitle on another manifest is an addition.
if any(t.get("type") == "Subtitle" for t in url_dicts):
tracks.subtitles.clear()
else:
track_dicts = list(tracks_map.values())
@@ -203,6 +275,14 @@ class ImportService:
track.drm = drm
tracks.add(track, warn_only=True)
_resolve_import_manifest_data(
tracks,
manifest_type,
session=self.session,
language=title.language,
title=title,
)
for attachment in entry.get("attachments") or []:
url = attachment.get("url")
if not url:
@@ -282,14 +362,25 @@ class ImportService:
@staticmethod
def track_is_encrypted(track: Any) -> bool:
"""True if the track carries DRM or its DASH manifest declares ContentProtection."""
"""True if the track carries DRM or its manifest declares protection.
ISM is checked as well as DASH because ISM.download_track reads only ``track.drm``. A rung
that misses key injection here downloads encrypted and muxes without error. DASH re-derives
its DRM from the manifest elements, so it survives the same omission.
"""
if track.drm:
return True
dash = track.data.get("dash") if getattr(track, "data", None) else None
data = getattr(track, "data", None) or {}
dash = data.get("dash")
if dash:
for element in (dash.get("representation"), dash.get("adaptation_set")):
if element is not None and element.findall("ContentProtection"):
return True
ism = data.get("ism")
if ism:
manifest = ism.get("manifest")
if manifest is not None and manifest.findall(".//ProtectionHeader"):
return True
return False
def exported_drm_system(self) -> str:
@@ -318,6 +409,11 @@ class ImportService:
) -> Optional[Union[bytes, str]]:
raise RuntimeError("ImportService should not request a license; keys come from the export.")
def get_clearkey_license(
self, *, challenge: bytes, title: Title_T, track: AnyTrack
) -> Optional[Union[bytes, str, dict]]:
raise RuntimeError("ImportService should not request a license; keys come from the export.")
def on_segment_downloaded(self, track: AnyTrack, segment: Any) -> None:
pass
+11 -1
View File
@@ -16,6 +16,7 @@ from zlib import crc32
from langcodes import Language
from requests import Session
from requests.adapters import HTTPAdapter, Retry
from unshackle.core import binaries
from unshackle.core.cdm.detect import is_playready_cdm, is_widevine_cdm
@@ -30,7 +31,7 @@ from unshackle.core.utils.subprocess import ffprobe
def direct_session(session: Union[Session, "RnetSession"]) -> Session:
"""Vanilla requests.Session with copied headers/cookies, no proxy."""
"""requests.Session with copied headers/cookies and no proxy."""
new = Session()
headers = getattr(session, "headers", None)
if headers is not None:
@@ -45,6 +46,15 @@ def direct_session(session: Union[Session, "RnetSession"]) -> Session:
new.cookies.update(jar if jar is not None else cookies)
except Exception:
pass
new.mount(
"https://",
HTTPAdapter(
max_retries=Retry(total=5, backoff_factor=0.2, status_forcelist=[429, 500, 502, 503, 504]),
pool_maxsize=64,
pool_block=True,
),
)
new.mount("http://", new.adapters["https://"])
return new