feat(providers): fill the western IDs of an AniList match from the anime-lists ID map

This commit is contained in:
imSp4rky
2026-11-29 10:47:22 -07:00
parent efd8ca65c7
commit 6758d9e4fa
13 changed files with 370 additions and 19 deletions
+100
View File
@@ -8,10 +8,16 @@ import pytest
from unshackle.core import providers
from unshackle.core.config import config
from unshackle.core.providers import anilist
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult
search_metadata = providers.search_metadata
ANIME_MAP = {
"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797},
"199": {"imdb": "tt0245429", "tmdb_movie": 129},
}
class FakeProvider(MetadataProvider):
"""Stands in for a real provider; records the ID it was asked for."""
@@ -330,3 +336,97 @@ def test_all_supplied_ids_survive_together(monkeypatch: pytest.MonkeyPatch) -> N
assert result is not None
assert result.external_ids.tvdb_id == 81797
assert result.external_ids.anilist_id == 21
@pytest.fixture
def anime_map(monkeypatch: pytest.MonkeyPatch) -> list[bool]:
"""A stubbed AniList ID map; returns one entry per map read."""
reads: list[bool] = []
monkeypatch.setattr(anilist, "western_id_map", lambda: reads.append(True) or ANIME_MAP)
monkeypatch.setattr(providers, "get_provider", lambda name: None)
return reads
def anilist_hit(anilist_id: int = 21, kind: str = "tv", **ids: Any) -> MetadataResult:
return MetadataResult(title="One Piece", kind=kind, external_ids=ExternalIds(anilist_id=anilist_id, **ids))
@pytest.mark.parametrize(
("hit", "kind", "expected"),
[
(anilist_hit(), "tv", ExternalIds("tt0388629", 37854, "tv", 81797, 21)),
(anilist_hit(199, "movie"), "movie", ExternalIds("tt0245429", 129, "movie", None, 199)),
(anilist_hit(tmdb_id=37854, tmdb_kind="tv"), "tv", ExternalIds("tt0388629", 37854, "tv", 81797, 21)),
(anilist_hit(tvdb_id=1), "tv", ExternalIds(tvdb_id=1, anilist_id=21)),
(anilist_hit(), "movie", ExternalIds(anilist_id=21)),
(anilist_hit(199, "movie"), "tv", ExternalIds(anilist_id=199)),
],
ids=["series", "movie", "agreeing-id-fills-the-rest", "other-id-wins", "tv-node-for-a-movie", "movie-node-for-tv"],
)
def test_an_anilist_match_takes_the_mapped_western_ids(
anime_map: list[bool], hit: MetadataResult, kind: str, expected: ExternalIds
) -> None:
providers.fill_anime_ids(hit, kind)
assert hit.external_ids == expected
@pytest.mark.parametrize(
"ids",
[ExternalIds("tt1", 5, "tv", 7, 21), ExternalIds(imdb_id="tt1"), ExternalIds()],
ids=["all-western-ids", "not-anilist", "no-ids"],
)
def test_the_map_is_not_read_without_need(anime_map: list[bool], ids: ExternalIds) -> None:
before = ExternalIds(**vars(ids))
providers.fill_anime_ids(MetadataResult(kind="tv", external_ids=ids), "tv")
assert (anime_map, ids) == ([], before)
def test_a_supplied_anilist_id_takes_the_mapped_western_ids(
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
) -> None:
patch_providers(monkeypatch, {"anilist": FakeProvider("anilist", anilist_hit())})
result = providers.resolve_by_ids(anilist_id=21, kind="tv")
assert result is not None
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)
def test_the_tagging_lookup_fills_the_rest_without_a_tmdb_key(
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
) -> None:
patch_providers(monkeypatch, {"anilist": FakeProvider("anilist", anilist_hit())})
result = providers.resolve_by_ids(37854, anilist_id=21, kind="tv")
assert result is not None
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)
def test_a_supplied_western_id_wins_over_the_map(monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]) -> None:
patch_providers(monkeypatch, {"anilist": FakeProvider("anilist", anilist_hit())})
result = providers.resolve_by_ids(tvdb_id=1, anilist_id=21, kind="tv")
assert result is not None
assert result.external_ids == ExternalIds(tvdb_id=1, anilist_id=21)
class AniListHit(FakeProvider):
def search(self, title: str, year: int | None, kind: str) -> MetadataResult | None:
return anilist_hit()
def test_a_search_hit_on_anilist_takes_the_mapped_western_ids(
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
) -> None:
monkeypatch.setattr(config, "disable_metadata", False)
monkeypatch.setattr(providers, "provider_order", lambda *a: [lambda: AniListHit("anilist")])
result = search_metadata("One Piece", None, "tv")
assert result is not None
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)