forked from kenzuya/unshackle
docs: strip non-essential comments, fold key facts into docstrings
This commit is contained in:
@@ -63,9 +63,6 @@ def get_provider(name: str) -> Optional[MetadataProvider]:
|
||||
return p if p.is_available() else None
|
||||
|
||||
|
||||
# -- Public API (replaces tags.py functions) --
|
||||
|
||||
|
||||
def search_metadata(
|
||||
title: str,
|
||||
year: Optional[int],
|
||||
@@ -87,7 +84,6 @@ def search_metadata(
|
||||
|
||||
ordered = provider_order(kind, anime)
|
||||
|
||||
# Check cache first
|
||||
if title_cacher and cache_title_id:
|
||||
for cls in ordered:
|
||||
p = cls()
|
||||
@@ -100,7 +96,6 @@ def search_metadata(
|
||||
log.debug("Using cached %s data for %r", p.NAME, title)
|
||||
return result
|
||||
|
||||
# Search providers in priority order
|
||||
for cls in ordered:
|
||||
p = cls()
|
||||
if not p.is_available():
|
||||
@@ -111,9 +106,7 @@ def search_metadata(
|
||||
log.debug("%s search failed: %s", p.NAME, exc)
|
||||
continue
|
||||
if result and result.title and fuzzy_match(result.title, title):
|
||||
# Enrich with cross-referenced IDs if we have IMDB but missing TMDB/TVDB
|
||||
enrich_ids(result)
|
||||
# Cache the result (include enriched IDs so they survive round-trip)
|
||||
if title_cacher and cache_title_id and result.raw:
|
||||
try:
|
||||
cache_data = result.raw
|
||||
@@ -141,7 +134,6 @@ def get_title_by_id(
|
||||
cache_account_hash: Optional[str] = None,
|
||||
) -> Optional[str]:
|
||||
"""Get title name by TMDB ID."""
|
||||
# Check cache first
|
||||
if title_cacher and cache_title_id:
|
||||
cached = title_cacher.get_cached_provider("tmdb", cache_title_id, kind, cache_region, cache_account_hash)
|
||||
if cached and cached.get("detail"):
|
||||
@@ -158,7 +150,6 @@ def get_title_by_id(
|
||||
if not result:
|
||||
return None
|
||||
|
||||
# Cache if possible
|
||||
if title_cacher and cache_title_id and result.raw:
|
||||
try:
|
||||
ext_ids = tmdb.get_external_ids(tmdb_id, kind)
|
||||
@@ -185,7 +176,6 @@ def get_year_by_id(
|
||||
cache_account_hash: Optional[str] = None,
|
||||
) -> Optional[int]:
|
||||
"""Get release year by TMDB ID."""
|
||||
# Check cache first
|
||||
if title_cacher and cache_title_id:
|
||||
cached = title_cacher.get_cached_provider("tmdb", cache_title_id, kind, cache_region, cache_account_hash)
|
||||
if cached and cached.get("detail"):
|
||||
@@ -203,7 +193,6 @@ def get_year_by_id(
|
||||
if not result:
|
||||
return None
|
||||
|
||||
# Cache if possible
|
||||
if title_cacher and cache_title_id and result.raw:
|
||||
try:
|
||||
ext_ids = tmdb.get_external_ids(tmdb_id, kind)
|
||||
@@ -253,7 +242,6 @@ def fetch_external_ids(
|
||||
cache_account_hash: Optional[str] = None,
|
||||
) -> ExternalIds:
|
||||
"""Get external IDs by TMDB ID."""
|
||||
# Check cache first
|
||||
if title_cacher and cache_title_id:
|
||||
cached = title_cacher.get_cached_provider("tmdb", cache_title_id, kind, cache_region, cache_account_hash)
|
||||
if cached and cached.get("external_ids"):
|
||||
@@ -271,7 +259,6 @@ def fetch_external_ids(
|
||||
return ExternalIds()
|
||||
ext = tmdb.get_external_ids(tmdb_id, kind)
|
||||
|
||||
# Cache if possible
|
||||
if title_cacher and cache_title_id:
|
||||
try:
|
||||
detail = None
|
||||
@@ -373,9 +360,6 @@ def resolve_by_ids(
|
||||
return result
|
||||
|
||||
|
||||
# -- Internal helpers --
|
||||
|
||||
|
||||
# trust ranking for cross-validating enrichments; `metadata_providers` filters this
|
||||
# set but sets search order only, not the ranking
|
||||
ENRICHMENT_AUTHORITY: tuple[str, ...] = ("tmdb", "simkl", "tvdb")
|
||||
@@ -402,7 +386,6 @@ def enrich_ids(result: MetadataResult) -> None:
|
||||
|
||||
kind = result.kind or "movie"
|
||||
|
||||
# Step 1: Collect enrichment results from all available providers
|
||||
authority = {name: i for i, name in enumerate(enrichment_providers(kind))}
|
||||
enrichments: list[tuple[str, ExternalIds]] = []
|
||||
for provider_name in authority:
|
||||
@@ -420,10 +403,8 @@ def enrich_ids(result: MetadataResult) -> None:
|
||||
if not enrichments:
|
||||
return
|
||||
|
||||
# Step 2: Cross-validate using tmdb_id as anchor — drop providers that disagree
|
||||
validated = validate_enrichments(enrichments, authority)
|
||||
|
||||
# Step 3: Merge validated data (fill gaps only)
|
||||
for _provider_name, ext in validated:
|
||||
if not ids.tmdb_id and ext.tmdb_id:
|
||||
ids.tmdb_id = ext.tmdb_id
|
||||
@@ -444,7 +425,6 @@ def validate_enrichments(
|
||||
"""
|
||||
from collections import Counter
|
||||
|
||||
# Collect tmdb_id votes
|
||||
tmdb_votes: dict[str, int] = {}
|
||||
for provider_name, ext in enrichments:
|
||||
if ext.tmdb_id is not None:
|
||||
@@ -453,7 +433,6 @@ def validate_enrichments(
|
||||
if len(set(tmdb_votes.values())) <= 1:
|
||||
return enrichments # all agree or only one voted — no conflict
|
||||
|
||||
# Find the authoritative tmdb_id
|
||||
value_counts = Counter(tmdb_votes.values())
|
||||
most_common_val, most_common_count = value_counts.most_common(1)[0]
|
||||
|
||||
@@ -467,7 +446,6 @@ def validate_enrichments(
|
||||
)
|
||||
anchor_tmdb_id = tmdb_votes[best_provider]
|
||||
|
||||
# Drop any provider that disagrees
|
||||
validated: list[tuple[str, ExternalIds]] = []
|
||||
for provider_name, ext in enrichments:
|
||||
if ext.tmdb_id is not None and ext.tmdb_id != anchor_tmdb_id:
|
||||
|
||||
@@ -38,8 +38,8 @@ class MetadataResult:
|
||||
kind: Optional[str] = None # "movie" or "tv"
|
||||
external_ids: ExternalIds = field(default_factory=ExternalIds)
|
||||
original_language: Optional[str] = None # alpha-2 or alpha-3, whichever the provider speaks
|
||||
source: str = "" # provider name, e.g. "tmdb", "simkl", "imdb"
|
||||
raw: Optional[dict] = None # original API response for caching
|
||||
source: str = ""
|
||||
raw: Optional[dict] = None
|
||||
|
||||
|
||||
class MetadataProvider(metaclass=ABCMeta):
|
||||
|
||||
@@ -9,7 +9,6 @@ from langcodes import Language
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, clean, fuzzy_match
|
||||
|
||||
# Mapping from our kind ("movie"/"tv") to OMDb title types
|
||||
KIND_TO_TYPE: dict[str, str] = {
|
||||
"movie": "movie",
|
||||
"tv": "series",
|
||||
|
||||
@@ -21,7 +21,6 @@ class SimklProvider(MetadataProvider):
|
||||
def search(self, title: str, year: Optional[int], kind: str) -> Optional[MetadataResult]:
|
||||
self.log.debug("Searching Simkl for %r (%s, %s)", title, kind, year)
|
||||
|
||||
# Construct appropriate filename based on type
|
||||
filename = f"{title}"
|
||||
if year:
|
||||
filename = f"{title} {year}"
|
||||
@@ -42,7 +41,6 @@ class SimklProvider(MetadataProvider):
|
||||
self.log.debug("Simkl search failed: %s", exc)
|
||||
return None
|
||||
|
||||
# Handle case where SIMKL returns empty list (no results)
|
||||
if isinstance(data, list):
|
||||
self.log.debug("Simkl returned list (no matches) for %r", filename)
|
||||
return None
|
||||
@@ -77,11 +75,9 @@ class SimklProvider(MetadataProvider):
|
||||
if not simkl_id:
|
||||
return None
|
||||
|
||||
# Map SIMKL type to endpoint
|
||||
simkl_type = entry.get("type", "")
|
||||
endpoint = "tv" if simkl_type in ("tv", "anime") else "movies"
|
||||
|
||||
# Fetch full details to get cross-referenced IDs
|
||||
try:
|
||||
r2 = self.session.get(
|
||||
f"{self.BASE_URL}/{endpoint}/{simkl_id}",
|
||||
|
||||
@@ -72,7 +72,6 @@ class TMDBProvider(MetadataProvider):
|
||||
if best_id is None:
|
||||
return None
|
||||
|
||||
# Fetch full detail for caching
|
||||
detail = self.fetch_detail(best_id, kind)
|
||||
ext_raw = self.fetch_external_ids_raw(best_id, kind)
|
||||
|
||||
@@ -127,7 +126,12 @@ class TMDBProvider(MetadataProvider):
|
||||
)
|
||||
|
||||
def find_by_imdb_id(self, imdb_id: str, kind: str) -> Optional[ExternalIds]:
|
||||
"""Look up TMDB/TVDB IDs from an IMDB ID using TMDB's /find endpoint."""
|
||||
"""Look up TMDB/TVDB IDs from an IMDB ID using TMDB's /find endpoint.
|
||||
|
||||
When the requested kind has no results, the other kind is tried, so the returned
|
||||
tmdb_kind can differ from the kind asked for. Returns None if the request fails or
|
||||
nothing matches.
|
||||
"""
|
||||
self.log.debug("Looking up IMDB ID %s on TMDB", imdb_id)
|
||||
try:
|
||||
r = self.session.get(
|
||||
@@ -141,14 +145,12 @@ class TMDBProvider(MetadataProvider):
|
||||
self.log.debug("TMDB find by IMDB ID failed: %s", exc)
|
||||
return None
|
||||
|
||||
# Check movie_results or tv_results based on kind
|
||||
if kind == "movie":
|
||||
results = data.get("movie_results") or []
|
||||
else:
|
||||
results = data.get("tv_results") or []
|
||||
|
||||
if not results:
|
||||
# Try the other type as fallback
|
||||
fallback_key = "tv_results" if kind == "movie" else "movie_results"
|
||||
results = data.get(fallback_key) or []
|
||||
if results:
|
||||
@@ -165,7 +167,6 @@ class TMDBProvider(MetadataProvider):
|
||||
|
||||
self.log.debug("TMDB find -> ID %s (%s) for IMDB %s", tmdb_id, kind, imdb_id)
|
||||
|
||||
# Now fetch the full external IDs from TMDB to get TVDB etc.
|
||||
ext_raw = self.fetch_external_ids_raw(tmdb_id, kind)
|
||||
|
||||
return ExternalIds(
|
||||
|
||||
Reference in New Issue
Block a user