mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-24 09:55:12 +00:00
feat(api): expose optional account-scoped title availability
This commit is contained in:
@@ -335,6 +335,19 @@ def get_titles(self) -> Titles_T:
|
||||
])
|
||||
```
|
||||
|
||||
### Optional availability metadata
|
||||
|
||||
A service may set `title.data["availability"]` to a dict with two optional fields:
|
||||
|
||||
- `paid`: a boolean describing the catalogue offer, not the account's entitlement.
|
||||
- `access`: `"granted"`, `"denied"`, or `"unknown"` for the authenticated account.
|
||||
Report `"denied"` only when the platform explicitly confirms lack of access.
|
||||
Missing or unverified access is not denial.
|
||||
|
||||
Only these allowlisted fields reach the REST API as `availability`, including
|
||||
through remote title reconstruction. The CLI's `--list-titles` does not display
|
||||
them, and nothing in core reads them to select or authorize downloads.
|
||||
|
||||
Common constructor fields:
|
||||
|
||||
=== "Movie"
|
||||
|
||||
@@ -283,6 +283,7 @@ These keys are sent only when the title carries them, so a title without them se
|
||||
|
||||
| Key | Type | On | Meaning |
|
||||
| --- | --- | --- | --- |
|
||||
| `availability` | object | all | Optional account-scoped metadata: `paid` is a boolean describing the catalogue offer; `access` is `"granted"`, `"denied"`, or `"unknown"`. Both fields are optional; missing or unknown access is not denial. Only these allowlisted fields are forwarded, including through remote services. |
|
||||
| `part` | integer | episode | Part index of a [split episode](../creating-a-service.md#split-episodes), counting from 1. |
|
||||
| `air_date` | string | episode | ISO air date of [daily and date-based content](../../guide/downloading.md#daily-and-date-based-content). unshackle names the episode by date instead of `SxxExx`. |
|
||||
| `absolute` | integer | episode | Absolute episode number across all seasons. |
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
"""Availability stays optional, allowlisted and separate from private service data."""
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.api.handlers import serialize_title
|
||||
from unshackle.core.remote_service import build_title
|
||||
from unshackle.core.titles.availability import availability_metadata
|
||||
from unshackle.core.titles.episode import Episode
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
class EXAMPLE:
|
||||
pass
|
||||
|
||||
|
||||
def episode(data=None):
|
||||
return Episode(id_="ep-0001", service=EXAMPLE, title="Show", season=1, number=1, data=data)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", [None, [], "denied", {}, {"paid": 1}, {"access": "false"}, {"access": []}])
|
||||
def test_invalid_metadata_is_omitted(value):
|
||||
assert availability_metadata(value) is None
|
||||
assert "availability" not in serialize_title(episode({"availability": value}))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("access", ["granted", "denied", "unknown"])
|
||||
def test_paid_and_access_are_independent_and_round_trip(access):
|
||||
data = {"paid": True, "access": access}
|
||||
serialized = serialize_title(episode({"availability": data}))
|
||||
assert serialized["availability"] == data
|
||||
assert build_title(serialized, "EXAMPLE", "fallback").data["availability"] == data
|
||||
|
||||
|
||||
def test_private_data_and_unknown_fields_never_leak():
|
||||
data = {"availability": {"paid": False, "access": "unknown", "token": "secret"}, "cookies": "secret"}
|
||||
result = serialize_title(episode(data))
|
||||
assert result["availability"] == {"paid": False, "access": "unknown"}
|
||||
assert "secret" not in str(result)
|
||||
assert data["availability"]["token"] == "secret"
|
||||
|
||||
|
||||
def test_missing_metadata_keeps_response_unchanged():
|
||||
assert "availability" not in serialize_title(episode())
|
||||
|
||||
|
||||
def test_remote_input_is_also_allowlisted():
|
||||
info = serialize_title(episode())
|
||||
info["availability"] = {"paid": True, "access": "denied", "cookies": "secret"}
|
||||
rebuilt = build_title(info, "EXAMPLE", "fallback")
|
||||
assert rebuilt.data["availability"] == {"paid": True, "access": "denied"}
|
||||
|
||||
|
||||
def test_availability_appends_without_reordering_existing_fields():
|
||||
title = episode()
|
||||
title.part = 2
|
||||
title.anime = True
|
||||
original = serialize_title(title)
|
||||
title.data = {"availability": {"paid": True, "access": "granted"}}
|
||||
serialized = serialize_title(title)
|
||||
assert list(serialized) == [*original, "availability"]
|
||||
assert {key: serialized[key] for key in original} == original
|
||||
@@ -43,6 +43,7 @@ from unshackle.core.proxies.resolve import initialize_proxy_providers, is_loopba
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.session import RnetSession, utf8_text
|
||||
from unshackle.core.titles import Episode, Movie, Song, Title_T
|
||||
from unshackle.core.titles.availability import availability_metadata
|
||||
from unshackle.core.titles.music import MUSIC_DATA_KEYS, SONG_FIELDS, song_metadata
|
||||
from unshackle.core.titles.title import rename
|
||||
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
|
||||
@@ -1686,6 +1687,7 @@ def serialize_title(title: Title_T) -> Dict[str, Any]:
|
||||
# a richer listing without re-fetching the page.
|
||||
description = getattr(title, "description", None) or None
|
||||
_data = getattr(title, "data", None)
|
||||
availability = availability_metadata(_data.get("availability")) if isinstance(_data, dict) else None
|
||||
date = _data.get("date") if isinstance(_data, dict) else None
|
||||
cover_url = _data.get("cover_url") if isinstance(_data, dict) else None
|
||||
|
||||
@@ -1736,6 +1738,8 @@ def serialize_title(title: Title_T) -> Dict[str, Any]:
|
||||
if isinstance(title, (Episode, Movie)) and getattr(title, "anime", None) is not None:
|
||||
result["anime"] = title.anime
|
||||
|
||||
if availability is not None:
|
||||
result["availability"] = availability
|
||||
return result
|
||||
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ from unshackle.core.proxies.resolve import is_loopback
|
||||
from unshackle.core.search_result import SearchResult
|
||||
from unshackle.core.session import MAX_BACKOFF
|
||||
from unshackle.core.titles import Title_T, Titles_T, remap_titles
|
||||
from unshackle.core.titles.availability import availability_metadata
|
||||
from unshackle.core.titles.episode import Episode, Series
|
||||
from unshackle.core.titles.movie import Movie, Movies
|
||||
from unshackle.core.titles.music import MUSIC_DATA_KEYS, SONG_FIELDS, Album, Song
|
||||
@@ -606,6 +607,9 @@ def apply_title_fields(title: Title_T, info: Dict[str, Any]) -> None:
|
||||
setattr(title, key, info[key])
|
||||
|
||||
extra = {k: info[k] for k in ("date", "cover_url") if info.get(k) is not None}
|
||||
availability = availability_metadata(info.get("availability"))
|
||||
if availability is not None:
|
||||
extra["availability"] = availability
|
||||
music_data = info.get("music_data")
|
||||
if isinstance(music_data, dict):
|
||||
extra.update({key: music_data[key] for key in MUSIC_DATA_KEYS if key in music_data})
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Public, account-scoped availability metadata supplied by a service.
|
||||
|
||||
Keep this allowlist separate from title.data: that dict can contain credentials,
|
||||
playback URLs and other private service state which must never cross the API.
|
||||
"""
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
|
||||
def availability_metadata(value: Any) -> Optional[Dict[str, Any]]:
|
||||
"""Return only well-typed display fields; absent metadata remains absent."""
|
||||
if not isinstance(value, dict):
|
||||
return None
|
||||
result: Dict[str, Any] = {}
|
||||
if isinstance(value.get("paid"), bool):
|
||||
result["paid"] = value["paid"]
|
||||
access = value.get("access")
|
||||
if isinstance(access, str) and access in ("granted", "denied", "unknown"):
|
||||
result["access"] = access
|
||||
return result or None
|
||||
Reference in New Issue
Block a user