mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-25 02:15:08 +00:00
Compare commits
13
Commits
5.2.0
..
3f5b5e0fe1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f5b5e0fe1 | ||
|
|
2f86d782bf | ||
|
|
d4b0c81c63 | ||
|
|
9cb20693e0 | ||
|
|
fb90cd87b0 | ||
|
|
8a9a1e1bdc | ||
|
|
40eafce17b | ||
|
|
733cb02391 | ||
|
|
5b724b48f8 | ||
|
|
a2f670b140 | ||
|
|
a9c677c349 | ||
|
|
ba69bc7d61 | ||
|
|
2e1ffebec0 |
@@ -39,7 +39,7 @@ unshackle --help
|
||||
|
||||
External tools on your `PATH` (recommended versions):
|
||||
|
||||
- [Python](https://www.python.org/) - 3.10 - 3.12
|
||||
- [Python](https://www.python.org/) - 3.11 - 3.14
|
||||
- [uv](https://docs.astral.sh/uv/) - ≥ 0.5
|
||||
- [FFmpeg](https://ffmpeg.org/) - ≥ 6.0
|
||||
- [MKVToolNix](https://mkvtoolnix.download/) - ≥ 80
|
||||
|
||||
@@ -204,6 +204,23 @@ Enable/disable tagging downloaded files with IMDB/TMDB/TVDB identifiers (when av
|
||||
Note: The `--split-audio` CLI flag overrides this setting. When `--split-audio` is passed,
|
||||
`merge_audio` is effectively set to `false` for that run.
|
||||
|
||||
- `merge_video`
|
||||
Merge video **language variants** into one file. Default: `false`
|
||||
- `false`: One MKV per video track (the default behaviour).
|
||||
- `true`: Group the selected video tracks by `(resolution, range, codec)` and merge
|
||||
each group into one MKV, so only language varies within a file. The player switches
|
||||
between the language tracks. No re-encode, no concatenation.
|
||||
|
||||
Only the language dimension is collapsed. Different **resolutions**, **ranges**
|
||||
(SDR/HDR10/HDR10+/DV/HYBRID) and **codecs** (H264/H265) always stay in separate files.
|
||||
For example, `-r HYBRID,DV,HDR10,SDR --merge-video` produces one file per range (never a
|
||||
single combined file), while a title offering English + French video of the same
|
||||
resolution/range/codec produces one file containing both video tracks.
|
||||
|
||||
Note: The `--merge-video` CLI flag overrides this setting. Can be set per service under
|
||||
`services.<TAG>.muxing.merge_video`. Change `group_videos_by_variant` in
|
||||
`unshackle/commands/dl.py` to adjust the grouping.
|
||||
|
||||
- `default_language` (dict)
|
||||
Override which track is flagged as the default in the muxed MKV, regardless
|
||||
of the title's original language. Useful when you always want your player to
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ name = "unshackle"
|
||||
version = "5.2.0"
|
||||
description = "Modular Movie, TV, and Music Archival Software."
|
||||
authors = [{ name = "unshackle team" }]
|
||||
requires-python = ">=3.10,<3.13"
|
||||
requires-python = ">=3.11,<3.15"
|
||||
readme = "README.md"
|
||||
license = "GPL-3.0-only"
|
||||
keywords = [
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
"""Tests for ``--merge-video`` track grouping.
|
||||
|
||||
``group_videos_by_variant`` (``unshackle/commands/dl.py``) decides which selected video
|
||||
tracks share one MKV when merge mode is on. The rule: group by ``(resolution, range,
|
||||
codec)`` so only language varies within a file; resolutions, ranges and codecs stay
|
||||
separate. With ``merge=False`` every track is its own group (one file per track).
|
||||
|
||||
These lock the pure grouping unit; the surrounding mux loop is Click-command orchestration.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unshackle.commands.dl import group_videos_by_variant
|
||||
from unshackle.core.tracks import Video
|
||||
|
||||
|
||||
def make_video(
|
||||
track_id: str,
|
||||
*,
|
||||
range_: Video.Range,
|
||||
height: int,
|
||||
codec: Video.Codec,
|
||||
language: str = "en",
|
||||
) -> Video:
|
||||
return Video(
|
||||
id_=track_id,
|
||||
url=f"https://example.test/{track_id}.m3u8",
|
||||
language=language,
|
||||
codec=codec,
|
||||
range_=range_,
|
||||
width=int(height * 16 / 9),
|
||||
height=height,
|
||||
bitrate=1_000_000,
|
||||
)
|
||||
|
||||
|
||||
HEVC = Video.Codec.HEVC
|
||||
AVC = Video.Codec.AVC
|
||||
SDR = Video.Range.SDR
|
||||
HDR10 = Video.Range.HDR10
|
||||
DV = Video.Range.DV
|
||||
|
||||
|
||||
def test_merge_collapses_language_only() -> None:
|
||||
"""Same (height, range, codec), different language → one group."""
|
||||
videos = [
|
||||
make_video("en", range_=SDR, height=1080, codec=HEVC, language="en"),
|
||||
make_video("fr", range_=SDR, height=1080, codec=HEVC, language="fr"),
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=True)
|
||||
assert len(groups) == 1
|
||||
assert [v.id for v in groups[0]] == ["en", "fr"]
|
||||
|
||||
|
||||
def test_merge_splits_on_codec() -> None:
|
||||
"""H264 vs H265 of the same resolution+range → separate groups."""
|
||||
videos = [
|
||||
make_video("hevc", range_=SDR, height=1080, codec=HEVC),
|
||||
make_video("avc", range_=SDR, height=1080, codec=AVC),
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=True)
|
||||
assert len(groups) == 2
|
||||
assert all(len(g) == 1 for g in groups)
|
||||
|
||||
|
||||
def test_merge_splits_on_range() -> None:
|
||||
"""SDR vs HDR10 of the same resolution+codec → separate groups."""
|
||||
videos = [
|
||||
make_video("sdr", range_=SDR, height=1080, codec=HEVC),
|
||||
make_video("hdr10", range_=HDR10, height=1080, codec=HEVC),
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=True)
|
||||
assert len(groups) == 2
|
||||
|
||||
|
||||
def test_merge_splits_on_resolution() -> None:
|
||||
"""1080p vs 2160p of the same range+codec → separate groups."""
|
||||
videos = [
|
||||
make_video("1080", range_=SDR, height=1080, codec=HEVC),
|
||||
make_video("2160", range_=SDR, height=2160, codec=HEVC),
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=True)
|
||||
assert len(groups) == 2
|
||||
|
||||
|
||||
def test_merge_multi_range_yields_one_group_per_range() -> None:
|
||||
"""Regression guard: -r HYBRID,DV,HDR10,SDR must never collapse into one file.
|
||||
|
||||
HYBRID is resolved upstream into a DV deliverable plus the requested standalone
|
||||
ranges; here the four selected single-range tracks must stay in four groups.
|
||||
"""
|
||||
videos = [
|
||||
make_video("sdr", range_=SDR, height=2160, codec=HEVC),
|
||||
make_video("hdr10", range_=HDR10, height=2160, codec=HEVC),
|
||||
make_video("dv", range_=DV, height=2160, codec=HEVC),
|
||||
make_video("dv-hybrid", range_=DV, height=1080, codec=HEVC), # different height
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=True)
|
||||
assert len(groups) == 4
|
||||
|
||||
|
||||
def test_no_merge_yields_one_group_per_track() -> None:
|
||||
"""merge=False reproduces today's per-track behaviour exactly."""
|
||||
videos = [
|
||||
make_video("en", range_=SDR, height=1080, codec=HEVC, language="en"),
|
||||
make_video("fr", range_=SDR, height=1080, codec=HEVC, language="fr"),
|
||||
make_video("avc", range_=SDR, height=1080, codec=AVC),
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=False)
|
||||
assert len(groups) == 3
|
||||
assert all(len(g) == 1 for g in groups)
|
||||
|
||||
|
||||
def test_merge_preserves_first_seen_order() -> None:
|
||||
"""Group order follows first-seen track order, for stable output filenames."""
|
||||
videos = [
|
||||
make_video("hevc-en", range_=SDR, height=1080, codec=HEVC, language="en"),
|
||||
make_video("avc-en", range_=SDR, height=1080, codec=AVC, language="en"),
|
||||
make_video("hevc-fr", range_=SDR, height=1080, codec=HEVC, language="fr"),
|
||||
]
|
||||
groups = group_videos_by_variant(videos, merge=True)
|
||||
# HEVC group seen first (and gathers both languages), AVC group second.
|
||||
assert [v.id for v in groups[0]] == ["hevc-en", "hevc-fr"]
|
||||
assert [v.id for v in groups[1]] == ["avc-en"]
|
||||
|
||||
|
||||
def test_empty_input_returns_empty() -> None:
|
||||
assert group_videos_by_variant([], merge=True) == []
|
||||
assert group_videos_by_variant([], merge=False) == []
|
||||
@@ -20,7 +20,7 @@ class _FakeReq:
|
||||
|
||||
|
||||
def _run(coro):
|
||||
return asyncio.get_event_loop().run_until_complete(coro) if False else asyncio.run(coro)
|
||||
return asyncio.run(coro)
|
||||
|
||||
|
||||
def test_skips_when_client_does_not_accept_gzip() -> None:
|
||||
|
||||
@@ -17,23 +17,32 @@ def _body(resp) -> dict:
|
||||
|
||||
|
||||
def test_api_error_default_http_status_per_code() -> None:
|
||||
# Every live APIErrorCode and the status a client observes for it. This is the
|
||||
# status half of the error contract PRD 05 must preserve through the collapse.
|
||||
cases = {
|
||||
APIErrorCode.INVALID_INPUT: 400,
|
||||
APIErrorCode.INVALID_SERVICE: 400,
|
||||
APIErrorCode.AUTH_REQUIRED: 401,
|
||||
APIErrorCode.INVALID_PROXY: 400,
|
||||
APIErrorCode.INVALID_PARAMETERS: 400,
|
||||
APIErrorCode.AUTH_FAILED: 401,
|
||||
APIErrorCode.FORBIDDEN: 403,
|
||||
APIErrorCode.GEOFENCE: 403,
|
||||
APIErrorCode.NOT_FOUND: 404,
|
||||
APIErrorCode.NO_CONTENT: 404,
|
||||
APIErrorCode.JOB_NOT_FOUND: 404,
|
||||
APIErrorCode.SESSION_NOT_FOUND: 404,
|
||||
APIErrorCode.TRACK_NOT_FOUND: 404,
|
||||
APIErrorCode.RATE_LIMITED: 429,
|
||||
APIErrorCode.INTERNAL_ERROR: 500,
|
||||
APIErrorCode.DOWNLOAD_ERROR: 500,
|
||||
APIErrorCode.WORKER_ERROR: 500,
|
||||
APIErrorCode.SERVICE_ERROR: 502,
|
||||
APIErrorCode.DRM_ERROR: 502,
|
||||
APIErrorCode.NETWORK_ERROR: 503,
|
||||
APIErrorCode.SERVICE_UNAVAILABLE: 503,
|
||||
}
|
||||
# Lock that the cases above are the *complete* live set — a new/removed code trips this.
|
||||
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
|
||||
|
||||
@@ -125,3 +134,45 @@ def test_handle_api_exception_categorizes_generic() -> None:
|
||||
resp = handle_api_exception(ConnectionError("oops"))
|
||||
body = _body(resp)
|
||||
assert body["error_code"] == "NETWORK_ERROR"
|
||||
|
||||
|
||||
# --- Error contract harness (PRD 05 gate) ---------------------------------
|
||||
# Locks the FULL observable response — (http status, error_code, retryable) — that
|
||||
# a client sees for a generic (non-APIError) exception flowing through the
|
||||
# handle_api_exception funnel that every route's `except Exception` path calls.
|
||||
# This is the equivalence reference: after the categorize_exception collapse,
|
||||
# any tuple that changes here is a client-visible behaviour change, not a refactor.
|
||||
@pytest.mark.parametrize(
|
||||
"exc, status, code, retryable",
|
||||
[
|
||||
(Exception("Invalid credentials provided"), 401, "AUTH_FAILED", False),
|
||||
(Exception("Connection refused"), 503, "NETWORK_ERROR", True),
|
||||
(TimeoutError("read timeout"), 503, "NETWORK_ERROR", True),
|
||||
(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("503 service unavailable"), 503, "SERVICE_UNAVAILABLE", True),
|
||||
(ValueError("malformed body"), 400, "INVALID_INPUT", False),
|
||||
(RuntimeError("totally novel failure xyz"), 500, "INTERNAL_ERROR", False),
|
||||
],
|
||||
)
|
||||
def test_error_contract_generic_exception(exc: Exception, status: int, code: str, retryable: bool) -> None:
|
||||
resp = handle_api_exception(exc)
|
||||
assert resp.status == status
|
||||
body = _body(resp)
|
||||
assert body["error_code"] == code
|
||||
# retryable is only serialized when True (build_error_response omits it otherwise).
|
||||
assert body.get("retryable", False) is retryable
|
||||
|
||||
|
||||
def test_error_contract_apierror_passthrough_full_shape() -> None:
|
||||
# APIError carriers keep status + code + message + details + retryable verbatim.
|
||||
err = APIError(APIErrorCode.RATE_LIMITED, "slow down", details={"retry_after": 5}, retryable=True)
|
||||
resp = handle_api_exception(err)
|
||||
assert resp.status == 429
|
||||
body = _body(resp)
|
||||
assert body["error_code"] == "RATE_LIMITED"
|
||||
assert body["message"] == "slow down"
|
||||
assert body["details"] == {"retry_after": 5}
|
||||
assert body["retryable"] is True
|
||||
|
||||
@@ -199,6 +199,22 @@ def test_serialize_drm_widevine_minimal() -> None:
|
||||
assert info["license_url"] == "https://lic.example.com/wv"
|
||||
|
||||
|
||||
def test_serialize_drm_playready_pssh_without_dumps_is_omitted() -> None:
|
||||
# pyplayready's PSSH exposes no dumps()/to_base64()/__bytes__; serialization
|
||||
# must omit the pssh field rather than emit an object repr.
|
||||
class _PSSH:
|
||||
pass
|
||||
|
||||
class _PlayReady:
|
||||
def __init__(self) -> None:
|
||||
self._pssh = _PSSH()
|
||||
self.kids = ["00112233445566778899aabbccddeeff"]
|
||||
|
||||
info = serialize_drm(_PlayReady())[0]
|
||||
assert "pssh" not in info
|
||||
assert info["kids"] == ["00112233445566778899aabbccddeeff"]
|
||||
|
||||
|
||||
# ---------- validate_service ----------
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import time
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.api.input_bridge import AuthStatus, BridgeCancelledError, InputBridge
|
||||
from unshackle.core.api.input_bridge import AuthStatus, InputBridge
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -16,7 +16,6 @@ def test_initial_status_is_authenticating() -> None:
|
||||
bridge = InputBridge()
|
||||
assert bridge.status is AuthStatus.AUTHENTICATING
|
||||
assert bridge.get_pending_prompt() is None
|
||||
assert bridge.error is None
|
||||
|
||||
|
||||
def test_submit_response_returns_false_when_no_prompt_pending() -> None:
|
||||
@@ -49,16 +48,15 @@ def test_request_input_blocks_until_submit() -> None:
|
||||
|
||||
def test_request_input_times_out() -> None:
|
||||
bridge = InputBridge()
|
||||
with pytest.raises(TimeoutError):
|
||||
with pytest.raises(TimeoutError, match="No client response"):
|
||||
bridge.request_input("hello", timeout=0.6)
|
||||
assert bridge.status is AuthStatus.FAILED
|
||||
assert "timed out" in (bridge.error or "")
|
||||
|
||||
|
||||
def test_cancel_before_request_raises_immediately() -> None:
|
||||
bridge = InputBridge()
|
||||
bridge.cancel()
|
||||
with pytest.raises(BridgeCancelledError):
|
||||
with pytest.raises(RuntimeError, match="cancelled"):
|
||||
bridge.request_input("hello", timeout=5)
|
||||
assert bridge.status is AuthStatus.FAILED
|
||||
|
||||
@@ -70,7 +68,7 @@ def test_cancel_unblocks_pending_request() -> None:
|
||||
def worker() -> None:
|
||||
try:
|
||||
bridge.request_input("OTP?", timeout=5)
|
||||
except BridgeCancelledError as e:
|
||||
except RuntimeError as e:
|
||||
exc.append(e)
|
||||
|
||||
t = threading.Thread(target=worker)
|
||||
@@ -82,7 +80,7 @@ def test_cancel_unblocks_pending_request() -> None:
|
||||
|
||||
bridge.cancel()
|
||||
t.join(timeout=2)
|
||||
assert exc and isinstance(exc[0], BridgeCancelledError)
|
||||
assert exc and isinstance(exc[0], RuntimeError)
|
||||
assert bridge.status is AuthStatus.FAILED
|
||||
|
||||
|
||||
@@ -92,9 +90,7 @@ def test_get_pending_prompt_returns_none_outside_pending_state() -> None:
|
||||
assert bridge.get_pending_prompt() is None
|
||||
|
||||
|
||||
def test_status_and_error_setters() -> None:
|
||||
def test_status_setter() -> None:
|
||||
bridge = InputBridge()
|
||||
bridge.status = AuthStatus.AUTHENTICATED
|
||||
bridge.error = "boom"
|
||||
assert bridge.status is AuthStatus.AUTHENTICATED
|
||||
assert bridge.error == "boom"
|
||||
|
||||
@@ -11,7 +11,8 @@ import pytest
|
||||
from aiohttp import web
|
||||
|
||||
from unshackle.core.api.compression import compression_middleware
|
||||
from unshackle.core.api.routes import cors_middleware, setup_routes
|
||||
from unshackle.core.api.errors import APIError, APIErrorCode
|
||||
from unshackle.core.api.routes import api_handler, cors_middleware, setup_routes
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -88,10 +89,44 @@ def test_setup_routes_remote_only_excludes_list_and_download(make_app) -> None:
|
||||
assert ("POST", "/api/list-tracks") not in paths
|
||||
assert ("POST", "/api/download") not in paths
|
||||
assert ("GET", "/api/download/jobs") not in paths
|
||||
# session endpoints still present
|
||||
assert ("POST", "/api/session/create") in paths
|
||||
assert ("GET", "/api/session/{session_id}/titles") in paths
|
||||
assert ("POST", "/api/session/{session_id}/license") in paths
|
||||
# The full remote subset is still registered after the single-table collapse.
|
||||
remote_subset = {
|
||||
("GET", "/api/health"),
|
||||
("GET", "/api/services"),
|
||||
("POST", "/api/search"),
|
||||
("POST", "/api/session/create"),
|
||||
("GET", "/api/session/{session_id}/titles"),
|
||||
("POST", "/api/session/{session_id}/tracks"),
|
||||
("POST", "/api/session/{session_id}/segments"),
|
||||
("POST", "/api/session/{session_id}/license"),
|
||||
("GET", "/api/session/{session_id}/prompt"),
|
||||
("POST", "/api/session/{session_id}/prompt"),
|
||||
("GET", "/api/session/{session_id}"),
|
||||
("DELETE", "/api/session/{session_id}"),
|
||||
}
|
||||
assert remote_subset.issubset(paths)
|
||||
|
||||
|
||||
async def test_api_handler_maps_apierror(aiohttp_client) -> None:
|
||||
"""The error wrapper turns a raised APIError into the same structured response
|
||||
the per-handler except blocks produced (status + code from APIError)."""
|
||||
|
||||
@api_handler
|
||||
async def boom(request: web.Request) -> web.Response:
|
||||
raise APIError(APIErrorCode.NOT_FOUND, "nope", details={"x": 1})
|
||||
|
||||
app = web.Application()
|
||||
app["debug_api"] = False
|
||||
app.router.add_get("/boom", boom)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.get("/boom")
|
||||
assert resp.status == 404 # NOT_FOUND -> 404 per errors.APIError._default_http_status
|
||||
body = await resp.json()
|
||||
assert body["status"] == "error"
|
||||
assert body["error_code"] == "NOT_FOUND"
|
||||
assert body["message"] == "nope"
|
||||
assert body["details"] == {"x": 1}
|
||||
|
||||
|
||||
async def test_cors_preflight_returns_headers(make_app, aiohttp_client) -> None:
|
||||
|
||||
@@ -85,6 +85,21 @@ async def test_cleanup_expired_keeps_pending_input_under_grace(store: SessionSto
|
||||
assert store.session_count == 1
|
||||
|
||||
|
||||
async def test_cleanup_expired_drops_pending_input_past_grace(store: SessionStore) -> None:
|
||||
"""The auth-state grace is the single AUTH_INPUT_TIMEOUT rule, not a separate magic number."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from unshackle.core.api.input_bridge import AUTH_INPUT_TIMEOUT
|
||||
|
||||
entry = await store.create("ATV", _FakeService())
|
||||
entry.input_bridge = InputBridge()
|
||||
entry.auth_status = AuthStatus.PENDING_INPUT
|
||||
entry.last_accessed = datetime.now(timezone.utc) - timedelta(seconds=AUTH_INPUT_TIMEOUT + 1)
|
||||
removed = await store.cleanup_expired()
|
||||
assert removed == 1
|
||||
assert store.session_count == 0
|
||||
|
||||
|
||||
async def test_cancel_all_bridges(store: SessionStore) -> None:
|
||||
a = await store.create("ATV", _FakeService())
|
||||
b = await store.create("NF", _FakeService())
|
||||
|
||||
+82
-22
@@ -58,7 +58,7 @@ from unshackle.core.music import (
|
||||
write_music_manifest,
|
||||
write_music_metadata,
|
||||
)
|
||||
from unshackle.core.proxies import Basic, Gluetun, Hola, NordVPN, SurfsharkVPN, WindscribeVPN
|
||||
from unshackle.core.proxies import Basic, ExpressVPN, Gluetun, Hola, NordVPN, SurfsharkVPN, WindscribeVPN
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.title_cacher import get_account_hash
|
||||
@@ -100,6 +100,22 @@ def normalize_dl_config(dl_config: dict[str, Any]) -> dict[str, Any]:
|
||||
return {DL_OPTION_ALIASES.get(key, key): value for key, value in dl_config.items()}
|
||||
|
||||
|
||||
def group_videos_by_variant(videos: list[Video], *, merge: bool) -> list[list[Video]]:
|
||||
"""Group video tracks for muxing.
|
||||
|
||||
When ``merge`` is True, tracks sharing ``(height, range, codec)`` are grouped into one
|
||||
file so only language varies within a group; different resolutions, ranges and codecs
|
||||
stay in separate groups (separate files). When False, each track is its own group
|
||||
(one file per track, the default behaviour). Group order follows first-seen track order.
|
||||
"""
|
||||
if not merge:
|
||||
return [[video] for video in videos]
|
||||
groups: dict[tuple[Any, ...], list[Video]] = {}
|
||||
for video in videos:
|
||||
groups.setdefault((video.height, video.range, video.codec), []).append(video)
|
||||
return list(groups.values())
|
||||
|
||||
|
||||
def apply_service_dl_overrides(ctx: click.Context, service_dl_config: dict[str, Any], log: logging.Logger) -> None:
|
||||
"""Apply ``services.<TAG>.dl`` config onto ``ctx.params``. Explicit CLI/env values win;
|
||||
defaults and global ``dl:`` default_map values are replaced."""
|
||||
@@ -459,6 +475,13 @@ class dl:
|
||||
default=None,
|
||||
help="Create separate output files per audio codec instead of merging all audio.",
|
||||
)
|
||||
@click.option(
|
||||
"--merge-video",
|
||||
"merge_video",
|
||||
is_flag=True,
|
||||
default=None,
|
||||
help="Mux all selected video tracks into a single file instead of one file per track.",
|
||||
)
|
||||
@click.option(
|
||||
"--select-titles",
|
||||
is_flag=True,
|
||||
@@ -1027,6 +1050,8 @@ class dl:
|
||||
with console.status("Loading Proxy Providers...", spinner="dots"):
|
||||
if config.proxy_providers.get("basic"):
|
||||
self.proxy_providers.append(Basic(**config.proxy_providers["basic"]))
|
||||
if config.proxy_providers.get("expressvpn"):
|
||||
self.proxy_providers.append(ExpressVPN(**config.proxy_providers["expressvpn"]))
|
||||
if config.proxy_providers.get("nordvpn"):
|
||||
self.proxy_providers.append(NordVPN(**config.proxy_providers["nordvpn"]))
|
||||
if config.proxy_providers.get("surfsharkvpn"):
|
||||
@@ -1046,8 +1071,8 @@ class dl:
|
||||
# requesting proxy from a specific proxy provider
|
||||
requested_provider, proxy = proxy.split(":", maxsplit=1)
|
||||
# Match simple region codes (us, ca, uk1) or provider:region format (nordvpn:ca, windscribe:us)
|
||||
if re.match(r"^[a-z]{2}(?:\d+)?$", proxy, re.IGNORECASE) or re.match(
|
||||
r"^[a-z]+:[a-z]{2}(?:\d+)?$", proxy, re.IGNORECASE
|
||||
if re.match(r"^[a-z]{2}(?:[-][a-z0-9]+)*(?:\d+)?$", proxy, re.IGNORECASE) or re.match(
|
||||
r"^[a-z]+:[a-z]{2}(?:[-][a-z0-9]+)*(?:\d+)?$", proxy, re.IGNORECASE
|
||||
):
|
||||
proxy = proxy.lower()
|
||||
# Preserve the original user query (region code) for service-specific proxy_map overrides.
|
||||
@@ -1082,7 +1107,13 @@ class dl:
|
||||
else:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
else:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
display = None
|
||||
if hasattr(proxy_provider, "last_connection_display"):
|
||||
display = proxy_provider.last_connection_display()
|
||||
if display:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy {display}")
|
||||
else:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
else:
|
||||
for proxy_provider in self.proxy_providers:
|
||||
proxy_uri = proxy_provider.get_proxy(proxy)
|
||||
@@ -1098,7 +1129,13 @@ class dl:
|
||||
else:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
else:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
display = None
|
||||
if hasattr(proxy_provider, "last_connection_display"):
|
||||
display = proxy_provider.last_connection_display()
|
||||
if display:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy {display}")
|
||||
else:
|
||||
self.log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
break
|
||||
# Store proxy query info for service-specific overrides
|
||||
ctx.params["proxy_query"] = proxy_query
|
||||
@@ -1173,6 +1210,7 @@ class dl:
|
||||
worst: bool,
|
||||
best_available: bool,
|
||||
split_audio: Optional[bool] = None,
|
||||
merge_video: Optional[bool] = None,
|
||||
real_video_bitrate: bool = False,
|
||||
real_audio_bitrate: bool = False,
|
||||
progress_sink: Optional[Callable[[dict[str, Any]], None]] = None,
|
||||
@@ -3062,6 +3100,9 @@ class dl:
|
||||
# When we split audio (merge_audio=False), multiple outputs may exist per title, so suffix codec.
|
||||
append_audio_codec_suffix = not merge_audio
|
||||
|
||||
# Mux all selected video tracks into one file instead of one file per track.
|
||||
merge_video = merge_video if merge_video is not None else config.muxing.get("merge_video", False)
|
||||
|
||||
multiplex_tasks: list[tuple[TaskID, Tracks, Optional[Audio.Codec]]] = []
|
||||
# Track hybrid-processing outputs explicitly so we can always clean them up,
|
||||
# even if muxing fails early (e.g. SystemExit) before the normal delete loop.
|
||||
@@ -3097,22 +3138,25 @@ class dl:
|
||||
task_tracks = clone_tracks_for_audio(base_tracks, codec_audio_tracks)
|
||||
multiplex_tasks.append((task_id, task_tracks, audio_codec))
|
||||
|
||||
def mux_video_standalone(video_track: Optional[Video]) -> None:
|
||||
if video_track and video_track.dv_compatible_bitstream:
|
||||
apply_dv_fixup(video_track)
|
||||
def mux_video_group(video_tracks: list[Optional[Video]]) -> None:
|
||||
for video_track in video_tracks:
|
||||
if video_track and video_track.dv_compatible_bitstream:
|
||||
apply_dv_fixup(video_track)
|
||||
|
||||
task_description = "Multiplexing"
|
||||
if video_track:
|
||||
# All tracks in a merged group share height/range/codec, so describe from the first.
|
||||
head = next((v for v in video_tracks if v), None)
|
||||
if head:
|
||||
if len(quality) > 1:
|
||||
task_description += f" {video_track.height}p"
|
||||
task_description += f" {head.height}p"
|
||||
if len(range_) > 1:
|
||||
task_description += f" {video_track.range.name}"
|
||||
task_description += f" {head.range.name}"
|
||||
if len(vcodec) > 1:
|
||||
task_description += f" {video_track.codec.name}"
|
||||
task_description += f" {head.codec.name}"
|
||||
|
||||
task_tracks = Tracks(title.tracks) + title.tracks.chapters + title.tracks.attachments
|
||||
if video_track:
|
||||
task_tracks.videos = [video_track]
|
||||
if head:
|
||||
task_tracks.videos = [v for v in video_tracks if v]
|
||||
|
||||
enqueue_mux_tasks(task_description, task_tracks)
|
||||
|
||||
@@ -3172,16 +3216,18 @@ class dl:
|
||||
enqueue_mux_tasks(task_description, task_tracks)
|
||||
|
||||
# Mux every requested range standalone, skipping the ingredient-only DV.
|
||||
for video_track in original_videos:
|
||||
if video_track.hybrid_base_only:
|
||||
continue
|
||||
mux_video_standalone(video_track)
|
||||
# merge_video collapses only language variants (same height/range/codec).
|
||||
standalone_videos = [v for v in original_videos if not v.hybrid_base_only]
|
||||
for group in group_videos_by_variant(standalone_videos, merge=merge_video):
|
||||
mux_video_group(group)
|
||||
|
||||
console.print()
|
||||
else:
|
||||
# Normal mode: process each video track separately
|
||||
for video_track in title.tracks.videos or [None]:
|
||||
mux_video_standalone(video_track)
|
||||
# Normal mode: one file per video track, unless merge_video groups
|
||||
# same-(height, range, codec) language variants into one file.
|
||||
groups = group_videos_by_variant(title.tracks.videos, merge=merge_video)
|
||||
for group in groups or [[None]]:
|
||||
mux_video_group(group)
|
||||
|
||||
if progress_sink:
|
||||
progress_sink(
|
||||
@@ -4111,8 +4157,20 @@ class dl:
|
||||
return default_cookie_file
|
||||
|
||||
@staticmethod
|
||||
def get_cookie_jar(service: str, profile: Optional[str]) -> Optional[MozillaCookieJar]:
|
||||
def get_cookie_jar(service: str, profile: Optional[str]) -> Optional[CookieJar]:
|
||||
"""Get Service Cookies for Profile."""
|
||||
# Check if automatic Firefox cookie extraction is configured for this service
|
||||
ff_settings = getattr(config, "firefox_cookies", {}).get(service)
|
||||
if ff_settings:
|
||||
try:
|
||||
from unshackle.core.utils.firefox_cookie_extractor import get_firefox_cookies
|
||||
extracted_jar = get_firefox_cookies(ff_settings)
|
||||
if extracted_jar:
|
||||
return extracted_jar
|
||||
except Exception:
|
||||
# Fallback to file-based if Firefox extraction fails
|
||||
pass
|
||||
|
||||
cookie_file = dl.get_cookie_path(service, profile)
|
||||
if cookie_file:
|
||||
cookie_jar = MozillaCookieJar(cookie_file)
|
||||
@@ -4129,6 +4187,8 @@ class dl:
|
||||
cookie_file.write_text(cookie_data, "utf8")
|
||||
cookie_jar.load(ignore_discard=True, ignore_expires=True)
|
||||
return cookie_jar
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def save_cookies(path: Path, cookies: CookieJar):
|
||||
|
||||
@@ -16,7 +16,7 @@ from unshackle.core import binaries
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.console import console
|
||||
from unshackle.core.constants import context_settings
|
||||
from unshackle.core.proxies import Basic, Gluetun, Hola, NordVPN, SurfsharkVPN, WindscribeVPN
|
||||
from unshackle.core.proxies import Basic, ExpressVPN, Gluetun, Hola, NordVPN, SurfsharkVPN, WindscribeVPN
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.utils.click_types import ContextData
|
||||
@@ -67,6 +67,8 @@ def search(ctx: click.Context, no_proxy: bool, profile: Optional[str] = None, pr
|
||||
with console.status("Loading Proxy Providers...", spinner="dots"):
|
||||
if config.proxy_providers.get("basic"):
|
||||
proxy_providers.append(Basic(**config.proxy_providers["basic"]))
|
||||
if config.proxy_providers.get("expressvpn"):
|
||||
proxy_providers.append(ExpressVPN(**config.proxy_providers["expressvpn"]))
|
||||
if config.proxy_providers.get("nordvpn"):
|
||||
proxy_providers.append(NordVPN(**config.proxy_providers["nordvpn"]))
|
||||
if config.proxy_providers.get("surfsharkvpn"):
|
||||
@@ -86,8 +88,8 @@ def search(ctx: click.Context, no_proxy: bool, profile: Optional[str] = None, pr
|
||||
# requesting proxy from a specific proxy provider
|
||||
requested_provider, proxy = proxy.split(":", maxsplit=1)
|
||||
# Match simple region codes (us, ca, uk1) or provider:region format (nordvpn:ca, windscribe:us)
|
||||
if re.match(r"^[a-z]{2}(?:\d+)?$", proxy, re.IGNORECASE) or re.match(
|
||||
r"^[a-z]+:[a-z]{2}(?:\d+)?$", proxy, re.IGNORECASE
|
||||
if re.match(r"^[a-z]{2}(?:[-][a-z0-9]+)*(?:\d+)?$", proxy, re.IGNORECASE) or re.match(
|
||||
r"^[a-z]+:[a-z]{2}(?:[-][a-z0-9]+)*(?:\d+)?$", proxy, re.IGNORECASE
|
||||
):
|
||||
proxy = proxy.lower()
|
||||
with console.status(f"Getting a Proxy to {proxy}...", spinner="dots"):
|
||||
@@ -103,13 +105,25 @@ def search(ctx: click.Context, no_proxy: bool, profile: Optional[str] = None, pr
|
||||
log.error(f"The proxy provider {requested_provider} had no proxy for {proxy}")
|
||||
sys.exit(1)
|
||||
proxy = ctx.params["proxy"] = proxy_uri
|
||||
log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
display = None
|
||||
if hasattr(proxy_provider, "last_connection_display"):
|
||||
display = proxy_provider.last_connection_display()
|
||||
if display:
|
||||
log.info(f"Using {proxy_provider.__class__.__name__} Proxy {display}")
|
||||
else:
|
||||
log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
else:
|
||||
for proxy_provider in proxy_providers:
|
||||
proxy_uri = proxy_provider.get_proxy(proxy)
|
||||
if proxy_uri:
|
||||
proxy = ctx.params["proxy"] = proxy_uri
|
||||
log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
display = None
|
||||
if hasattr(proxy_provider, "last_connection_display"):
|
||||
display = proxy_provider.last_connection_display()
|
||||
if display:
|
||||
log.info(f"Using {proxy_provider.__class__.__name__} Proxy {display}")
|
||||
else:
|
||||
log.info(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
|
||||
break
|
||||
else:
|
||||
log.info(f"Using explicit Proxy: {proxy}")
|
||||
|
||||
@@ -104,6 +104,10 @@ class DownloadJob:
|
||||
completed_tracks: int = 0
|
||||
total_tracks: int = 0
|
||||
active_tracks: List[str] = field(default_factory=list)
|
||||
# Segment counts and transfer speed of the track downloading now (granular display)
|
||||
segments_done: float = 0.0
|
||||
segments_total: float = 0.0
|
||||
speed: Optional[str] = None
|
||||
|
||||
# Subtitles skipped under skip_subtitle_errors (non-fatal). Each entry is a dl.SkippedSubtitle
|
||||
# dict (id / language / title) so a client can report which weren't available.
|
||||
@@ -125,6 +129,9 @@ class DownloadJob:
|
||||
"completed_tracks": self.completed_tracks,
|
||||
"total_tracks": self.total_tracks,
|
||||
"active_tracks": self.active_tracks,
|
||||
"segments_done": self.segments_done,
|
||||
"segments_total": self.segments_total,
|
||||
"speed": self.speed,
|
||||
"skipped_subtitles": self.skipped_subtitles,
|
||||
}
|
||||
|
||||
@@ -148,25 +155,25 @@ class DownloadJob:
|
||||
return result
|
||||
|
||||
|
||||
def to_enum(values: List[str], enum_cls: type[Enum]) -> List[Enum]:
|
||||
"""Map case-insensitive strings (by member name or value) to enum members, dropping unknowns."""
|
||||
lookup = {m.name.upper(): m for m in enum_cls}
|
||||
lookup.update({str(m.value).upper(): m for m in enum_cls})
|
||||
return [lookup[v.upper()] for v in values if v.upper() in lookup]
|
||||
|
||||
|
||||
def _perform_download(
|
||||
job_id: str,
|
||||
service: str,
|
||||
title_id: str,
|
||||
params: Dict[str, Any],
|
||||
cancel_event: Optional[threading.Event] = None,
|
||||
progress_callback: Optional[Callable[[Dict[str, Any]], None]] = None,
|
||||
) -> List[str]:
|
||||
"""Execute the synchronous download logic for a job."""
|
||||
|
||||
def _check_cancel(stage: str):
|
||||
if cancel_event and cancel_event.is_set():
|
||||
raise Exception(f"Job was cancelled {stage}")
|
||||
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
from io import StringIO
|
||||
|
||||
_check_cancel("before execution started")
|
||||
|
||||
# Import dl.py components lazily to avoid circular deps during module import
|
||||
import click
|
||||
import yaml
|
||||
@@ -197,9 +204,7 @@ def _perform_download(
|
||||
if isinstance(vcodec_raw, str):
|
||||
vcodec_raw = [vcodec_raw]
|
||||
if isinstance(vcodec_raw, list) and vcodec_raw and not isinstance(vcodec_raw[0], Video.Codec):
|
||||
codec_map = {c.name.upper(): c for c in Video.Codec}
|
||||
codec_map.update({c.value.upper(): c for c in Video.Codec})
|
||||
params["vcodec"] = [codec_map[v.upper()] for v in vcodec_raw if v.upper() in codec_map]
|
||||
params["vcodec"] = to_enum(vcodec_raw, Video.Codec)
|
||||
else:
|
||||
params["vcodec"] = []
|
||||
|
||||
@@ -208,17 +213,14 @@ def _perform_download(
|
||||
if isinstance(range_raw, str):
|
||||
range_raw = [range_raw]
|
||||
if isinstance(range_raw, list) and range_raw and not isinstance(range_raw[0], Video.Range):
|
||||
range_map = {r.name.upper(): r for r in Video.Range}
|
||||
range_map.update({r.value.upper(): r for r in Video.Range})
|
||||
params["range"] = [range_map[r.upper()] for r in range_raw if r.upper() in range_map]
|
||||
params["range"] = to_enum(range_raw, Video.Range)
|
||||
else:
|
||||
params["range"] = [Video.Range.SDR]
|
||||
|
||||
sub_format_raw = params.get("sub_format")
|
||||
if sub_format_raw and isinstance(sub_format_raw, str):
|
||||
sub_map = {c.name.upper(): c for c in Subtitle.Codec}
|
||||
sub_map.update({c.value.upper(): c for c in Subtitle.Codec})
|
||||
params["sub_format"] = sub_map.get(sub_format_raw.upper())
|
||||
mapped = to_enum([sub_format_raw], Subtitle.Codec)
|
||||
params["sub_format"] = mapped[0] if mapped else None
|
||||
|
||||
if params.get("export"):
|
||||
params["export"] = bool(params["export"])
|
||||
@@ -321,8 +323,6 @@ def _perform_download(
|
||||
|
||||
service_module = Services.load(service)
|
||||
|
||||
_check_cancel("before service instantiation")
|
||||
|
||||
try:
|
||||
import inspect
|
||||
|
||||
@@ -360,29 +360,11 @@ def _perform_download(
|
||||
|
||||
original_download_dir = config.directories.downloads
|
||||
|
||||
_check_cancel("before download execution")
|
||||
|
||||
stdout_capture = StringIO()
|
||||
stderr_capture = StringIO()
|
||||
|
||||
# The progress_sink (dl.build_job_progress_callables) owns the percentage; status changes
|
||||
# are emitted here.
|
||||
if progress_callback:
|
||||
progress_callback({"progress": 0.0, "status": "starting"})
|
||||
original_result = dl_instance.result
|
||||
|
||||
def result_with_progress(*args, **kwargs):
|
||||
try:
|
||||
progress_callback({"status": "downloading"})
|
||||
result = original_result(*args, **kwargs)
|
||||
progress_callback({"progress": 100.0, "status": "completed"})
|
||||
return result
|
||||
except Exception as e:
|
||||
progress_callback({"progress": 0.0, "status": "failed", "error": str(e)})
|
||||
raise
|
||||
|
||||
dl_instance.result = result_with_progress
|
||||
|
||||
# progress_sink (dl.build_job_progress_callables) owns percentage; terminal status
|
||||
# (success/failure) is reported by the worker's result file.
|
||||
try:
|
||||
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
|
||||
dl_instance.result(
|
||||
@@ -756,6 +738,14 @@ class DownloadQueueManager:
|
||||
job.completed_tracks = int(progress_data["completed_tracks"])
|
||||
if "active_tracks" in progress_data:
|
||||
job.active_tracks = list(progress_data["active_tracks"])
|
||||
for _sk in ("segments_done", "segments_total"):
|
||||
if _sk in progress_data:
|
||||
try:
|
||||
setattr(job, _sk, float(progress_data[_sk]))
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
if progress_data.get("speed"):
|
||||
job.speed = str(progress_data["speed"])
|
||||
if progress_data.get("skipped_subtitles"):
|
||||
job.skipped_subtitles = progress_data["skipped_subtitles"]
|
||||
if "progress" in progress_data:
|
||||
|
||||
@@ -60,7 +60,7 @@ def main(argv: list[str]) -> int:
|
||||
log.error(f"Failed to write progress update: {e}")
|
||||
|
||||
output_files = _perform_download(
|
||||
job_id, service, title_id, params, cancel_event=None, progress_callback=progress_callback
|
||||
job_id, service, title_id, params, progress_callback=progress_callback
|
||||
)
|
||||
|
||||
result = {"status": "success", "output_files": output_files}
|
||||
|
||||
@@ -21,14 +21,10 @@ class APIErrorCode(str, Enum):
|
||||
# Client errors (4xx)
|
||||
INVALID_INPUT = "INVALID_INPUT" # Missing or malformed request data
|
||||
INVALID_SERVICE = "INVALID_SERVICE" # Unknown service name
|
||||
INVALID_TITLE_ID = "INVALID_TITLE_ID" # Invalid or malformed title ID
|
||||
INVALID_PROFILE = "INVALID_PROFILE" # Profile doesn't exist
|
||||
INVALID_PROXY = "INVALID_PROXY" # Invalid proxy specification
|
||||
INVALID_LANGUAGE = "INVALID_LANGUAGE" # Invalid language code
|
||||
INVALID_PARAMETERS = "INVALID_PARAMETERS" # Invalid download parameters
|
||||
|
||||
AUTH_FAILED = "AUTH_FAILED" # Authentication failure (invalid credentials/cookies)
|
||||
AUTH_REQUIRED = "AUTH_REQUIRED" # Missing authentication
|
||||
FORBIDDEN = "FORBIDDEN" # Action not allowed
|
||||
GEOFENCE = "GEOFENCE" # Content not available in region
|
||||
|
||||
@@ -84,13 +80,9 @@ class APIError(Exception):
|
||||
# 400 Bad Request
|
||||
APIErrorCode.INVALID_INPUT: 400,
|
||||
APIErrorCode.INVALID_SERVICE: 400,
|
||||
APIErrorCode.INVALID_TITLE_ID: 400,
|
||||
APIErrorCode.INVALID_PROFILE: 400,
|
||||
APIErrorCode.INVALID_PROXY: 400,
|
||||
APIErrorCode.INVALID_LANGUAGE: 400,
|
||||
APIErrorCode.INVALID_PARAMETERS: 400,
|
||||
# 401 Unauthorized
|
||||
APIErrorCode.AUTH_REQUIRED: 401,
|
||||
APIErrorCode.AUTH_FAILED: 401,
|
||||
# 403 Forbidden
|
||||
APIErrorCode.FORBIDDEN: 403,
|
||||
|
||||
+143
-257
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import enum
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from aiohttp import web
|
||||
@@ -200,6 +201,44 @@ def instantiate_service(
|
||||
return parent_ctx.invoke(service_module.cli, title=title, **extras)
|
||||
|
||||
|
||||
def setup_list_service(data: Dict[str, Any], normalized_service: str, profile: Optional[str], title_id: str) -> Any:
|
||||
"""Build and authenticate a service instance for list_titles / list_tracks.
|
||||
|
||||
Runs the shared preamble: load yaml → resolve proxy → load CDM → build ctx →
|
||||
instantiate → authenticate. Raises APIError on proxy failure.
|
||||
"""
|
||||
from unshackle.commands.dl import dl
|
||||
|
||||
service_config = load_service_yaml(normalized_service)
|
||||
|
||||
proxy_param = data.get("proxy")
|
||||
no_proxy = data.get("no_proxy", False)
|
||||
proxy_providers = []
|
||||
|
||||
if not no_proxy:
|
||||
proxy_providers = initialize_proxy_providers()
|
||||
|
||||
if proxy_param and not no_proxy:
|
||||
try:
|
||||
proxy_param = resolve_proxy(proxy_param, proxy_providers)
|
||||
except ValueError as e:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_PROXY,
|
||||
f"Proxy error: {e}",
|
||||
details={"proxy": proxy_param, "service": normalized_service},
|
||||
)
|
||||
|
||||
cdm = load_full_cdm(normalized_service, profile, data.get("cdm_type"))
|
||||
parent_ctx = build_parent_ctx(profile, cdm, proxy_param, no_proxy, proxy_providers, service_config)
|
||||
service_module = Services.load(normalized_service)
|
||||
service_instance = instantiate_service(parent_ctx, service_module, title_id, data, LIST_HANDLER_TRANSPORT_KEYS)
|
||||
|
||||
cookies = dl.get_cookie_jar(normalized_service, profile)
|
||||
credential = dl.get_credentials(normalized_service, profile)
|
||||
service_instance.authenticate(cookies, credential)
|
||||
return service_instance
|
||||
|
||||
|
||||
def get_allowed_services(request: Optional[web.Request] = None) -> Optional[List[str]]:
|
||||
"""Get effective service allowlist considering global + per-key config.
|
||||
|
||||
@@ -247,37 +286,50 @@ def validate_service(service_tag: str, request: Optional[web.Request] = None) ->
|
||||
return None
|
||||
|
||||
|
||||
def require_fields(data: Dict[str, Any], *names: str) -> None:
|
||||
"""Raise INVALID_INPUT for the first missing/falsy required field."""
|
||||
for name in names:
|
||||
if not data.get(name):
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
f"Missing required parameter: {name}",
|
||||
details={"missing_parameter": name},
|
||||
)
|
||||
|
||||
|
||||
def serialize_title(title: Title_T) -> Dict[str, Any]:
|
||||
"""Convert a title object to JSON-serializable dict."""
|
||||
title_language = str(title.language) if hasattr(title, "language") and title.language else None
|
||||
# Optional display metadata a service may provide: a synopsis (title.description) and a
|
||||
# release/air date or poster image URL stashed in title.data. Surfaced so a client can show
|
||||
# a richer listing without re-fetching the page.
|
||||
description = getattr(title, "description", None) or None
|
||||
_data = getattr(title, "data", None)
|
||||
date = _data.get("date") if isinstance(_data, dict) else None
|
||||
cover_url = _data.get("cover_url") if isinstance(_data, dict) else None
|
||||
|
||||
if isinstance(title, Episode):
|
||||
episode_name = title.name if title.name else f"Episode {title.number:02d}"
|
||||
result = {
|
||||
"type": "episode",
|
||||
"name": episode_name,
|
||||
"series_title": str(title.title),
|
||||
"season": title.season,
|
||||
"number": title.number,
|
||||
"year": title.year,
|
||||
"id": str(title.id) if hasattr(title, "id") else None,
|
||||
"language": title_language,
|
||||
}
|
||||
elif isinstance(title, Movie):
|
||||
result = {
|
||||
"type": "movie",
|
||||
"name": str(title.name) if hasattr(title, "name") else str(title),
|
||||
"year": title.year,
|
||||
"id": str(title.id) if hasattr(title, "id") else None,
|
||||
"language": title_language,
|
||||
}
|
||||
is_episode = isinstance(title, Episode)
|
||||
if is_episode:
|
||||
name = title.name if title.name else f"Episode {title.number:02d}"
|
||||
else:
|
||||
result = {
|
||||
"type": "other",
|
||||
"name": str(title.name) if hasattr(title, "name") else str(title),
|
||||
"id": str(title.id) if hasattr(title, "id") else None,
|
||||
"language": title_language,
|
||||
}
|
||||
name = str(title.name) if hasattr(title, "name") else str(title)
|
||||
|
||||
result = {
|
||||
"type": "episode" if is_episode else "movie" if isinstance(title, Movie) else "other",
|
||||
"name": name,
|
||||
"id": str(title.id) if hasattr(title, "id") else None,
|
||||
"language": title_language,
|
||||
"description": description,
|
||||
"date": date,
|
||||
"cover_url": cover_url,
|
||||
}
|
||||
# "other" titles carry no year; only Episode/Movie do.
|
||||
if isinstance(title, (Episode, Movie)):
|
||||
result["year"] = title.year
|
||||
if is_episode:
|
||||
result["series_title"] = str(title.title)
|
||||
result["season"] = title.season
|
||||
result["number"] = title.number
|
||||
|
||||
return result
|
||||
|
||||
@@ -346,46 +398,19 @@ def serialize_drm(drm_list) -> Optional[List[Dict[str, Any]]]:
|
||||
drm_class = drm.__class__.__name__
|
||||
drm_info["type"] = drm_class.lower()
|
||||
|
||||
# Get PSSH - handle both Widevine and PlayReady
|
||||
if hasattr(drm, "_pssh") and drm._pssh:
|
||||
pssh_obj = None
|
||||
# PSSH: pywidevine exposes dumps(); pyplayready's PSSH has no base64 method
|
||||
# here, so PlayReady omits the field (unchanged from prior behaviour).
|
||||
pssh_obj = getattr(drm, "_pssh", None)
|
||||
if pssh_obj is not None and hasattr(pssh_obj, "dumps"):
|
||||
try:
|
||||
pssh_obj = drm._pssh
|
||||
# Try to get base64 representation
|
||||
if hasattr(pssh_obj, "dumps"):
|
||||
# pywidevine PSSH has dumps() method
|
||||
drm_info["pssh"] = pssh_obj.dumps()
|
||||
elif hasattr(pssh_obj, "__bytes__"):
|
||||
# Convert to base64
|
||||
import base64
|
||||
|
||||
drm_info["pssh"] = base64.b64encode(bytes(pssh_obj)).decode()
|
||||
elif hasattr(pssh_obj, "to_base64"):
|
||||
drm_info["pssh"] = pssh_obj.to_base64()
|
||||
else:
|
||||
# Fallback - str() works for pywidevine PSSH
|
||||
pssh_str = str(pssh_obj)
|
||||
# Check if it's already base64-like or an object repr
|
||||
if not pssh_str.startswith("<"):
|
||||
drm_info["pssh"] = pssh_str
|
||||
drm_info["pssh"] = pssh_obj.dumps()
|
||||
except (ValueError, TypeError, KeyError):
|
||||
# Some PSSH implementations can fail to parse/serialize; log and continue.
|
||||
pssh_type = type(pssh_obj).__name__ if pssh_obj is not None else None
|
||||
log.warning(
|
||||
"Failed to extract/serialize PSSH for DRM type=%s pssh_type=%s",
|
||||
"Failed to serialize PSSH for DRM type=%s pssh_type=%s",
|
||||
drm_class,
|
||||
pssh_type,
|
||||
type(pssh_obj).__name__,
|
||||
exc_info=True,
|
||||
)
|
||||
except Exception:
|
||||
# Don't silently swallow unexpected failures; make them visible and propagate.
|
||||
pssh_type = type(pssh_obj).__name__ if pssh_obj is not None else None
|
||||
log.exception(
|
||||
"Unexpected error while extracting/serializing PSSH for DRM type=%s pssh_type=%s",
|
||||
drm_class,
|
||||
pssh_type,
|
||||
)
|
||||
raise
|
||||
|
||||
# Get KIDs
|
||||
if hasattr(drm, "kids") and drm.kids:
|
||||
@@ -406,15 +431,21 @@ def serialize_drm(drm_list) -> Optional[List[Dict[str, Any]]]:
|
||||
return result if result else None
|
||||
|
||||
|
||||
def enum_name(value: Any) -> str:
|
||||
"""Return an enum-like value's .name, falling back to str()."""
|
||||
return value.name if hasattr(value, "name") else str(value)
|
||||
|
||||
|
||||
def descriptor_name(track: Any) -> Optional[str]:
|
||||
"""Manifest descriptor (HLS/DASH/URL) name for a track, or None."""
|
||||
descriptor = getattr(track, "descriptor", None)
|
||||
return enum_name(descriptor) if descriptor else None
|
||||
|
||||
|
||||
def serialize_video_track(track: Video, include_url: bool = False) -> Dict[str, Any]:
|
||||
"""Convert video track to JSON-serializable dict."""
|
||||
codec_name = track.codec.name if hasattr(track.codec, "name") else str(track.codec)
|
||||
range_name = track.range.name if hasattr(track.range, "name") else str(track.range)
|
||||
|
||||
# Serialize the manifest descriptor (HLS, DASH, URL, etc.)
|
||||
descriptor_name = None
|
||||
if hasattr(track, "descriptor") and track.descriptor:
|
||||
descriptor_name = track.descriptor.name if hasattr(track.descriptor, "name") else str(track.descriptor)
|
||||
codec_name = enum_name(track.codec)
|
||||
range_name = enum_name(track.range)
|
||||
|
||||
result = {
|
||||
"id": str(track.id),
|
||||
@@ -429,7 +460,7 @@ def serialize_video_track(track: Video, include_url: bool = False) -> Dict[str,
|
||||
"range_display": DYNAMIC_RANGE_MAP.get(range_name, range_name),
|
||||
"language": str(track.language) if track.language else None,
|
||||
"drm": serialize_drm(track.drm) if hasattr(track, "drm") and track.drm else None,
|
||||
"descriptor": descriptor_name,
|
||||
"descriptor": descriptor_name(track),
|
||||
}
|
||||
if include_url and hasattr(track, "url") and track.url:
|
||||
result["url"] = str(track.url)
|
||||
@@ -438,12 +469,7 @@ def serialize_video_track(track: Video, include_url: bool = False) -> Dict[str,
|
||||
|
||||
def serialize_audio_track(track: Audio, include_url: bool = False) -> Dict[str, Any]:
|
||||
"""Convert audio track to JSON-serializable dict."""
|
||||
codec_name = track.codec.name if hasattr(track.codec, "name") else str(track.codec)
|
||||
|
||||
# Serialize the manifest descriptor (HLS, DASH, URL, etc.)
|
||||
descriptor_name = None
|
||||
if hasattr(track, "descriptor") and track.descriptor:
|
||||
descriptor_name = track.descriptor.name if hasattr(track.descriptor, "name") else str(track.descriptor)
|
||||
codec_name = enum_name(track.codec)
|
||||
|
||||
result = {
|
||||
"id": str(track.id),
|
||||
@@ -455,7 +481,7 @@ def serialize_audio_track(track: Audio, include_url: bool = False) -> Dict[str,
|
||||
"atmos": track.atmos if hasattr(track, "atmos") else False,
|
||||
"descriptive": track.descriptive if hasattr(track, "descriptive") else False,
|
||||
"drm": serialize_drm(track.drm) if hasattr(track, "drm") and track.drm else None,
|
||||
"descriptor": descriptor_name,
|
||||
"descriptor": descriptor_name(track),
|
||||
}
|
||||
if include_url and hasattr(track, "url") and track.url:
|
||||
result["url"] = str(track.url)
|
||||
@@ -464,19 +490,14 @@ def serialize_audio_track(track: Audio, include_url: bool = False) -> Dict[str,
|
||||
|
||||
def serialize_subtitle_track(track: Subtitle, include_url: bool = False) -> Dict[str, Any]:
|
||||
"""Convert subtitle track to JSON-serializable dict."""
|
||||
# Get descriptor for compatibility
|
||||
descriptor_name = None
|
||||
if hasattr(track, "descriptor") and track.descriptor:
|
||||
descriptor_name = track.descriptor.name if hasattr(track.descriptor, "name") else str(track.descriptor)
|
||||
|
||||
result = {
|
||||
"id": str(track.id),
|
||||
"codec": track.codec.name if hasattr(track.codec, "name") else str(track.codec),
|
||||
"codec": enum_name(track.codec),
|
||||
"language": str(track.language) if track.language else None,
|
||||
"forced": track.forced if hasattr(track, "forced") else False,
|
||||
"sdh": track.sdh if hasattr(track, "sdh") else False,
|
||||
"cc": track.cc if hasattr(track, "cc") else False,
|
||||
"descriptor": descriptor_name,
|
||||
"descriptor": descriptor_name(track),
|
||||
}
|
||||
if include_url and hasattr(track, "url") and track.url:
|
||||
result["url"] = str(track.url)
|
||||
@@ -575,24 +596,11 @@ async def search_handler(data: Dict[str, Any], request: Optional[web.Request] =
|
||||
|
||||
async def list_titles_handler(data: Dict[str, Any], request: Optional[web.Request] = None) -> web.Response:
|
||||
"""Handle list-titles request."""
|
||||
require_fields(data, "service", "title_id")
|
||||
service_tag = data.get("service")
|
||||
title_id = data.get("title_id")
|
||||
profile = data.get("profile")
|
||||
|
||||
if not service_tag:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
"Missing required parameter: service",
|
||||
details={"missing_parameter": "service"},
|
||||
)
|
||||
|
||||
if not title_id:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
"Missing required parameter: title_id",
|
||||
details={"missing_parameter": "title_id"},
|
||||
)
|
||||
|
||||
normalized_service = validate_service(service_tag, request)
|
||||
if not normalized_service:
|
||||
raise APIError(
|
||||
@@ -602,37 +610,7 @@ async def list_titles_handler(data: Dict[str, Any], request: Optional[web.Reques
|
||||
)
|
||||
|
||||
try:
|
||||
from unshackle.commands.dl import dl
|
||||
|
||||
service_config = load_service_yaml(normalized_service)
|
||||
|
||||
proxy_param = data.get("proxy")
|
||||
no_proxy = data.get("no_proxy", False)
|
||||
proxy_providers = []
|
||||
|
||||
if not no_proxy:
|
||||
proxy_providers = initialize_proxy_providers()
|
||||
|
||||
if proxy_param and not no_proxy:
|
||||
try:
|
||||
resolved_proxy = resolve_proxy(proxy_param, proxy_providers)
|
||||
proxy_param = resolved_proxy
|
||||
except ValueError as e:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_PROXY,
|
||||
f"Proxy error: {e}",
|
||||
details={"proxy": proxy_param, "service": normalized_service},
|
||||
)
|
||||
|
||||
cdm = load_full_cdm(normalized_service, profile, data.get("cdm_type"))
|
||||
parent_ctx = build_parent_ctx(profile, cdm, proxy_param, no_proxy, proxy_providers, service_config)
|
||||
service_module = Services.load(normalized_service)
|
||||
service_instance = instantiate_service(parent_ctx, service_module, title_id, data, LIST_HANDLER_TRANSPORT_KEYS)
|
||||
|
||||
cookies = dl.get_cookie_jar(normalized_service, profile)
|
||||
credential = dl.get_credentials(normalized_service, profile)
|
||||
service_instance.authenticate(cookies, credential)
|
||||
|
||||
service_instance = setup_list_service(data, normalized_service, profile, title_id)
|
||||
titles = service_instance.get_titles()
|
||||
|
||||
if hasattr(titles, "__iter__") and not isinstance(titles, str):
|
||||
@@ -656,24 +634,11 @@ async def list_titles_handler(data: Dict[str, Any], request: Optional[web.Reques
|
||||
|
||||
async def list_tracks_handler(data: Dict[str, Any], request: Optional[web.Request] = None) -> web.Response:
|
||||
"""Handle list-tracks request."""
|
||||
require_fields(data, "service", "title_id")
|
||||
service_tag = data.get("service")
|
||||
title_id = data.get("title_id")
|
||||
profile = data.get("profile")
|
||||
|
||||
if not service_tag:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
"Missing required parameter: service",
|
||||
details={"missing_parameter": "service"},
|
||||
)
|
||||
|
||||
if not title_id:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
"Missing required parameter: title_id",
|
||||
details={"missing_parameter": "title_id"},
|
||||
)
|
||||
|
||||
normalized_service = validate_service(service_tag, request)
|
||||
if not normalized_service:
|
||||
raise APIError(
|
||||
@@ -683,37 +648,7 @@ async def list_tracks_handler(data: Dict[str, Any], request: Optional[web.Reques
|
||||
)
|
||||
|
||||
try:
|
||||
from unshackle.commands.dl import dl
|
||||
|
||||
service_config = load_service_yaml(normalized_service)
|
||||
|
||||
proxy_param = data.get("proxy")
|
||||
no_proxy = data.get("no_proxy", False)
|
||||
proxy_providers = []
|
||||
|
||||
if not no_proxy:
|
||||
proxy_providers = initialize_proxy_providers()
|
||||
|
||||
if proxy_param and not no_proxy:
|
||||
try:
|
||||
resolved_proxy = resolve_proxy(proxy_param, proxy_providers)
|
||||
proxy_param = resolved_proxy
|
||||
except ValueError as e:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_PROXY,
|
||||
f"Proxy error: {e}",
|
||||
details={"proxy": proxy_param, "service": normalized_service},
|
||||
)
|
||||
|
||||
cdm = load_full_cdm(normalized_service, profile, data.get("cdm_type"))
|
||||
parent_ctx = build_parent_ctx(profile, cdm, proxy_param, no_proxy, proxy_providers, service_config)
|
||||
service_module = Services.load(normalized_service)
|
||||
service_instance = instantiate_service(parent_ctx, service_module, title_id, data, LIST_HANDLER_TRANSPORT_KEYS)
|
||||
|
||||
cookies = dl.get_cookie_jar(normalized_service, profile)
|
||||
credential = dl.get_credentials(normalized_service, profile)
|
||||
service_instance.authenticate(cookies, credential)
|
||||
|
||||
service_instance = setup_list_service(data, normalized_service, profile, title_id)
|
||||
titles = service_instance.get_titles()
|
||||
|
||||
wanted_param = data.get("wanted")
|
||||
@@ -855,6 +790,25 @@ async def list_tracks_handler(data: Dict[str, Any], request: Optional[web.Reques
|
||||
)
|
||||
|
||||
|
||||
VALID_VCODECS = ["H264", "H265", "H.264", "H.265", "AVC", "HEVC", "VC1", "VC-1", "VP8", "VP9", "AV1"]
|
||||
VALID_ACODECS = ["AAC", "AC3", "EC3", "EAC3", "DD", "DD+", "AC4", "OPUS", "FLAC", "ALAC", "VORBIS", "OGG", "DTS"]
|
||||
|
||||
|
||||
def check_codec(value: Any, allowed: List[str], name: str) -> Optional[str]:
|
||||
"""Validate a comma-string or list of codec tokens against `allowed` (case-insensitive)."""
|
||||
if isinstance(value, str):
|
||||
tokens = [v.strip() for v in value.split(",") if v.strip()]
|
||||
elif isinstance(value, list):
|
||||
tokens = [str(v).strip() for v in value if str(v).strip()]
|
||||
else:
|
||||
return f"{name} must be a string or list"
|
||||
|
||||
invalid = [token for token in tokens if token.upper() not in allowed]
|
||||
if invalid:
|
||||
return f"Invalid {name}: {', '.join(invalid)}. Must be one of: {', '.join(allowed)}"
|
||||
return None
|
||||
|
||||
|
||||
def validate_download_parameters(data: Dict[str, Any]) -> Optional[str]:
|
||||
"""
|
||||
Validate download parameters and return error message if invalid.
|
||||
@@ -863,44 +817,14 @@ def validate_download_parameters(data: Dict[str, Any]) -> Optional[str]:
|
||||
None if valid, error message string if invalid
|
||||
"""
|
||||
if "vcodec" in data and data["vcodec"]:
|
||||
valid_vcodecs = ["H264", "H265", "H.264", "H.265", "AVC", "HEVC", "VC1", "VC-1", "VP8", "VP9", "AV1"]
|
||||
if isinstance(data["vcodec"], str):
|
||||
vcodec_values = [v.strip() for v in data["vcodec"].split(",") if v.strip()]
|
||||
elif isinstance(data["vcodec"], list):
|
||||
vcodec_values = [str(v).strip() for v in data["vcodec"] if str(v).strip()]
|
||||
else:
|
||||
return "vcodec must be a string or list"
|
||||
|
||||
invalid = [value for value in vcodec_values if value.upper() not in valid_vcodecs]
|
||||
if invalid:
|
||||
return f"Invalid vcodec: {', '.join(invalid)}. Must be one of: {', '.join(valid_vcodecs)}"
|
||||
err = check_codec(data["vcodec"], VALID_VCODECS, "vcodec")
|
||||
if err:
|
||||
return err
|
||||
|
||||
if "acodec" in data and data["acodec"]:
|
||||
valid_acodecs = [
|
||||
"AAC",
|
||||
"AC3",
|
||||
"EC3",
|
||||
"EAC3",
|
||||
"DD",
|
||||
"DD+",
|
||||
"AC4",
|
||||
"OPUS",
|
||||
"FLAC",
|
||||
"ALAC",
|
||||
"VORBIS",
|
||||
"OGG",
|
||||
"DTS",
|
||||
]
|
||||
if isinstance(data["acodec"], str):
|
||||
acodec_values = [v.strip() for v in data["acodec"].split(",") if v.strip()]
|
||||
elif isinstance(data["acodec"], list):
|
||||
acodec_values = [str(v).strip() for v in data["acodec"] if str(v).strip()]
|
||||
else:
|
||||
return "acodec must be a string or list"
|
||||
|
||||
invalid = [value for value in acodec_values if value.upper() not in valid_acodecs]
|
||||
if invalid:
|
||||
return f"Invalid acodec: {', '.join(invalid)}. Must be one of: {', '.join(valid_acodecs)}"
|
||||
err = check_codec(data["acodec"], VALID_ACODECS, "acodec")
|
||||
if err:
|
||||
return err
|
||||
|
||||
if "sub_format" in data and data["sub_format"]:
|
||||
valid_sub_formats = ["SRT", "VTT", "ASS", "SSA", "TTML", "STPP", "WVTT", "SMI", "SUB", "MPL2", "TMP"]
|
||||
@@ -972,23 +896,10 @@ async def download_handler(data: Dict[str, Any], request: Optional[web.Request]
|
||||
"""Handle download request - create and queue a download job."""
|
||||
from unshackle.core.api.download_manager import get_download_manager
|
||||
|
||||
require_fields(data, "service", "title_id")
|
||||
service_tag = data.get("service")
|
||||
title_id = data.get("title_id")
|
||||
|
||||
if not service_tag:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
"Missing required parameter: service",
|
||||
details={"missing_parameter": "service"},
|
||||
)
|
||||
|
||||
if not title_id:
|
||||
raise APIError(
|
||||
APIErrorCode.INVALID_INPUT,
|
||||
"Missing required parameter: title_id",
|
||||
details={"missing_parameter": "title_id"},
|
||||
)
|
||||
|
||||
normalized_service = validate_service(service_tag, request)
|
||||
if not normalized_service:
|
||||
raise APIError(
|
||||
@@ -1426,7 +1337,6 @@ async def session_create_handler(data: Dict[str, Any], request: Optional[web.Req
|
||||
session.auth_status = AuthStatus.FAILED
|
||||
session.auth_error = str(e)
|
||||
bridge.status = AuthStatus.FAILED
|
||||
bridge.error = str(e)
|
||||
|
||||
asyncio.create_task(_run_auth())
|
||||
|
||||
@@ -1671,31 +1581,12 @@ async def session_segments_handler(
|
||||
else:
|
||||
track_info["cookies"] = {}
|
||||
|
||||
# Include manifest-specific data for segment resolution
|
||||
# Include manifest-specific data for segment resolution. Round-trip through
|
||||
# JSON so any non-serializable value becomes its str() (default=str).
|
||||
if hasattr(track, "data") and track.data:
|
||||
track_data = {}
|
||||
for key, val in track.data.items():
|
||||
if isinstance(val, dict):
|
||||
# Convert non-serializable values
|
||||
serializable = {}
|
||||
for k, v in val.items():
|
||||
try:
|
||||
import json
|
||||
import json
|
||||
|
||||
json.dumps(v)
|
||||
serializable[k] = v
|
||||
except (TypeError, ValueError):
|
||||
serializable[k] = str(v)
|
||||
track_data[key] = serializable
|
||||
else:
|
||||
try:
|
||||
import json
|
||||
|
||||
json.dumps(val)
|
||||
track_data[key] = val
|
||||
except (TypeError, ValueError):
|
||||
track_data[key] = str(val)
|
||||
track_info["data"] = track_data
|
||||
track_info["data"] = json.loads(json.dumps(track.data, default=str))
|
||||
else:
|
||||
track_info["data"] = {}
|
||||
|
||||
@@ -1715,15 +1606,10 @@ async def session_segments_handler(
|
||||
)
|
||||
|
||||
|
||||
class _CdmTypeStub:
|
||||
"""Lightweight CDM stub so ``is_playready_cdm()`` can detect CDM type.
|
||||
|
||||
Used on the server when the client sends ``cdm_type`` but the server
|
||||
does not need a full CDM (e.g. for cache key / device selection only).
|
||||
"""
|
||||
|
||||
def __init__(self, cdm_type: str) -> None:
|
||||
self.is_playready = cdm_type == "playready"
|
||||
def _cdm_type_stub(cdm_type: str) -> SimpleNamespace:
|
||||
"""Lightweight CDM stand-in so ``is_playready_cdm()`` (reads ``.is_playready``)
|
||||
can detect the type without loading a full CDM."""
|
||||
return SimpleNamespace(is_playready=cdm_type == "playready")
|
||||
|
||||
|
||||
def _resolve_server_cdm(service: str, profile: Optional[str], cdm_type: Optional[str]) -> Optional[Any]:
|
||||
@@ -1751,10 +1637,10 @@ def _resolve_server_cdm(service: str, profile: Optional[str], cdm_type: Optional
|
||||
if cdm_name and isinstance(cdm_name, str):
|
||||
detected_type = _detect_cdm_type(cdm_name, app_config)
|
||||
if detected_type:
|
||||
return _CdmTypeStub(detected_type)
|
||||
return _cdm_type_stub(detected_type)
|
||||
|
||||
if cdm_type:
|
||||
return _CdmTypeStub(cdm_type)
|
||||
return _cdm_type_stub(cdm_type)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ response value.
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
@@ -25,8 +24,9 @@ class AuthStatus(Enum):
|
||||
FAILED = "failed"
|
||||
|
||||
|
||||
class BridgeCancelledError(Exception):
|
||||
"""Raised when the bridge is cancelled (client disconnected, session deleted)."""
|
||||
# Seconds the server waits for a remote client to answer an input prompt before
|
||||
# giving up; also the TTL SessionStore grants AUTHENTICATING/PENDING_INPUT sessions.
|
||||
AUTH_INPUT_TIMEOUT = 600.0
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -40,12 +40,11 @@ class InputBridge:
|
||||
_prompt: Optional[str] = field(default=None, init=False, repr=False)
|
||||
_response: Optional[str] = field(default=None, init=False, repr=False)
|
||||
_status: AuthStatus = field(default=AuthStatus.AUTHENTICATING, init=False)
|
||||
_error: Optional[str] = field(default=None, init=False, repr=False)
|
||||
_cancelled: bool = field(default=False, init=False, repr=False)
|
||||
_response_ready: threading.Event = field(default_factory=threading.Event, init=False, repr=False)
|
||||
_lock: threading.Lock = field(default_factory=threading.Lock, init=False, repr=False)
|
||||
|
||||
def request_input(self, prompt: str, timeout: float = 600.0) -> str:
|
||||
def request_input(self, prompt: str, timeout: float = AUTH_INPUT_TIMEOUT) -> str:
|
||||
"""Block until the remote client submits a response for *prompt*.
|
||||
|
||||
Args:
|
||||
@@ -57,32 +56,26 @@ class InputBridge:
|
||||
|
||||
Raises:
|
||||
TimeoutError: If no response is received within *timeout*.
|
||||
BridgeCancelledError: If the bridge was cancelled.
|
||||
RuntimeError: If the bridge was cancelled.
|
||||
"""
|
||||
with self._lock:
|
||||
if self._cancelled:
|
||||
raise BridgeCancelledError("Session was cancelled")
|
||||
raise RuntimeError("Session was cancelled")
|
||||
self._prompt = prompt
|
||||
self._response = None
|
||||
self._status = AuthStatus.PENDING_INPUT
|
||||
self._response_ready.clear()
|
||||
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
if self._response_ready.wait(timeout=0.5):
|
||||
break
|
||||
with self._lock:
|
||||
if self._cancelled:
|
||||
raise BridgeCancelledError("Session was cancelled")
|
||||
else:
|
||||
# cancel() and submit_response() both .set() the event, so a single wait
|
||||
# returns on signal-or-cancel; no poll loop needed.
|
||||
if not self._response_ready.wait(timeout=timeout):
|
||||
with self._lock:
|
||||
self._status = AuthStatus.FAILED
|
||||
self._error = "Input request timed out waiting for client response"
|
||||
raise TimeoutError(f"No client response for prompt within {timeout}s")
|
||||
|
||||
with self._lock:
|
||||
if self._cancelled:
|
||||
raise BridgeCancelledError("Session was cancelled")
|
||||
raise RuntimeError("Session was cancelled")
|
||||
response = self._response or ""
|
||||
self._prompt = None
|
||||
self._response = None
|
||||
@@ -115,7 +108,6 @@ class InputBridge:
|
||||
with self._lock:
|
||||
self._cancelled = True
|
||||
self._status = AuthStatus.FAILED
|
||||
self._error = "Session cancelled"
|
||||
self._response_ready.set()
|
||||
|
||||
@property
|
||||
@@ -127,13 +119,3 @@ class InputBridge:
|
||||
def status(self, value: AuthStatus) -> None:
|
||||
with self._lock:
|
||||
self._status = value
|
||||
|
||||
@property
|
||||
def error(self) -> Optional[str]:
|
||||
with self._lock:
|
||||
return self._error
|
||||
|
||||
@error.setter
|
||||
def error(self, value: Optional[str]) -> None:
|
||||
with self._lock:
|
||||
self._error = value
|
||||
|
||||
@@ -6,7 +6,7 @@ a bitrate-weighted percentage, track counts, and the labels of the tracks downlo
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from unshackle.core.constants import AnyTrack
|
||||
|
||||
@@ -70,6 +70,9 @@ def build_job_progress_callables(
|
||||
fractions = [0.0] * total
|
||||
done = [False] * total
|
||||
started = [False] * total
|
||||
seg_done = [0.0] * total
|
||||
seg_total = [0.0] * total
|
||||
speeds: list[Optional[str]] = [None] * total
|
||||
|
||||
def emit() -> None:
|
||||
completed = sum(done)
|
||||
@@ -82,6 +85,8 @@ def build_job_progress_callables(
|
||||
phase += f" (+{len(active) - 3} more)"
|
||||
else:
|
||||
phase = f"downloading {completed}/{total} tracks"
|
||||
# segment counts and transfer speed of the track downloading now, for a granular display
|
||||
active_i = next((i for i in range(total) if started[i] and not done[i]), None)
|
||||
sink(
|
||||
{
|
||||
"progress": progress,
|
||||
@@ -89,6 +94,9 @@ def build_job_progress_callables(
|
||||
"completed_tracks": completed,
|
||||
"total_tracks": total,
|
||||
"active_tracks": active,
|
||||
"segments_done": seg_done[active_i] if active_i is not None else 0.0,
|
||||
"segments_total": seg_total[active_i] if active_i is not None else 0.0,
|
||||
"speed": speeds[active_i] if active_i is not None else None,
|
||||
"status": "downloading",
|
||||
}
|
||||
)
|
||||
@@ -104,6 +112,11 @@ def build_job_progress_callables(
|
||||
counts["completed"] = kwargs["completed"]
|
||||
if "advance" in kwargs:
|
||||
counts["completed"] += kwargs["advance"]
|
||||
seg_done[index] = counts["completed"]
|
||||
seg_total[index] = counts["total"]
|
||||
_dl = kwargs.get("downloaded")
|
||||
if isinstance(_dl, str) and _dl.endswith("/s"):
|
||||
speeds[index] = _dl
|
||||
if kwargs.get("downloaded") in JOB_PROGRESS_TERMINAL_STATES:
|
||||
done[index] = True
|
||||
fractions[index] = 1.0
|
||||
|
||||
+91
-118
@@ -1,5 +1,8 @@
|
||||
import functools
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any
|
||||
|
||||
import click
|
||||
from aiohttp import web
|
||||
@@ -18,9 +21,12 @@ from unshackle.core.update_checker import UpdateChecker
|
||||
|
||||
|
||||
@web.middleware
|
||||
async def cors_middleware(request: web.Request, handler):
|
||||
async def cors_middleware(
|
||||
request: web.Request, handler: Callable[[web.Request], Awaitable[web.StreamResponse]]
|
||||
) -> web.StreamResponse:
|
||||
"""Add CORS headers to all responses."""
|
||||
# Handle preflight requests
|
||||
response: web.StreamResponse
|
||||
if request.method == "OPTIONS":
|
||||
response = web.Response()
|
||||
else:
|
||||
@@ -37,7 +43,24 @@ async def cors_middleware(request: web.Request, handler):
|
||||
|
||||
log = logging.getLogger("api")
|
||||
|
||||
# Route handler signature: takes the request, returns a response.
|
||||
Handler = Callable[[web.Request], Awaitable[web.Response]]
|
||||
|
||||
|
||||
def api_handler(handler: Handler) -> Handler:
|
||||
"""Wrap a route handler so any raised APIError becomes a structured error response."""
|
||||
|
||||
@functools.wraps(handler)
|
||||
async def wrapper(request: web.Request) -> web.Response:
|
||||
try:
|
||||
return await handler(request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@api_handler
|
||||
async def health(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Health check endpoint.
|
||||
@@ -84,6 +107,7 @@ async def health(request: web.Request) -> web.Response:
|
||||
return web.json_response({"status": "ok", "version": __version__, "update_check": update_info})
|
||||
|
||||
|
||||
@api_handler
|
||||
async def services(request: web.Request) -> web.Response:
|
||||
"""
|
||||
List available services.
|
||||
@@ -161,7 +185,14 @@ async def services(request: web.Request) -> web.Response:
|
||||
services_info = []
|
||||
|
||||
for tag in service_tags:
|
||||
service_data = {"tag": tag, "aliases": [], "geofence": [], "title_regex": None, "url": None, "help": None}
|
||||
service_data: dict[str, Any] = {
|
||||
"tag": tag,
|
||||
"aliases": [],
|
||||
"geofence": [],
|
||||
"title_regex": None,
|
||||
"url": None,
|
||||
"help": None,
|
||||
}
|
||||
|
||||
try:
|
||||
service_module = Services.load(tag)
|
||||
@@ -268,6 +299,7 @@ async def services(request: web.Request) -> web.Response:
|
||||
return handle_api_exception(e, context={"operation": "list_services"}, debug_mode=debug_mode)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def search(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Search for titles from a service.
|
||||
@@ -347,14 +379,13 @@ async def search(request: web.Request) -> web.Response:
|
||||
|
||||
try:
|
||||
return await search_handler(data, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in search")
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return handle_api_exception(e, context={"operation": "search"}, debug_mode=debug_mode)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def list_titles(request: web.Request) -> web.Response:
|
||||
"""
|
||||
List titles for a service and title ID.
|
||||
@@ -474,13 +505,10 @@ async def list_titles(request: web.Request) -> web.Response:
|
||||
request.app.get("debug_api", False),
|
||||
)
|
||||
|
||||
try:
|
||||
return await list_titles_handler(data, request)
|
||||
except APIError as e:
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return build_error_response(e, debug_mode)
|
||||
return await list_titles_handler(data, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def list_tracks(request: web.Request) -> web.Response:
|
||||
"""
|
||||
List tracks for a title, separated by type.
|
||||
@@ -527,13 +555,10 @@ async def list_tracks(request: web.Request) -> web.Response:
|
||||
request.app.get("debug_api", False),
|
||||
)
|
||||
|
||||
try:
|
||||
return await list_tracks_handler(data, request)
|
||||
except APIError as e:
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return build_error_response(e, debug_mode)
|
||||
return await list_tracks_handler(data, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def download(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Download content based on provided parameters.
|
||||
@@ -767,13 +792,10 @@ async def download(request: web.Request) -> web.Response:
|
||||
request.app.get("debug_api", False),
|
||||
)
|
||||
|
||||
try:
|
||||
return await download_handler(data, request)
|
||||
except APIError as e:
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return build_error_response(e, debug_mode)
|
||||
return await download_handler(data, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def download_jobs(request: web.Request) -> web.Response:
|
||||
"""
|
||||
List all download jobs with optional filtering and sorting.
|
||||
@@ -847,13 +869,10 @@ async def download_jobs(request: web.Request) -> web.Response:
|
||||
"sort_by": request.query.get("sort_by", "created_time"),
|
||||
"sort_order": request.query.get("sort_order", "desc"),
|
||||
}
|
||||
try:
|
||||
return await list_download_jobs_handler(query_params, request)
|
||||
except APIError as e:
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return build_error_response(e, debug_mode)
|
||||
return await list_download_jobs_handler(query_params, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def download_job_detail(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Get download job details.
|
||||
@@ -875,13 +894,10 @@ async def download_job_detail(request: web.Request) -> web.Response:
|
||||
description: Server error
|
||||
"""
|
||||
job_id = request.match_info["job_id"]
|
||||
try:
|
||||
return await get_download_job_handler(job_id, request)
|
||||
except APIError as e:
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return build_error_response(e, debug_mode)
|
||||
return await get_download_job_handler(job_id, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def cancel_download_job(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Cancel download job.
|
||||
@@ -905,13 +921,10 @@ async def cancel_download_job(request: web.Request) -> web.Response:
|
||||
description: Server error
|
||||
"""
|
||||
job_id = request.match_info["job_id"]
|
||||
try:
|
||||
return await cancel_download_job_handler(job_id, request)
|
||||
except APIError as e:
|
||||
debug_mode = request.app.get("debug_api", False)
|
||||
return build_error_response(e, debug_mode)
|
||||
return await cancel_download_job_handler(job_id, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_create(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Create a remote-dl session.
|
||||
@@ -964,8 +977,6 @@ async def session_create(request: web.Request) -> web.Response:
|
||||
)
|
||||
try:
|
||||
return await session_create_handler(data, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session create")
|
||||
return handle_api_exception(
|
||||
@@ -973,6 +984,7 @@ async def session_create(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_titles(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Get titles for an authenticated session.
|
||||
@@ -994,8 +1006,6 @@ async def session_titles(request: web.Request) -> web.Response:
|
||||
session_id = request.match_info["session_id"]
|
||||
try:
|
||||
return await session_titles_handler(session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session titles")
|
||||
return handle_api_exception(
|
||||
@@ -1003,6 +1013,7 @@ async def session_titles(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_tracks(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Get tracks and chapters for a specific title.
|
||||
@@ -1043,8 +1054,6 @@ async def session_tracks(request: web.Request) -> web.Response:
|
||||
)
|
||||
try:
|
||||
return await session_tracks_handler(data, session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session tracks")
|
||||
return handle_api_exception(
|
||||
@@ -1052,6 +1061,7 @@ async def session_tracks(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_segments(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Resolve segment URLs for selected tracks.
|
||||
@@ -1094,8 +1104,6 @@ async def session_segments(request: web.Request) -> web.Response:
|
||||
)
|
||||
try:
|
||||
return await session_segments_handler(data, session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session segments")
|
||||
return handle_api_exception(
|
||||
@@ -1103,6 +1111,7 @@ async def session_segments(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_license(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Proxy DRM license through authenticated service.
|
||||
@@ -1151,8 +1160,6 @@ async def session_license(request: web.Request) -> web.Response:
|
||||
)
|
||||
try:
|
||||
return await session_license_handler(data, session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session license")
|
||||
return handle_api_exception(
|
||||
@@ -1160,6 +1167,7 @@ async def session_license(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_info(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Get session info.
|
||||
@@ -1179,12 +1187,10 @@ async def session_info(request: web.Request) -> web.Response:
|
||||
description: Session not found
|
||||
"""
|
||||
session_id = request.match_info["session_id"]
|
||||
try:
|
||||
return await session_info_handler(session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
return await session_info_handler(session_id, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_delete(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Delete a session.
|
||||
@@ -1204,12 +1210,10 @@ async def session_delete(request: web.Request) -> web.Response:
|
||||
description: Session not found
|
||||
"""
|
||||
session_id = request.match_info["session_id"]
|
||||
try:
|
||||
return await session_delete_handler(session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
return await session_delete_handler(session_id, request)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_prompt_get(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Poll for pending interactive prompts during authentication.
|
||||
@@ -1245,8 +1249,6 @@ async def session_prompt_get(request: web.Request) -> web.Response:
|
||||
session_id = request.match_info["session_id"]
|
||||
try:
|
||||
return await session_prompt_get_handler(session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session prompt get")
|
||||
return handle_api_exception(
|
||||
@@ -1254,6 +1256,7 @@ async def session_prompt_get(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
@api_handler
|
||||
async def session_prompt_submit(request: web.Request) -> web.Response:
|
||||
"""
|
||||
Submit a response to a pending interactive prompt.
|
||||
@@ -1304,8 +1307,6 @@ async def session_prompt_submit(request: web.Request) -> web.Response:
|
||||
)
|
||||
try:
|
||||
return await session_prompt_post_handler(data, session_id, request)
|
||||
except APIError as e:
|
||||
return build_error_response(e, request.app.get("debug_api", False))
|
||||
except Exception as e:
|
||||
log.exception("Error in session prompt submit")
|
||||
return handle_api_exception(
|
||||
@@ -1313,48 +1314,42 @@ async def session_prompt_submit(request: web.Request) -> web.Response:
|
||||
)
|
||||
|
||||
|
||||
def _setup_remote_session_routes(app: web.Application) -> None:
|
||||
"""Setup remote-DL session endpoints only."""
|
||||
app.router.add_get("/api/health", health)
|
||||
app.router.add_get("/api/services", services)
|
||||
app.router.add_post("/api/search", search)
|
||||
app.router.add_post("/api/session/create", session_create)
|
||||
app.router.add_get("/api/session/{session_id}/titles", session_titles)
|
||||
app.router.add_post("/api/session/{session_id}/tracks", session_tracks)
|
||||
app.router.add_post("/api/session/{session_id}/segments", session_segments)
|
||||
app.router.add_post("/api/session/{session_id}/license", session_license)
|
||||
app.router.add_get("/api/session/{session_id}/prompt", session_prompt_get)
|
||||
app.router.add_post("/api/session/{session_id}/prompt", session_prompt_submit)
|
||||
app.router.add_get("/api/session/{session_id}", session_info)
|
||||
app.router.add_delete("/api/session/{session_id}", session_delete)
|
||||
# Single source of truth for all API routes. `remote` marks endpoints exposed in
|
||||
# --remote-only server mode; full mode registers every route. Both setup_routes and
|
||||
# setup_swagger derive from this table so the live routes and the docs cannot drift.
|
||||
ROUTES: list[tuple[str, str, Handler, bool]] = [
|
||||
("GET", "/api/health", health, True),
|
||||
("GET", "/api/services", services, True),
|
||||
("POST", "/api/search", search, True),
|
||||
("POST", "/api/list-titles", list_titles, False),
|
||||
("POST", "/api/list-tracks", list_tracks, False),
|
||||
("POST", "/api/download", download, False),
|
||||
("GET", "/api/download/jobs", download_jobs, False),
|
||||
("GET", "/api/download/jobs/{job_id}", download_job_detail, False),
|
||||
("DELETE", "/api/download/jobs/{job_id}", cancel_download_job, False),
|
||||
("POST", "/api/session/create", session_create, True),
|
||||
("GET", "/api/session/{session_id}/titles", session_titles, True),
|
||||
("POST", "/api/session/{session_id}/tracks", session_tracks, True),
|
||||
("POST", "/api/session/{session_id}/segments", session_segments, True),
|
||||
("POST", "/api/session/{session_id}/license", session_license, True),
|
||||
("GET", "/api/session/{session_id}/prompt", session_prompt_get, True),
|
||||
("POST", "/api/session/{session_id}/prompt", session_prompt_submit, True),
|
||||
("GET", "/api/session/{session_id}", session_info, True),
|
||||
("DELETE", "/api/session/{session_id}", session_delete, True),
|
||||
]
|
||||
|
||||
|
||||
def setup_routes(app: web.Application, remote_only: bool = False) -> None:
|
||||
"""Setup API routes. When remote_only=True, only expose remote session endpoints."""
|
||||
if remote_only:
|
||||
_setup_remote_session_routes(app)
|
||||
return
|
||||
|
||||
app.router.add_get("/api/health", health)
|
||||
app.router.add_get("/api/services", services)
|
||||
app.router.add_post("/api/search", search)
|
||||
app.router.add_post("/api/list-titles", list_titles)
|
||||
app.router.add_post("/api/list-tracks", list_tracks)
|
||||
app.router.add_post("/api/download", download)
|
||||
app.router.add_get("/api/download/jobs", download_jobs)
|
||||
app.router.add_get("/api/download/jobs/{job_id}", download_job_detail)
|
||||
app.router.add_delete("/api/download/jobs/{job_id}", cancel_download_job)
|
||||
|
||||
# Remote-DL session endpoints
|
||||
app.router.add_post("/api/session/create", session_create)
|
||||
app.router.add_get("/api/session/{session_id}/titles", session_titles)
|
||||
app.router.add_post("/api/session/{session_id}/tracks", session_tracks)
|
||||
app.router.add_post("/api/session/{session_id}/segments", session_segments)
|
||||
app.router.add_post("/api/session/{session_id}/license", session_license)
|
||||
app.router.add_get("/api/session/{session_id}/prompt", session_prompt_get)
|
||||
app.router.add_post("/api/session/{session_id}/prompt", session_prompt_submit)
|
||||
app.router.add_get("/api/session/{session_id}", session_info)
|
||||
app.router.add_delete("/api/session/{session_id}", session_delete)
|
||||
add: dict[str, Callable[..., Any]] = {
|
||||
"GET": app.router.add_get,
|
||||
"POST": app.router.add_post,
|
||||
"DELETE": app.router.add_delete,
|
||||
}
|
||||
for method, path, handler, remote in ROUTES:
|
||||
if remote_only and not remote:
|
||||
continue
|
||||
add[method](path, handler)
|
||||
|
||||
|
||||
def setup_swagger(app: web.Application) -> None:
|
||||
@@ -1369,27 +1364,5 @@ def setup_swagger(app: web.Application) -> None:
|
||||
),
|
||||
)
|
||||
|
||||
# Add routes with OpenAPI documentation
|
||||
swagger.add_routes(
|
||||
[
|
||||
web.get("/api/health", health),
|
||||
web.get("/api/services", services),
|
||||
web.post("/api/search", search),
|
||||
web.post("/api/list-titles", list_titles),
|
||||
web.post("/api/list-tracks", list_tracks),
|
||||
web.post("/api/download", download),
|
||||
web.get("/api/download/jobs", download_jobs),
|
||||
web.get("/api/download/jobs/{job_id}", download_job_detail),
|
||||
web.delete("/api/download/jobs/{job_id}", cancel_download_job),
|
||||
# Remote-DL session endpoints
|
||||
web.post("/api/session/create", session_create),
|
||||
web.get("/api/session/{session_id}/titles", session_titles),
|
||||
web.post("/api/session/{session_id}/tracks", session_tracks),
|
||||
web.post("/api/session/{session_id}/segments", session_segments),
|
||||
web.post("/api/session/{session_id}/license", session_license),
|
||||
web.get("/api/session/{session_id}/prompt", session_prompt_get),
|
||||
web.post("/api/session/{session_id}/prompt", session_prompt_submit),
|
||||
web.get("/api/session/{session_id}", session_info),
|
||||
web.delete("/api/session/{session_id}", session_delete),
|
||||
]
|
||||
)
|
||||
route: dict[str, Callable[..., Any]] = {"GET": web.get, "POST": web.post, "DELETE": web.delete}
|
||||
swagger.add_routes([route[method](path, handler) for method, path, handler, _ in ROUTES])
|
||||
|
||||
@@ -14,7 +14,7 @@ from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from unshackle.core.api.input_bridge import AuthStatus, InputBridge
|
||||
from unshackle.core.api.input_bridge import AUTH_INPUT_TIMEOUT, AuthStatus, InputBridge
|
||||
from unshackle.core.api.sanitize import sanitize_log
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.tracks import Track
|
||||
@@ -127,7 +127,7 @@ class SessionStore:
|
||||
for sid, entry in self._sessions.items():
|
||||
elapsed = (now - entry.last_accessed).total_seconds()
|
||||
if entry.auth_status in (AuthStatus.AUTHENTICATING, AuthStatus.PENDING_INPUT):
|
||||
if elapsed > 600:
|
||||
if elapsed > AUTH_INPUT_TIMEOUT:
|
||||
expired.append(sid)
|
||||
elif elapsed > self._ttl:
|
||||
expired.append(sid)
|
||||
|
||||
@@ -47,6 +47,7 @@ class Config:
|
||||
self.curl_impersonate: dict = kwargs.get("curl_impersonate") or {}
|
||||
self.remote_cdm: list[dict] = kwargs.get("remote_cdm") or []
|
||||
self.credentials: dict = kwargs.get("credentials") or {}
|
||||
self.firefox_cookies: dict = kwargs.get("firefox_cookies") or {}
|
||||
self.subtitle: dict = kwargs.get("subtitle") or {}
|
||||
|
||||
self.directories = self._Directories()
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from .basic import Basic
|
||||
from .expressvpn import ExpressVPN
|
||||
from .gluetun import Gluetun
|
||||
from .hola import Hola
|
||||
from .nordvpn import NordVPN
|
||||
from .surfsharkvpn import SurfsharkVPN
|
||||
from .windscribevpn import WindscribeVPN
|
||||
|
||||
__all__ = ("Basic", "Gluetun", "Hola", "NordVPN", "SurfsharkVPN", "WindscribeVPN")
|
||||
__all__ = ("Basic", "ExpressVPN", "Gluetun", "Hola", "NordVPN", "SurfsharkVPN", "WindscribeVPN")
|
||||
|
||||
@@ -0,0 +1,845 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import secrets
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import requests
|
||||
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
|
||||
log = logging.getLogger("proxies.expressvpn")
|
||||
|
||||
|
||||
class ExpressVPN(Proxy):
|
||||
"""
|
||||
ExpressVPN HTTPS proxy provider.
|
||||
|
||||
This provider follows the browser extension proxy flow:
|
||||
browser cookies or a cached refresh token are exchanged for API tokens,
|
||||
those tokens are used to resolve proxy-capable locations, and get_proxy()
|
||||
returns an authenticated HTTPS proxy URL.
|
||||
|
||||
Query format:
|
||||
country -- smart connection (random location)
|
||||
country-city -- specific city
|
||||
country-city-N / cityN -- pinned server by position
|
||||
full-slug -- ExpressVPN location slug
|
||||
hostname.expressprovider.com -- direct hostname
|
||||
"""
|
||||
|
||||
CLIENT_ID = "f457fed092a54b9e9f1e2113782d74a2"
|
||||
AUTH_BASE = "https://auth.expressvpn.com/realms/xvpn/protocol/openid-connect"
|
||||
API_BASE = "https://cp.expressapisv2.net"
|
||||
EXTENSION_REDIRECT_URI = "chrome-extension://fgddmllnllkalaagkghckoinaemmogpe/src/html/auth-callback.html"
|
||||
USER_AGENT = (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
||||
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
region_map: Optional[dict[str, str]] = None,
|
||||
server_map: Optional[dict[str, str]] = None,
|
||||
refresh_token: Optional[str] = None,
|
||||
access_token: Optional[str] = None,
|
||||
connection_token: Optional[str] = None,
|
||||
account_json: Optional[str] = None,
|
||||
cookie_path: Optional[str] = None,
|
||||
cache_path: Optional[str] = None,
|
||||
timeout: float = 10.0,
|
||||
):
|
||||
"""
|
||||
Proxy Service using ExpressVPN browser-extension proxy credentials.
|
||||
|
||||
Args:
|
||||
region_map: Optional country-to-preset mapping. Keys are country
|
||||
codes (e.g. ``"us"``), values are optional city/server presets
|
||||
(e.g. ``"ny-02"``). When a key is used without a city in the
|
||||
CLI query, the preset is applied. Empty/null values enable
|
||||
smart connection (random location in that country).
|
||||
server_map: Optional aliases using the same convention as other
|
||||
proxy providers. Values may be ExpressVPN location slugs or
|
||||
concrete .expressprovider.com hosts.
|
||||
refresh_token: Optional OAuth refresh token. If omitted, cached
|
||||
tokens or browser cookies are used.
|
||||
access_token: Optional OAuth access token for advanced/manual use.
|
||||
connection_token: Optional cached connection authorization token.
|
||||
account_json: Optional path to ExpressVPN desktop account.json.
|
||||
cookie_path: Optional path to exported ExpressVPN browser cookies.
|
||||
cache_path: Optional path for cached ExpressVPN tokens.
|
||||
timeout: Request timeout in seconds.
|
||||
"""
|
||||
if region_map is not None and not isinstance(region_map, dict):
|
||||
raise TypeError(f"Expected region_map to be a dict mapping aliases to locations, not '{region_map!r}'.")
|
||||
if server_map is not None and not isinstance(server_map, dict):
|
||||
raise TypeError(f"Expected server_map to be a dict mapping aliases to locations, not '{server_map!r}'.")
|
||||
|
||||
# region_map: country code → optional preset (city[-server])
|
||||
self.region_map = {
|
||||
str(k).lower().strip(): (str(v).lower().strip() if v else None)
|
||||
for k, v in (region_map or {}).items()
|
||||
}
|
||||
# server_map: legacy alias → location slug or direct hostname
|
||||
self.server_map = {
|
||||
str(k).lower().strip(): str(v).lower().strip()
|
||||
for k, v in (server_map or {}).items()
|
||||
}
|
||||
self.refresh_token = refresh_token or None
|
||||
self.access_token = access_token or None
|
||||
self.connection_token = connection_token or None
|
||||
self.account_json = Path(account_json).expanduser() if account_json else None
|
||||
self.timeout = timeout
|
||||
self._tokens: Optional[dict] = None
|
||||
self._srt: Optional[str] = None
|
||||
self._locations: Optional[list[dict]] = None
|
||||
|
||||
# Display info set during _resolve_endpoint for log messages
|
||||
self._last_location_name: Optional[str] = None
|
||||
self._last_server_index: Optional[int] = None
|
||||
self._last_server_total: Optional[int] = None
|
||||
self._last_endpoint_host: Optional[str] = None
|
||||
|
||||
self.cookie_path = Path(cookie_path).expanduser() if cookie_path else self._default_cookie_path()
|
||||
self.cache_path = Path(cache_path).expanduser() if cache_path else self._default_cache_path()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if self._locations is not None:
|
||||
locations = self._locations
|
||||
countries = len({
|
||||
str(loc.get("country_code") or "").upper()
|
||||
for loc in locations if loc.get("country_code")
|
||||
})
|
||||
servers = len(locations)
|
||||
return (
|
||||
f"{countries} Countr{'ies' if countries != 1 else 'y'} "
|
||||
f"({servers} Server{'s' if servers != 1 else ''})"
|
||||
)
|
||||
alias_count = len(self.region_map) + len(self.server_map)
|
||||
if alias_count:
|
||||
return f"{alias_count} Region Alias{'es' if alias_count != 1 else ''} (ExpressVPN HTTPS Proxy)"
|
||||
return "ExpressVPN HTTPS Proxy"
|
||||
|
||||
def get_proxy(self, query: str) -> Optional[str]:
|
||||
query = query.strip().lower()
|
||||
endpoint = self._resolve_endpoint(query)
|
||||
if not endpoint:
|
||||
return None
|
||||
|
||||
connection_token = self._get_connection_token()
|
||||
if not connection_token:
|
||||
log.error("ExpressVPN: connection token was not available")
|
||||
return None
|
||||
|
||||
display = self.last_connection_display()
|
||||
log.debug("ExpressVPN proxy ready: %s", display or f"https://cat:***@{endpoint}:443")
|
||||
return f"https://cat:{connection_token}@{endpoint}:443"
|
||||
|
||||
def last_connection_display(self) -> Optional[str]:
|
||||
"""Return a human-readable string describing the last resolved connection.
|
||||
|
||||
Used by the download/search commands to display a friendly log message
|
||||
instead of a raw sanitized proxy URL.
|
||||
|
||||
Returns:
|
||||
A string like ``"(USA - New York, #3 of 5): .214"`` or None if
|
||||
no connection has been resolved yet.
|
||||
"""
|
||||
if not self._last_location_name or not self._last_endpoint_host:
|
||||
return None
|
||||
|
||||
# Strip the .expressprovider.com suffix for readability
|
||||
short_host = self._last_endpoint_host.replace(".expressprovider.com", "")
|
||||
|
||||
parts = [self._last_location_name]
|
||||
if self._last_server_index is not None and self._last_server_total is not None:
|
||||
parts.append(f"#{self._last_server_index} of {self._last_server_total}")
|
||||
|
||||
return f"({', '.join(parts)}): {short_host}"
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Endpoint Resolution
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _resolve_endpoint(self, query: str) -> Optional[str]:
|
||||
# Reset display info
|
||||
self._last_location_name = None
|
||||
self._last_server_index = None
|
||||
self._last_server_total = None
|
||||
self._last_endpoint_host = None
|
||||
|
||||
# 1) Check legacy server_map first (direct hostname / slug aliases)
|
||||
mapped = self.server_map.get(query)
|
||||
if mapped:
|
||||
query = mapped
|
||||
|
||||
# 2) Direct hostname pass-through
|
||||
if "expressprovider" in query:
|
||||
host = query if query.endswith(".expressprovider.com") else f"{query}.expressprovider.com"
|
||||
self._last_endpoint_host = host
|
||||
return host
|
||||
|
||||
# 3) Parse the query into (country, city, server_num)
|
||||
country, city, server_num = self._parse_query(query)
|
||||
|
||||
# 4) If the query matched a country in region_map and no city was
|
||||
# specified in the CLI, apply the preset from region_map
|
||||
if country and not city:
|
||||
preset = self.region_map.get(country)
|
||||
if preset:
|
||||
# Preset is like "ny-02" or "ny" or "ny2"
|
||||
_, preset_city, preset_server = self._parse_query(f"{country}-{preset}")
|
||||
city = preset_city
|
||||
server_num = preset_server
|
||||
|
||||
# 5) Resolve the location
|
||||
if country:
|
||||
location = self._resolve_location_by_country(country, city)
|
||||
else:
|
||||
# Fallback: try the full query as a location slug
|
||||
location = self._resolve_location_by_slug(query)
|
||||
|
||||
if not location:
|
||||
log.warning("ExpressVPN: no location matched query '%s'", query)
|
||||
return None
|
||||
|
||||
self._last_location_name = location.get("name")
|
||||
|
||||
# 6) Get endpoints and pick server
|
||||
endpoints = self._get_endpoints_for_location(location)
|
||||
if not endpoints:
|
||||
log.error("ExpressVPN: no proxy endpoints returned for %s", location.get("name") or query)
|
||||
return None
|
||||
|
||||
host = self._pick_endpoint(endpoints, server_num)
|
||||
return host
|
||||
|
||||
def _parse_query(self, query: str) -> tuple[Optional[str], Optional[str], Optional[int]]:
|
||||
"""Parse a query string into (country_code, city_query, server_number).
|
||||
|
||||
Supported formats::
|
||||
|
||||
us -> ("us", None, None)
|
||||
us-ny -> ("us", "ny", None)
|
||||
us-ny-2 -> ("us", "ny", 2)
|
||||
us-ny2 -> ("us", "ny", 2)
|
||||
us-ny-02 -> ("us", "ny", 2)
|
||||
usa-new-york -> (None, None, None) -- treated as slug
|
||||
mx -> ("mx", None, None)
|
||||
|
||||
When the query doesn't look like a country[-city] pattern, all
|
||||
fields are returned as None and the caller falls back to slug
|
||||
resolution.
|
||||
"""
|
||||
# Try to extract a trailing server number: "us-ny-2" or "us-ny2"
|
||||
server_num = None
|
||||
base_query = query
|
||||
|
||||
# Match trailing -N or trailing digits glued to alpha
|
||||
num_match = re.match(r"^(.+?)[-]?(\d+)$", query)
|
||||
if num_match:
|
||||
candidate_base = num_match.group(1).rstrip("-")
|
||||
candidate_num = int(num_match.group(2))
|
||||
# Only treat as server number if the base contains alpha characters
|
||||
# and the number is reasonable (1-99)
|
||||
if re.search(r"[a-z]", candidate_base) and 1 <= candidate_num <= 99:
|
||||
base_query = candidate_base
|
||||
server_num = candidate_num
|
||||
|
||||
# Try to split as country-city
|
||||
# Country codes are 2 letters; city is the rest
|
||||
country_match = re.match(r"^([a-z]{2})(?:-(.+))?$", base_query)
|
||||
if country_match:
|
||||
country = country_match.group(1)
|
||||
city = country_match.group(2) # may be None
|
||||
|
||||
# Verify this is a valid country code by checking locations
|
||||
locations = self._get_locations()
|
||||
valid_countries = {str(loc.get("country_code") or "").lower() for loc in locations}
|
||||
if country in valid_countries:
|
||||
return country, city or None, server_num
|
||||
|
||||
# Not a country-city pattern; return None to trigger slug fallback
|
||||
return None, None, None
|
||||
|
||||
def _resolve_location_by_country(
|
||||
self, country_code: str, city_query: Optional[str] = None
|
||||
) -> Optional[dict]:
|
||||
"""Find a location by country code and optional city query.
|
||||
|
||||
If *city_query* is None, a random location in the country is selected
|
||||
(smart connection). Otherwise, the city is matched against location
|
||||
names using abbreviation, prefix, and substring strategies.
|
||||
"""
|
||||
locations = self._get_locations()
|
||||
if not locations:
|
||||
return None
|
||||
|
||||
# Filter locations for this country
|
||||
country_locations = [
|
||||
loc for loc in locations
|
||||
if str(loc.get("country_code") or "").lower() == country_code
|
||||
]
|
||||
if not country_locations:
|
||||
log.warning("ExpressVPN: no locations found for country '%s'", country_code.upper())
|
||||
return None
|
||||
|
||||
if not city_query:
|
||||
# Smart connection: random location in the country
|
||||
return random.choice(country_locations)
|
||||
|
||||
# Match city query against location names
|
||||
return self._match_city(country_locations, city_query)
|
||||
|
||||
def _match_city(self, locations: list[dict], city_query: str) -> Optional[dict]:
|
||||
"""Match a short city query against a list of locations.
|
||||
|
||||
Matching strategies (in priority order):
|
||||
1. First-letter abbreviation: ``"ny"`` matches ``"New York"``
|
||||
2. Exact slug match: ``"miami"`` matches ``"miami"``
|
||||
3. Prefix match on slug: ``"mia"`` matches ``"miami"``
|
||||
4. Substring match on slug: ``"york"`` matches ``"new-york"``
|
||||
|
||||
Returns the first match from the highest-priority non-empty bucket.
|
||||
"""
|
||||
city_query = city_query.strip().lower()
|
||||
abbreviation_hits: list[dict] = []
|
||||
exact_hits: list[dict] = []
|
||||
prefix_hits: list[dict] = []
|
||||
substring_hits: list[dict] = []
|
||||
|
||||
for loc in locations:
|
||||
full_name = str(loc.get("name") or "")
|
||||
# Extract city part: strip country prefix like "USA - "
|
||||
city_part = re.sub(r"^[A-Z]{2,}(?:\s*-\s*)", "", full_name, count=1).strip()
|
||||
if not city_part:
|
||||
city_part = full_name
|
||||
|
||||
city_slug = _slugify(city_part)
|
||||
|
||||
# Strategy 1: first-letter abbreviation
|
||||
words = re.findall(r"[a-zA-Z]+", city_part)
|
||||
abbreviation = "".join(w[0] for w in words).lower() if words else ""
|
||||
if city_query == abbreviation:
|
||||
abbreviation_hits.append(loc)
|
||||
continue
|
||||
|
||||
# Strategy 2: exact slug match
|
||||
if city_query == city_slug:
|
||||
exact_hits.append(loc)
|
||||
continue
|
||||
|
||||
# Strategy 3: prefix match on slug
|
||||
if city_slug.startswith(city_query):
|
||||
prefix_hits.append(loc)
|
||||
continue
|
||||
|
||||
# Strategy 4: substring match on slug
|
||||
if city_query in city_slug:
|
||||
substring_hits.append(loc)
|
||||
|
||||
# Return the first match from the highest-priority non-empty bucket
|
||||
candidates = abbreviation_hits or exact_hits or prefix_hits or substring_hits
|
||||
if not candidates:
|
||||
log.warning("ExpressVPN: no city matched '%s' in available locations", city_query)
|
||||
return None
|
||||
|
||||
if len(candidates) > 1:
|
||||
names = [c.get("name") for c in candidates]
|
||||
log.debug("ExpressVPN: city '%s' matched %d locations: %s", city_query, len(candidates), names)
|
||||
|
||||
return candidates[0]
|
||||
|
||||
def _resolve_location_by_slug(self, query: str) -> Optional[dict]:
|
||||
"""Resolve a location by matching the full query against slugified names, IDs, or country codes."""
|
||||
target = query.strip().lower()
|
||||
locations = self._get_locations()
|
||||
if not locations:
|
||||
return None
|
||||
|
||||
for location in locations:
|
||||
location_id = str(location.get("id") or "").lower()
|
||||
location_name = str(location.get("name") or "").lower()
|
||||
country_code = str(location.get("country_code") or "").lower()
|
||||
slug = _slugify(location_name)
|
||||
if target in (location_id, location_name, country_code, slug):
|
||||
return location
|
||||
return None
|
||||
|
||||
def _pick_endpoint(self, endpoints: list[dict], server_num: Optional[int] = None) -> Optional[str]:
|
||||
"""Select an endpoint host from the list, optionally by position index.
|
||||
|
||||
Args:
|
||||
endpoints: List of endpoint dicts with ``"host"`` keys.
|
||||
server_num: 1-based server position index, or None for random.
|
||||
|
||||
Returns:
|
||||
The selected hostname, or None if no endpoints are available.
|
||||
"""
|
||||
hosts = [str(ep.get("host") or "").lower() for ep in endpoints if ep.get("host")]
|
||||
if not hosts:
|
||||
return None
|
||||
|
||||
total = len(hosts)
|
||||
self._last_server_total = total
|
||||
|
||||
if server_num is not None:
|
||||
if server_num < 1 or server_num > total:
|
||||
log.warning(
|
||||
"ExpressVPN: server #%d is not available (only %d server%s), selecting random",
|
||||
server_num, total, "s" if total != 1 else "",
|
||||
)
|
||||
chosen = random.choice(hosts)
|
||||
self._last_server_index = hosts.index(chosen) + 1
|
||||
else:
|
||||
chosen = hosts[server_num - 1]
|
||||
self._last_server_index = server_num
|
||||
else:
|
||||
chosen = random.choice(hosts)
|
||||
self._last_server_index = hosts.index(chosen) + 1
|
||||
|
||||
self._last_endpoint_host = chosen
|
||||
return chosen
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Locations & Endpoints API
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _get_locations(self) -> list[dict]:
|
||||
if self._locations is not None:
|
||||
return self._locations
|
||||
|
||||
srt = self._get_srt()
|
||||
if not srt:
|
||||
return []
|
||||
|
||||
response = self._request(
|
||||
"GET",
|
||||
f"{self.API_BASE}/ids2/locations",
|
||||
headers=self._api_headers(srt),
|
||||
params={"protocols": "proxy"},
|
||||
)
|
||||
self._locations = response.json().get("locations", [])
|
||||
return self._locations
|
||||
|
||||
def _get_endpoints_for_location(self, location: dict) -> list[dict]:
|
||||
srt = self._get_srt()
|
||||
if not srt:
|
||||
return []
|
||||
|
||||
location_id = location.get("id")
|
||||
response = self._request(
|
||||
"POST",
|
||||
f"{self.API_BASE}/ids2/locations/{location_id}/instances",
|
||||
headers=self._api_headers(srt),
|
||||
params={"protocols": "proxy"},
|
||||
json={},
|
||||
)
|
||||
return response.json().get("endpoints", [])
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Token Management
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _get_tokens(self) -> dict:
|
||||
# Return in-memory cache if access_token is still valid
|
||||
if self._tokens:
|
||||
access_token = self._tokens.get("access_token")
|
||||
if access_token and not self._is_jwt_expired(access_token):
|
||||
return self._tokens
|
||||
|
||||
cached = self._load_cached_tokens()
|
||||
tokens = {
|
||||
**cached,
|
||||
**{k: v for k, v in {
|
||||
"access_token": self.access_token,
|
||||
"refresh_token": self.refresh_token,
|
||||
"connection_token": self.connection_token,
|
||||
}.items() if v},
|
||||
}
|
||||
|
||||
access_token = tokens.get("access_token")
|
||||
if access_token and not self._is_jwt_expired(access_token):
|
||||
self._tokens = tokens
|
||||
return tokens
|
||||
|
||||
refresh_token = tokens.get("refresh_token")
|
||||
if refresh_token:
|
||||
refreshed = self._refresh_access_token(refresh_token)
|
||||
if refreshed:
|
||||
tokens.update(
|
||||
{
|
||||
"access_token": refreshed.get("access_token"),
|
||||
"refresh_token": refreshed.get("refresh_token") or refresh_token,
|
||||
}
|
||||
)
|
||||
self._save_cached_tokens(tokens)
|
||||
self._tokens = tokens
|
||||
return tokens
|
||||
log.warning("ExpressVPN: refresh token failed or session expired")
|
||||
|
||||
if self.account_json and self.account_json.is_file():
|
||||
try:
|
||||
data = json.loads(self.account_json.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError) as error:
|
||||
log.error("ExpressVPN: failed to read account_json %s: %s", self.account_json, error)
|
||||
else:
|
||||
tokens.update(
|
||||
{
|
||||
"access_token": data.get("accessToken"),
|
||||
"connection_token": data.get("connectionToken"),
|
||||
"subscription_id": data.get("subscriptionId"),
|
||||
}
|
||||
)
|
||||
tokens = {k: v for k, v in tokens.items() if v}
|
||||
self._tokens = tokens
|
||||
return tokens
|
||||
|
||||
if self.cookie_path.is_file():
|
||||
bootstrapped = self._run_pkce_bootstrap()
|
||||
if bootstrapped:
|
||||
tokens.update(
|
||||
{
|
||||
"access_token": bootstrapped.get("access_token"),
|
||||
"refresh_token": bootstrapped.get("refresh_token"),
|
||||
}
|
||||
)
|
||||
self._save_cached_tokens(tokens)
|
||||
self._tokens = tokens
|
||||
return tokens
|
||||
|
||||
tokens = {k: v for k, v in tokens.items() if v}
|
||||
self._tokens = tokens
|
||||
return tokens
|
||||
|
||||
def _refresh_access_token(self, refresh_token: str) -> Optional[dict]:
|
||||
response = self._request(
|
||||
"POST",
|
||||
f"{self.AUTH_BASE}/token",
|
||||
data={
|
||||
"grant_type": "refresh_token",
|
||||
"client_id": self.CLIENT_ID,
|
||||
"refresh_token": refresh_token,
|
||||
},
|
||||
headers=self._form_headers(),
|
||||
allow_error=True,
|
||||
)
|
||||
if not response.ok:
|
||||
log.warning("ExpressVPN: access token refresh failed with HTTP %s", response.status_code)
|
||||
return None
|
||||
return response.json()
|
||||
|
||||
def _run_pkce_bootstrap(self) -> Optional[dict]:
|
||||
cookies = self._load_cookies()
|
||||
keycloak_identity = cookies.get("KEYCLOAK_IDENTITY")
|
||||
keycloak_session = cookies.get("KEYCLOAK_SESSION")
|
||||
auth_session_id = cookies.get("AUTH_SESSION_ID")
|
||||
if not keycloak_identity or not keycloak_session:
|
||||
log.error("ExpressVPN: KEYCLOAK_IDENTITY or KEYCLOAK_SESSION is missing from %s", self.cookie_path)
|
||||
return None
|
||||
|
||||
verifier = base64.urlsafe_b64encode(
|
||||
secrets.token_bytes(32)
|
||||
).decode("utf-8").rstrip("=")
|
||||
challenge = base64.urlsafe_b64encode(
|
||||
hashlib.sha256(verifier.encode("utf-8")).digest()
|
||||
).decode("utf-8").rstrip("=")
|
||||
auth_cookies = requests.cookies.RequestsCookieJar()
|
||||
auth_cookies.set("KEYCLOAK_IDENTITY", keycloak_identity, domain="auth.expressvpn.com")
|
||||
auth_cookies.set("KEYCLOAK_SESSION", keycloak_session, domain="auth.expressvpn.com")
|
||||
if auth_session_id:
|
||||
auth_cookies.set("AUTH_SESSION_ID", auth_session_id, domain="auth.expressvpn.com")
|
||||
|
||||
response = self._request(
|
||||
"GET",
|
||||
f"{self.AUTH_BASE}/auth",
|
||||
params={
|
||||
"client_id": self.CLIENT_ID,
|
||||
"response_type": "code",
|
||||
"redirect_uri": self.EXTENSION_REDIRECT_URI,
|
||||
"scope": "profile offline_access",
|
||||
"code_challenge": challenge,
|
||||
"code_challenge_method": "S256",
|
||||
"ui_locales": "en",
|
||||
},
|
||||
headers=self._browser_headers(),
|
||||
cookies=auth_cookies,
|
||||
allow_redirects=False,
|
||||
allow_error=True,
|
||||
)
|
||||
|
||||
redirect_url = response.headers.get("Location", "")
|
||||
code = parse_qs(urlparse(redirect_url).query).get("code", [None])[0]
|
||||
if not code:
|
||||
log.error("ExpressVPN: OAuth bootstrap did not return an authorization code")
|
||||
return None
|
||||
|
||||
token_response = self._request(
|
||||
"POST",
|
||||
f"{self.AUTH_BASE}/token",
|
||||
data={
|
||||
"grant_type": "authorization_code",
|
||||
"client_id": self.CLIENT_ID,
|
||||
"code": code,
|
||||
"redirect_uri": self.EXTENSION_REDIRECT_URI,
|
||||
"code_verifier": verifier,
|
||||
},
|
||||
headers=self._form_headers(),
|
||||
allow_error=True,
|
||||
)
|
||||
if not token_response.ok:
|
||||
log.error("ExpressVPN: OAuth token exchange failed with HTTP %s", token_response.status_code)
|
||||
return None
|
||||
return token_response.json()
|
||||
|
||||
def _get_srt(self) -> Optional[str]:
|
||||
if self._srt and not self._is_jwt_expired(self._srt):
|
||||
return self._srt
|
||||
|
||||
tokens = self._get_tokens()
|
||||
cached_srt = tokens.get("srt")
|
||||
if cached_srt and not self._is_jwt_expired(cached_srt):
|
||||
self._srt = cached_srt
|
||||
return self._srt
|
||||
|
||||
access_token = tokens.get("access_token")
|
||||
if not access_token:
|
||||
log.error("ExpressVPN: access token was not available")
|
||||
return None
|
||||
|
||||
response = self._request(
|
||||
"POST",
|
||||
f"{self.API_BASE}/srs2/subscription_receipts",
|
||||
headers=self._api_headers(access_token),
|
||||
json={},
|
||||
)
|
||||
receipts = response.json().get("srts", [])
|
||||
active_subscription_id = tokens.get("subscription_id")
|
||||
srt = self._select_srt(receipts, active_subscription_id)
|
||||
if not srt:
|
||||
log.error("ExpressVPN: no active subscription receipt was found")
|
||||
return None
|
||||
|
||||
self._srt = srt
|
||||
tokens["srt"] = srt
|
||||
self._save_cached_tokens(tokens)
|
||||
return srt
|
||||
|
||||
def _get_connection_token(self) -> Optional[str]:
|
||||
tokens = self._get_tokens()
|
||||
connection_token = tokens.get("connection_token")
|
||||
if connection_token and not self._is_jwt_expired(connection_token):
|
||||
return connection_token
|
||||
|
||||
auth_token = self._get_srt() or tokens.get("access_token")
|
||||
if not auth_token:
|
||||
return connection_token
|
||||
|
||||
response = self._request(
|
||||
"POST",
|
||||
f"{self.API_BASE}/srs2/connection_token",
|
||||
headers=self._api_headers(auth_token),
|
||||
json={},
|
||||
)
|
||||
body = response.json()
|
||||
connection_token = body.get("connection_token") or body.get("token")
|
||||
if connection_token:
|
||||
tokens["connection_token"] = connection_token
|
||||
self._save_cached_tokens(tokens)
|
||||
return connection_token
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Cookies & Cache
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _load_cookies(self) -> dict[str, str]:
|
||||
if not self.cookie_path.is_file():
|
||||
return {}
|
||||
|
||||
try:
|
||||
content = self.cookie_path.read_text(encoding="utf-8").strip()
|
||||
except OSError as error:
|
||||
log.error("ExpressVPN: failed to read cookies file %s: %s", self.cookie_path, error)
|
||||
return {}
|
||||
|
||||
cookies: dict[str, str] = {}
|
||||
try:
|
||||
data = json.loads(content)
|
||||
except json.JSONDecodeError:
|
||||
for line in content.splitlines():
|
||||
if not line.strip() or line.startswith("#"):
|
||||
continue
|
||||
parts = line.rstrip("\n").split("\t")
|
||||
if len(parts) >= 7 and "expressvpn.com" in parts[0]:
|
||||
cookies[parts[5]] = parts[6]
|
||||
elif "=" in line:
|
||||
name, value = line.split("=", maxsplit=1)
|
||||
cookies[name.strip()] = value.strip()
|
||||
else:
|
||||
if isinstance(data, list):
|
||||
for cookie in data:
|
||||
if isinstance(cookie, dict) and "expressvpn.com" in str(cookie.get("domain", "")):
|
||||
name = cookie.get("name")
|
||||
value = cookie.get("value")
|
||||
if name and value is not None:
|
||||
cookies[str(name)] = str(value)
|
||||
elif isinstance(data, dict):
|
||||
cookies.update({str(k): str(v) for k, v in data.items()})
|
||||
return cookies
|
||||
|
||||
def _load_cached_tokens(self) -> dict:
|
||||
if not self.cache_path.is_file():
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(self.cache_path.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError) as error:
|
||||
log.warning("ExpressVPN: failed to read token cache %s: %s", self.cache_path, error)
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
def _save_cached_tokens(self, tokens: dict) -> None:
|
||||
# Invalidate in-memory cache so next _get_tokens() re-reads
|
||||
self._tokens = tokens.copy()
|
||||
try:
|
||||
self.cache_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
payload = json.dumps({k: v for k, v in tokens.items() if v}, indent=2)
|
||||
_write_private(self.cache_path, payload)
|
||||
except OSError as error:
|
||||
log.error("ExpressVPN: failed to save token cache %s: %s", self.cache_path, error)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# HTTP & Helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _request(self, method: str, url: str, allow_error: bool = False, **kwargs) -> requests.Response:
|
||||
kwargs.setdefault("timeout", self.timeout)
|
||||
response = requests.request(method, url, **kwargs)
|
||||
if response.ok or allow_error:
|
||||
return response
|
||||
raise ValueError(f"ExpressVPN request failed with HTTP {response.status_code}: {url}")
|
||||
|
||||
def _api_headers(self, token: str) -> dict[str, str]:
|
||||
return {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": self.USER_AGENT,
|
||||
"X-Client-App-Version": "12.0.0",
|
||||
"X-Client-OS": "Windows",
|
||||
"X-Client-Device-Model": "Browser Extension",
|
||||
}
|
||||
|
||||
def _browser_headers(self) -> dict[str, str]:
|
||||
return {
|
||||
"User-Agent": self.USER_AGENT,
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
}
|
||||
|
||||
def _form_headers(self) -> dict[str, str]:
|
||||
return {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Accept": "application/json",
|
||||
"User-Agent": self.USER_AGENT,
|
||||
}
|
||||
|
||||
def _select_srt(self, receipts: list[dict], active_subscription_id: Optional[str]) -> Optional[str]:
|
||||
for receipt in receipts:
|
||||
srt = receipt.get("srt")
|
||||
if "xv.vpn" in self._decode_jwt_payload(srt).get("entitlements", {}):
|
||||
return srt
|
||||
|
||||
if active_subscription_id:
|
||||
for receipt in receipts:
|
||||
if receipt.get("subscription_id") == active_subscription_id:
|
||||
return receipt.get("srt")
|
||||
|
||||
if receipts:
|
||||
return receipts[0].get("srt")
|
||||
return None
|
||||
|
||||
def _decode_jwt_payload(self, token: Optional[str]) -> dict:
|
||||
if not token:
|
||||
return {}
|
||||
try:
|
||||
parts = token.split(".")
|
||||
if len(parts) < 2:
|
||||
return {}
|
||||
payload = parts[1] + "=" * ((4 - len(parts[1]) % 4) % 4)
|
||||
return json.loads(base64.urlsafe_b64decode(payload).decode("utf-8"))
|
||||
except (ValueError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
def _is_jwt_expired(self, token: str) -> bool:
|
||||
expires_at = self._decode_jwt_payload(token).get("exp")
|
||||
if not expires_at:
|
||||
# Tokens without ``exp`` are treated as non-expiring. Keycloak
|
||||
# access/refresh tokens always carry ``exp``; proprietary SRT/
|
||||
# connection tokens may omit it — they are refreshed via the
|
||||
# normal SRT → connection_token flow anyway.
|
||||
return False
|
||||
return time.time() >= (int(expires_at) - 300)
|
||||
|
||||
def _default_cookie_path(self) -> Path:
|
||||
cookies_dir = Path(config.directories.cookies)
|
||||
for folder in ("vpn", "vpns"):
|
||||
candidate = cookies_dir / folder / "expressvpn.txt"
|
||||
if candidate.is_file():
|
||||
return candidate
|
||||
return cookies_dir / "vpn" / "expressvpn.txt"
|
||||
|
||||
def _default_cache_path(self) -> Path:
|
||||
cache_dir = Path(config.directories.cache)
|
||||
for folder in ("vpn", "vpns"):
|
||||
candidate = cache_dir / "global" / f"{folder}_expressvpn_tokens.json"
|
||||
if candidate.is_file():
|
||||
return candidate
|
||||
candidate = cache_dir / "global" / "expressvpn_tokens.json"
|
||||
if candidate.is_file():
|
||||
return candidate
|
||||
return cache_dir / "global" / "expressvpn_tokens.json"
|
||||
|
||||
def close(self) -> None:
|
||||
pass
|
||||
|
||||
def __enter__(self) -> ExpressVPN:
|
||||
return self
|
||||
|
||||
def __exit__(self, *_: object) -> None:
|
||||
self.close()
|
||||
|
||||
|
||||
def _slugify(value: str) -> str:
|
||||
return re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-")
|
||||
|
||||
|
||||
def _write_private(path: Path, content: str) -> None:
|
||||
"""Write *content* to *path* with owner-only permissions (0600).
|
||||
|
||||
Opens with ``os.open`` so the file is **created** at 0600 — no window
|
||||
where it is world-readable. On Windows ``os.fchmod`` is unavailable,
|
||||
so we fall back to ``path.chmod`` after close.
|
||||
"""
|
||||
if hasattr(os, "fchmod"):
|
||||
fd = os.open(str(path), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
||||
try:
|
||||
os.fchmod(fd, 0o600)
|
||||
os.write(fd, content.encode("utf-8"))
|
||||
finally:
|
||||
os.close(fd)
|
||||
else:
|
||||
path.write_text(content, encoding="utf-8")
|
||||
try:
|
||||
path.chmod(0o600)
|
||||
except OSError:
|
||||
pass
|
||||
@@ -19,6 +19,7 @@ def initialize_proxy_providers() -> List[Any]:
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.config import config as main_config
|
||||
from unshackle.core.proxies.basic import Basic
|
||||
from unshackle.core.proxies.expressvpn import ExpressVPN
|
||||
from unshackle.core.proxies.hola import Hola
|
||||
from unshackle.core.proxies.nordvpn import NordVPN
|
||||
from unshackle.core.proxies.surfsharkvpn import SurfsharkVPN
|
||||
@@ -27,6 +28,8 @@ def initialize_proxy_providers() -> List[Any]:
|
||||
|
||||
if proxy_config.get("basic"):
|
||||
proxy_providers.append(Basic(**proxy_config["basic"]))
|
||||
if proxy_config.get("expressvpn"):
|
||||
proxy_providers.append(ExpressVPN(**proxy_config["expressvpn"]))
|
||||
if proxy_config.get("nordvpn"):
|
||||
proxy_providers.append(NordVPN(**proxy_config["nordvpn"]))
|
||||
if proxy_config.get("surfsharkvpn"):
|
||||
|
||||
@@ -198,7 +198,7 @@ def _build_tracks(data: Dict[str, Any]) -> Tracks:
|
||||
return tracks
|
||||
|
||||
|
||||
def _resolve_manifest_data(tracks: Tracks, manifests: list, session: Any) -> None:
|
||||
def _resolve_manifest_data(tracks: Tracks, manifests: list) -> None:
|
||||
"""Re-parse serialized manifests and populate track.data for downloading.
|
||||
|
||||
The server serializes DASH and ISM manifest XML as zlib-compressed base64.
|
||||
@@ -604,7 +604,7 @@ class RemoteService:
|
||||
for k, v in result.get("session_cookies", {}).items():
|
||||
self._session.cookies.set(k, v)
|
||||
|
||||
_resolve_manifest_data(tracks, result.get("manifests", []), self._session)
|
||||
_resolve_manifest_data(tracks, result.get("manifests", []))
|
||||
|
||||
self._server_cdm_type = result.get("server_cdm_type", "widevine")
|
||||
|
||||
|
||||
@@ -209,8 +209,7 @@ class UpdateChecker:
|
||||
return None
|
||||
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
latest_version = await loop.run_in_executor(None, cls._fetch_latest_version)
|
||||
latest_version = await asyncio.to_thread(cls._fetch_latest_version)
|
||||
|
||||
if latest_version and cls._compare_versions(current_version, latest_version):
|
||||
return latest_version
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
"""
|
||||
Firefox Cookie and LocalStorage Extractor Utility for unshackle.
|
||||
Provides a secure, read-only mechanism to extract authentication tokens from Firefox.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
import platform
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from http.cookiejar import CookieJar, Cookie
|
||||
from typing import Dict, Optional, List
|
||||
|
||||
|
||||
def get_firefox_root() -> Path:
|
||||
"""Locates the operating system specific root directory for Firefox profiles."""
|
||||
system = platform.system()
|
||||
home = Path.home()
|
||||
if system == 'Windows':
|
||||
return Path(os.getenv('APPDATA', '')) / 'Mozilla' / 'Firefox'
|
||||
elif system == 'Darwin':
|
||||
return home / 'Library' / 'Application Support' / 'Firefox'
|
||||
elif system == 'Linux':
|
||||
paths = [
|
||||
home / '.mozilla' / 'firefox',
|
||||
home / 'snap' / 'firefox' / 'common' / '.mozilla' / 'firefox',
|
||||
home / '.var' / 'app' / 'org.mozilla.firefox' / '.mozilla' / 'firefox'
|
||||
]
|
||||
for p in paths:
|
||||
if p.exists():
|
||||
return p
|
||||
return paths[0]
|
||||
raise RuntimeError(f"Unsupported operating system: {system}")
|
||||
|
||||
|
||||
def get_latest_profile_path(ff_root: Path) -> Path:
|
||||
"""Finds the most recently modified valid Firefox profile subdirectory."""
|
||||
search_dirs = [ff_root / 'Profiles', ff_root]
|
||||
valid_profiles = []
|
||||
for base in search_dirs:
|
||||
if base.exists():
|
||||
valid_profiles.extend([p for p in base.iterdir() if p.is_dir() and (p / 'cookies.sqlite').exists()])
|
||||
|
||||
if not valid_profiles:
|
||||
raise FileNotFoundError("No active Firefox profiles detected.")
|
||||
|
||||
return max(valid_profiles, key=lambda p: (p / 'cookies.sqlite').stat().st_mtime)
|
||||
|
||||
|
||||
def get_local_storage_data(profile_path: Path, hosts: List[str], tmp_dir_path: Path) -> Dict[str, str]:
|
||||
"""
|
||||
Extracts LocalStorage tokens from webappsstore.sqlite with strict origin matching.
|
||||
Operates within a secure temporary directory.
|
||||
"""
|
||||
ls_db = profile_path / 'webappsstore.sqlite'
|
||||
if not ls_db.exists():
|
||||
return {}
|
||||
|
||||
temp_ls = tmp_dir_path / "webappsstore.sqlite"
|
||||
extracted_storage = {}
|
||||
|
||||
try:
|
||||
shutil.copy2(ls_db, temp_ls)
|
||||
conn = sqlite3.connect(temp_ls)
|
||||
cursor = conn.cursor()
|
||||
|
||||
for host in hosts:
|
||||
# Firefox stores LocalStorage as reversed host (e.g., moc.viki.:https:443)
|
||||
# Use strict prefix matching to prevent over-broad harvesting
|
||||
reversed_host = host[::-1]
|
||||
query = "SELECT key, value FROM webappsstore2 WHERE originKey LIKE ?"
|
||||
cursor.execute(query, (f"{reversed_host}.%",))
|
||||
|
||||
for key, value in cursor.fetchall():
|
||||
extracted_storage[key] = value
|
||||
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return extracted_storage
|
||||
|
||||
|
||||
def get_firefox_cookies(service_settings: dict) -> Optional[CookieJar]:
|
||||
"""
|
||||
Extracts cookies and optionally localStorage from Firefox based on provided host patterns.
|
||||
Implements security hardening and data consistency (WAL support) as per PR review.
|
||||
"""
|
||||
raw_hosts = service_settings.get('hosts', [])
|
||||
# Filter empty or dangerously short hosts to prevent full store dumps (Finding #3)
|
||||
priority_hosts = [h.strip().lower() for h in raw_hosts if h and len(h.strip()) >= 3]
|
||||
use_local_storage = service_settings.get('local_storage', False)
|
||||
|
||||
if not priority_hosts:
|
||||
return None
|
||||
|
||||
try:
|
||||
profile_path = get_latest_profile_path(get_firefox_root())
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
cookie_jar = CookieJar()
|
||||
|
||||
# Use a secure temporary directory with restricted permissions (Finding #5)
|
||||
with tempfile.TemporaryDirectory(prefix="unshackle_ff_") as tmp_dir:
|
||||
tmp_dir_path = Path(tmp_dir)
|
||||
os.chmod(tmp_dir, 0o700)
|
||||
|
||||
temp_db = tmp_dir_path / "cookies.sqlite"
|
||||
temp_wal = tmp_dir_path / "cookies.sqlite-wal"
|
||||
|
||||
try:
|
||||
# Copy main DB and its WAL log to ensure fresh data capture (Finding #5)
|
||||
shutil.copy2(profile_path / 'cookies.sqlite', temp_db)
|
||||
wal_path = profile_path / 'cookies.sqlite-wal'
|
||||
if wal_path.exists():
|
||||
shutil.copy2(wal_path, temp_wal)
|
||||
|
||||
conn = sqlite3.connect(temp_db)
|
||||
cursor = conn.cursor()
|
||||
|
||||
for host in priority_hosts:
|
||||
# Precise matching: exact host or dot-prefixed domain suffix (Finding #2)
|
||||
query = """
|
||||
SELECT host, name, value, path, expiry, isSecure, isHttpOnly
|
||||
FROM moz_cookies
|
||||
WHERE host = ? OR host LIKE ?
|
||||
"""
|
||||
cursor.execute(query, (host, f"%.{host}"))
|
||||
rows = cursor.fetchall()
|
||||
|
||||
for h, n, v, p, e, secure, httponly in rows:
|
||||
# Final validation to ensure proper domain suffix
|
||||
if h == host or h.endswith(f".{host}"):
|
||||
c = Cookie(
|
||||
version=0, name=n, value=v, port=None, port_specified=False,
|
||||
domain=h, domain_specified=True, domain_initial_dot=h.startswith('.'),
|
||||
path=p, path_specified=True, secure=bool(secure), expires=e,
|
||||
discard=False, comment=None, comment_url=None,
|
||||
rest={'HttpOnly': str(bool(httponly))}, rfc2109=False
|
||||
)
|
||||
cookie_jar.set_cookie(c)
|
||||
conn.close()
|
||||
|
||||
# Optional LocalStorage harvesting (Finding #4)
|
||||
if use_local_storage:
|
||||
storage_data = get_local_storage_data(profile_path, priority_hosts, tmp_dir_path)
|
||||
for key, val in storage_data.items():
|
||||
c = Cookie(
|
||||
version=0, name=key, value=val, port=None, port_specified=False,
|
||||
domain=".localstorage", domain_specified=True, domain_initial_dot=False,
|
||||
path='/', path_specified=True, secure=False, expires=None,
|
||||
discard=True, comment=None, comment_url=None, rest={}, rfc2109=False
|
||||
)
|
||||
cookie_jar.set_cookie(c)
|
||||
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
return cookie_jar if len(list(cookie_jar)) > 0 else None
|
||||
@@ -158,6 +158,14 @@ muxing:
|
||||
# false: Separate MKV per (quality, audio_codec) combination
|
||||
# Example: Title.1080p.AAC.mkv, Title.1080p.EC3.mkv
|
||||
merge_audio: true
|
||||
# merge_video: Merge video language variants into one file
|
||||
# false (default): One file per video track
|
||||
# true: Group videos by (resolution, range, codec) and merge each group into one
|
||||
# MKV - so only language varies within a file. Different resolutions, ranges
|
||||
# (SDR/HDR10/DV/...) and codecs (H264/H265) still produce separate files.
|
||||
# Example: -r HYBRID,DV,HDR10,SDR yields one file per range, not one mega-file.
|
||||
# The --merge-video CLI flag overrides this.
|
||||
merge_video: false
|
||||
# default_language: Override which track is flagged as the default in the muxed MKV.
|
||||
# audio: BCP-47 tag of the preferred default audio track (e.g. pl, en, pt-BR).
|
||||
# Wins over the title's original_language. Falls back to is_original_lang
|
||||
@@ -675,9 +683,10 @@ services:
|
||||
User-Agent: "Service-specific user agent string"
|
||||
Accept-Language: "en-US,en;q=0.9"
|
||||
|
||||
# Override muxing options
|
||||
# Override muxing options (always merge this service's video tracks into one file)
|
||||
muxing:
|
||||
set_title: true
|
||||
merge_video: true
|
||||
|
||||
# Remap service-provided titles before naming/output
|
||||
# Keyed by the exact title the service returns -> desired output title.
|
||||
@@ -804,6 +813,27 @@ proxy_providers:
|
||||
# Example: --proxy gluetun:nordvpn:us1239 connects to us1239.nordvpn.com
|
||||
# Supported providers: nordvpn, surfshark, expressvpn, cyberghost
|
||||
|
||||
# ExpressVPN HTTPS Proxy Provider
|
||||
# Uses browser extension proxy flow with OAuth token authentication.
|
||||
# Requires exported browser cookies from auth.expressvpn.com (Keycloak session).
|
||||
# Cookie file: cookies/vpn/expressvpn.txt (or cookies/vpns/expressvpn.txt)
|
||||
# Token cache: cache/global/expressvpn_tokens.json (auto-managed)
|
||||
#
|
||||
# Query formats:
|
||||
# --proxy expressvpn:us → random US location (smart connection)
|
||||
# --proxy expressvpn:us-ny → specific city (New York)
|
||||
# --proxy expressvpn:us-ny-2 → pinned server #2 in New York
|
||||
# --proxy expressvpn:mx → random Mexico location
|
||||
# --proxy expressvpn:es-madrid → specific city (Madrid)
|
||||
#
|
||||
# expressvpn:
|
||||
# region_map:
|
||||
# us: "ny" # Default to New York when --proxy expressvpn:us
|
||||
# gb: "london" # Default to London when --proxy expressvpn:gb
|
||||
# es: "madrid" # Default to Madrid when --proxy expressvpn:es
|
||||
# mx: # Smart connection (random MX location) when --proxy expressvpn:mx
|
||||
# de: "frankfurt" # Default to Frankfurt when --proxy expressvpn:de
|
||||
|
||||
basic:
|
||||
GB:
|
||||
- "socks5://username:password@bhx.socks.ipvanish.com:1080" # 1 (Birmingham)
|
||||
|
||||
Reference in New Issue
Block a user