feat(providers): let a service name the metadata provider its catalogue resolves through

This commit is contained in:
imSp4rky
2026-11-29 09:42:39 -07:00
parent 8e67081e11
commit 36fd0033ce
18 changed files with 260 additions and 30 deletions
+21 -6
View File
@@ -34,22 +34,33 @@ ALIASES: dict[str, str] = {"imdbapi": "imdb"}
DEFAULT_ORDER: tuple[str, ...] = ("imdb", "omdb", "simkl", "tmdb", "tvdb", "anilist")
def provider_order(kind: str | None = None, anime: bool = False) -> list[type[MetadataProvider]]:
def provider_name(name: object) -> str:
"""A `metadata_providers` entry as its registry name."""
return ALIASES.get(str(name).lower(), str(name).lower())
def provider_order(
kind: str | None = None, anime: bool = False, prefer: str | None = None
) -> list[type[MetadataProvider]]:
"""Metadata provider classes in the order configured by `metadata_providers`.
`metadata_providers` is either a flat list applying to both kinds, or a mapping of
kind ("tv"/"movie") to its own list. A kind the mapping omits uses `DEFAULT_ORDER`.
An `anime` title puts AniList first. The rest of the order stays behind it to
fall back on.
fall back on. `prefer` (a service's `METADATA_PROVIDER`) moves that metadata provider
in front of both, so an anime title asks it first and AniList second. Neither hint adds a
metadata provider the config left out.
"""
from unshackle.core.config import config
configured = config.metadata_providers
selected = (configured.get(kind) if kind else None) if isinstance(configured, dict) else configured
names = [ALIASES.get(str(n).lower(), str(n).lower()) for n in (selected or DEFAULT_ORDER)]
names = [provider_name(n) for n in (selected or DEFAULT_ORDER)]
if anime and AniListProvider.NAME in names:
names = [AniListProvider.NAME, *names]
if prefer and (wanted := provider_name(prefer)) in names:
names = [wanted, *names]
unknown = [n for n in names if n not in REGISTRY]
if unknown:
log.warning("Ignoring unknown metadata_providers entries: %s", ", ".join(unknown))
@@ -79,6 +90,7 @@ def search_metadata(
cache_region: str | None = None,
cache_account_hash: str | None = None,
anime: bool = False,
prefer: str | None = None,
) -> MetadataResult | None:
"""Find metadata in every available metadata provider. Returns the best result."""
from unshackle.core.config import config
@@ -89,7 +101,7 @@ def search_metadata(
log.debug("Metadata lookups are disabled by config; not searching for %r", title)
return None
ordered = provider_order(kind, anime)
ordered = provider_order(kind, anime, prefer)
if title_cacher and cache_title_id:
for cls in ordered:
@@ -278,6 +290,7 @@ def resolve_by_ids(
cache_region: str | None = None,
cache_account_hash: str | None = None,
anime: bool = False,
prefer: str | None = None,
) -> MetadataResult | None:
"""Get metadata from user-supplied external IDs, and find the title by name only without them.
@@ -299,10 +312,12 @@ def resolve_by_ids(
if not supplied:
if not title:
return None
return search_metadata(title, year, kind, title_cacher, cache_title_id, cache_region, cache_account_hash, anime)
return search_metadata(
title, year, kind, title_cacher, cache_title_id, cache_region, cache_account_hash, anime, prefer=prefer
)
result: MetadataResult | None = None
for cls in provider_order(kind, anime):
for cls in provider_order(kind, anime, prefer):
provider_id = supplied.get(cls.ID_KIND or "")
if provider_id is None:
continue