feat(providers): fill the AniList ID of a TMDB or TVDB match from the anime-lists ID map

This commit is contained in:
imSp4rky
2026-11-29 11:29:31 -07:00
parent d2eea0c4c5
commit 1cbaa2d307
7 changed files with 324 additions and 13 deletions
+27 -2
View File
@@ -12,12 +12,19 @@ from unshackle.core.providers._base import (
log,
result_matches,
)
from unshackle.core.providers.anilist import AniListProvider, parse_anilist_ref, western_ids
from unshackle.core.providers.anilist import (
COUNTRY_TO_LANGUAGE,
AniListProvider,
anilist_id_for,
parse_anilist_ref,
western_ids,
)
from unshackle.core.providers.imdb import IMDBProvider, title_to_result
from unshackle.core.providers.omdb import OMDBProvider
from unshackle.core.providers.simkl import SimklProvider
from unshackle.core.providers.tmdb import TMDBProvider
from unshackle.core.providers.tvdb import TVDBProvider
from unshackle.core.utilities import spoken_language
if TYPE_CHECKING:
from unshackle.core.title_cacher import TitleCacher
@@ -292,6 +299,7 @@ def resolve_by_ids(
cache_account_hash: str | None = None,
anime: bool = False,
prefer: str | None = None,
season: int | None = None,
) -> MetadataResult | None:
"""Get metadata from user-supplied external IDs, and find the title by name only without them.
@@ -313,9 +321,12 @@ def resolve_by_ids(
if not supplied:
if not title:
return None
return search_metadata(
found = search_metadata(
title, year, kind, title_cacher, cache_title_id, cache_region, cache_account_hash, anime, prefer=prefer
)
if found:
fill_anilist_id(found, kind, anime, season)
return found
result: MetadataResult | None = None
for cls in provider_order(kind, anime, prefer):
@@ -357,6 +368,7 @@ def resolve_by_ids(
ids.tvdb_id = ext.tvdb_id
fill_anime_ids(result, kind)
enrich_ids(result)
fill_anilist_id(result, kind, anime, season)
return result
@@ -392,6 +404,19 @@ def fill_anime_ids(result: MetadataResult, kind: str) -> None:
ids.tvdb_id = ids.tvdb_id or mapped.tvdb_id
def fill_anilist_id(result: MetadataResult, kind: str, anime: bool = False, season: int | None = None) -> None:
"""Fill the AniList ID that a TMDB or TVDB match of ``kind`` lacks from the AniList ID map.
Only an anime lookup, or a match in Japanese, Korean or Chinese, reads the map, so a run outside them never downloads it.
"""
ids = result.external_ids
if ids.anilist_id or not (ids.tmdb_id or ids.tvdb_id) or (result.kind or kind) != kind:
return
if not anime and spoken_language(result.original_language) not in COUNTRY_TO_LANGUAGE.values():
return
ids.anilist_id = anilist_id_for(ids, kind, season)
def enrich_ids(result: MetadataResult) -> None:
"""Enrich a MetadataResult by cross-referencing IMDB ID with the available metadata providers.
+69 -7
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import dataclasses
import functools
import re
@@ -21,8 +22,9 @@ MAL_QUERY = f"query UnshackleAniListMal($idMal: Int) {{ Media(idMal: $idMal, typ
TITLE_LANGUAGES: tuple[str, ...] = ("english", "romaji", "native")
ID_MAP_URL = "https://raw.githubusercontent.com/Fribb/anime-lists/master/anime-list-mini.json"
ID_MAP_CACHE_KEY = "anilist_id_map"
ID_MAP_CACHE_KEY = "anilist_id_map_v2"
ID_MAP_LIFETIME = 7 * 24 * 3600
SEASON_KEYS = ("tmdb_season", "tvdb_season")
# AniList records where a title was produced, not what it is spoken in; these are the
# production countries whose anime output is reliably in the country's own language
@@ -152,9 +154,9 @@ class AniListProvider(MetadataProvider):
return result.external_ids if result else ExternalIds()
def as_id(value: object) -> int | None:
"""A positive integer ID, or None for anything else."""
return value if isinstance(value, int) and not isinstance(value, bool) and value > 0 else None
def as_id(value: object, low: int = 1) -> int | None:
"""An integer of at least ``low``, or None for anything else."""
return value if isinstance(value, int) and not isinstance(value, bool) and value >= low else None
def single(values: object) -> object:
@@ -163,17 +165,22 @@ def single(values: object) -> object:
def reduce_map_entry(entry: dict) -> dict[str, int | str]:
"""The western IDs that one anime-lists entry sets; a list that holds several IDs gives none."""
"""The western IDs and season numbers that one anime-lists entry sets; a list that holds several IDs gives none."""
tmdb = entry.get("themoviedb_id")
tmdb = tmdb if isinstance(tmdb, dict) else {}
season = entry.get("season")
season = season if isinstance(season, dict) else {}
imdb = single(entry.get("imdb_id"))
ids = {
ids: dict[str, int | str | None] = {
"imdb": imdb if isinstance(imdb, str) and re.fullmatch(r"tt\d+", imdb) else None,
"tmdb_tv": as_id(tmdb.get("tv")),
"tmdb_movie": as_id(single(tmdb.get("movie"))),
"tvdb": as_id(entry.get("tvdb_id")),
}
return {key: value for key, value in ids.items() if value}
if not any(ids.values()):
return {}
ids |= {"tmdb_season": as_id(season.get("tmdb"), 0), "tvdb_season": as_id(season.get("tvdb"), 0)}
return {key: value for key, value in ids.items() if value is not None}
@functools.cache
@@ -223,6 +230,61 @@ def western_ids(anilist_id: int, kind: str) -> ExternalIds:
)
def fits(entry: dict, ids: ExternalIds, kind: str) -> bool:
"""Whether a map entry is the entry of a western match of ``kind`` whose IDs all agree with it.
A movie entry fits only from its TMDB movie ID, and a series entry from its TMDB TV or TVDB ID.
"""
if kind == "movie":
keys: tuple[tuple[object, str], ...] = ((ids.tmdb_id, "tmdb_movie"),)
elif "tmdb_movie" in entry:
return False
else:
keys = ((ids.tmdb_id, "tmdb_tv"), (ids.tvdb_id, "tvdb"))
if not any(have and entry.get(key) == have for have, key in keys):
return False
return not any(have and entry.get(key, have) != have for have, key in ((ids.imdb_id, "imdb"), *keys))
def season_hits(hits: dict[str, dict], ids: ExternalIds, season: int | None) -> dict[str, dict]:
"""The series entries that the TMDB or the TVDB season numbering picks for ``season`` on its own.
An entry without a season covers every season only when no entry of the series names a season past 0,
since anime-lists also leaves out the season of an entry in absolute numbering.
"""
split = any(entry.get(key) for entry in hits.values() for key in SEASON_KEYS)
def covers(entry: dict, season_key: str) -> bool:
if season_key in entry:
return bool(entry[season_key] == season)
return not split and not any(key in entry for key in SEASON_KEYS)
picks: dict[str, dict] = {}
for have, id_key, season_key in ((ids.tmdb_id, "tmdb_tv", "tmdb_season"), (ids.tvdb_id, "tvdb", "tvdb_season")):
fit = {
key: entry
for key, entry in hits.items()
if have and entry.get(id_key) == have and covers(entry, season_key)
}
if len(fit) == 1:
picks |= fit
return picks
def anilist_id_for(ids: ExternalIds, kind: str, season: int | None = None) -> int | None:
"""The AniList ID that the ID map gives a western match of ``kind``, or None unless exactly one entry fits.
When the TMDB and the TVDB numbering each pick an entry, they must agree. Without a ``season``, a series
takes an ID only when the map holds one entry for it.
"""
if ids.tmdb_kind not in (None, kind):
ids = dataclasses.replace(ids, tmdb_id=None, tmdb_kind=None)
hits = {key: entry for key, entry in western_id_map().items() if fits(entry, ids, kind)}
if kind != "movie":
hits = season_hits(hits, ids, season) if season is not None or len(hits) == 1 else {}
return int(next(iter(hits))) if len(hits) == 1 else None
def media_names(node: dict) -> tuple[str, ...]:
"""Every title variant of an AniList media node that is set."""
return tuple(str(n) for n in (node.get("title") or {}).values() if n)
+10 -1
View File
@@ -147,7 +147,16 @@ def tag_file(
return
result = resolve_by_ids(
tmdb_id, imdb_id, tvdb_id, anilist_id, title=name, year=year, kind=kind, anime=anime, prefer=prefer
tmdb_id,
imdb_id,
tvdb_id,
anilist_id,
title=name,
year=year,
kind=kind,
anime=anime,
prefer=prefer,
season=title.season if isinstance(title, Episode) else None,
)
if result and result.external_ids: