forked from kenzuya/unshackle
feat(providers): fill the AniList ID of a TMDB or TVDB match from the anime-lists ID map
This commit is contained in:
@@ -364,3 +364,87 @@ def test_western_ids_never_cross_the_kind(
|
||||
id_map: list[str], anilist_id: int, kind: str, expected: anilist.ExternalIds
|
||||
) -> None:
|
||||
assert anilist.western_ids(anilist_id, kind) == expected
|
||||
|
||||
|
||||
def test_a_map_entry_keeps_its_season_numbers() -> None:
|
||||
entry = {"themoviedb_id": {"tv": 1429}, "tvdb_id": 267440, "season": {"tvdb": 0, "tmdb": 2}}
|
||||
|
||||
assert anilist.reduce_map_entry(entry) == {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 2, "tvdb_season": 0}
|
||||
assert anilist.reduce_map_entry({"season": {"tvdb": 1}}) == {}
|
||||
|
||||
|
||||
REVERSE_MAP = {
|
||||
"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797},
|
||||
"459": {"tmdb_movie": 19576, "tvdb": 81797, "tvdb_season": 0},
|
||||
"460": {"tmdb_movie": 23446, "tvdb": 81797},
|
||||
"527": {"tmdb_tv": 60572},
|
||||
"1564": {"tmdb_tv": 60572, "tmdb_season": 6},
|
||||
"700": {"tmdb_tv": 7, "tvdb": 8, "tmdb_season": 1, "tvdb_season": 1},
|
||||
"701": {"tmdb_tv": 7, "tvdb": 8, "tmdb_season": 1, "tvdb_season": 2},
|
||||
"900": {"tmdb_tv": 9, "tvdb": 10, "tmdb_season": 2, "tvdb_season": 1},
|
||||
"901": {"tmdb_tv": 9, "tvdb": 10, "tmdb_season": 1, "tvdb_season": 2},
|
||||
"16498": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 1, "tvdb_season": 1},
|
||||
"20958": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 2, "tvdb_season": 2},
|
||||
"99147": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 3, "tvdb_season": 3},
|
||||
"104578": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 3, "tvdb_season": 3},
|
||||
"199": {"imdb": "tt0245429", "tmdb_movie": 129},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("ids", "kind", "season", "expected"),
|
||||
[
|
||||
(anilist.ExternalIds(tmdb_id=37854), "tv", None, 21),
|
||||
(anilist.ExternalIds(tvdb_id=81797), "tv", 1, 21),
|
||||
(anilist.ExternalIds(tmdb_id=1429, tvdb_id=267440), "tv", 2, 20958),
|
||||
(anilist.ExternalIds(tmdb_id=1429), "tv", None, None),
|
||||
(anilist.ExternalIds(tmdb_id=1429), "tv", 3, None),
|
||||
(anilist.ExternalIds(tmdb_id=1429), "tv", 5, None),
|
||||
(anilist.ExternalIds(tmdb_id=37854, tvdb_id=1), "tv", None, None),
|
||||
(anilist.ExternalIds(imdb_id="tt9", tmdb_id=37854), "tv", None, None),
|
||||
(anilist.ExternalIds(tmdb_id=129), "movie", None, 199),
|
||||
(anilist.ExternalIds(tmdb_id=37854), "movie", None, None),
|
||||
(anilist.ExternalIds(tmdb_id=129), "tv", None, None),
|
||||
(anilist.ExternalIds(tvdb_id=81797), "movie", None, None),
|
||||
(anilist.ExternalIds(tvdb_id=81797), "tv", None, 21),
|
||||
(anilist.ExternalIds(tmdb_id=19576), "movie", None, 459),
|
||||
(anilist.ExternalIds(tmdb_id=60572), "tv", None, None),
|
||||
(anilist.ExternalIds(tmdb_id=60572), "tv", 6, 1564),
|
||||
(anilist.ExternalIds(tmdb_id=60572), "tv", 3, None),
|
||||
(anilist.ExternalIds(tmdb_id=7, tvdb_id=8), "tv", 1, 700),
|
||||
(anilist.ExternalIds(tmdb_id=7, tvdb_id=8), "tv", 2, 701),
|
||||
(anilist.ExternalIds(tmdb_id=7), "tv", 1, None),
|
||||
(anilist.ExternalIds(tmdb_id=9, tvdb_id=10), "tv", 1, None),
|
||||
(anilist.ExternalIds(tmdb_id=9), "tv", 1, 901),
|
||||
],
|
||||
ids=[
|
||||
"series-entry",
|
||||
"movie-entries-on-the-same-tvdb-drop-out",
|
||||
"season-picks-one",
|
||||
"several-seasons-without-a-season",
|
||||
"two-entries-for-one-season",
|
||||
"no-entry-for-the-season",
|
||||
"tvdb-disagrees",
|
||||
"imdb-disagrees",
|
||||
"movie",
|
||||
"tv-id-for-a-movie",
|
||||
"movie-id-for-a-series",
|
||||
"tvdb-id-for-a-movie",
|
||||
"movie-entries-without-a-season-drop-out",
|
||||
"movie-entry-with-a-season",
|
||||
"whole-series-entry-beside-season-entries",
|
||||
"season-entry-beside-a-whole-series-entry",
|
||||
"whole-series-entry-of-a-split-series",
|
||||
"tvdb-numbering-picks-season-1",
|
||||
"tvdb-numbering-picks-season-2",
|
||||
"tmdb-numbering-alone-is-ambiguous",
|
||||
"tmdb-and-tvdb-numbering-disagree",
|
||||
"tmdb-numbering-picks",
|
||||
],
|
||||
)
|
||||
def test_a_western_match_takes_the_one_anilist_entry_that_fits(
|
||||
monkeypatch: pytest.MonkeyPatch, ids: anilist.ExternalIds, kind: str, season: int | None, expected: int | None
|
||||
) -> None:
|
||||
monkeypatch.setattr(anilist, "western_id_map", lambda: REVERSE_MAP)
|
||||
|
||||
assert anilist.anilist_id_for(ids, kind, season) == expected
|
||||
|
||||
@@ -16,6 +16,8 @@ search_metadata = providers.search_metadata
|
||||
ANIME_MAP = {
|
||||
"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797},
|
||||
"199": {"imdb": "tt0245429", "tmdb_movie": 129},
|
||||
"16498": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 1, "tvdb_season": 1},
|
||||
"20958": {"tmdb_tv": 1429, "tvdb": 267440, "tmdb_season": 2, "tvdb_season": 2},
|
||||
}
|
||||
|
||||
|
||||
@@ -415,18 +417,117 @@ def test_a_supplied_western_id_wins_over_the_map(monkeypatch: pytest.MonkeyPatch
|
||||
assert result.external_ids == ExternalIds(tvdb_id=1, anilist_id=21)
|
||||
|
||||
|
||||
class AniListHit(FakeProvider):
|
||||
class SearchHit(FakeProvider):
|
||||
def search(self, title: str, year: int | None, kind: str) -> MetadataResult | None:
|
||||
return anilist_hit()
|
||||
return self._result
|
||||
|
||||
|
||||
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")])
|
||||
monkeypatch.setattr(providers, "provider_order", lambda *a: [lambda: SearchHit("anilist", anilist_hit())])
|
||||
|
||||
result = search_metadata("One Piece", None, "tv")
|
||||
|
||||
assert result is not None
|
||||
assert result.external_ids == ExternalIds("tt0388629", 37854, "tv", 81797, 21)
|
||||
|
||||
|
||||
def western_hit(language: str | None = "ja", kind: str = "tv", **ids: Any) -> MetadataResult:
|
||||
ids = {"tmdb_id": 37854, "tmdb_kind": kind, **ids}
|
||||
return MetadataResult(title="One Piece", kind=kind, original_language=language, external_ids=ExternalIds(**ids))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hit", "kind", "anime", "season", "expected"),
|
||||
[
|
||||
(western_hit(), "tv", False, None, 21),
|
||||
(western_hit("jpn"), "tv", False, None, 21),
|
||||
(western_hit(None), "tv", True, None, 21),
|
||||
(western_hit(tmdb_id=1429), "tv", False, 2, 20958),
|
||||
(western_hit(tmdb_id=1429), "tv", False, None, None),
|
||||
(western_hit(tmdb_id=129, kind="movie"), "movie", False, None, 199),
|
||||
(western_hit(tmdb_kind="movie"), "tv", False, None, None),
|
||||
(
|
||||
MetadataResult(kind="movie", original_language="ja", external_ids=ExternalIds(tmdb_id=37854)),
|
||||
"tv",
|
||||
False,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(western_hit(anilist_id=7), "tv", False, None, 7),
|
||||
],
|
||||
ids=[
|
||||
"ja",
|
||||
"alpha-3",
|
||||
"anime-run",
|
||||
"season",
|
||||
"no-season",
|
||||
"movie",
|
||||
"movie-id-on-tv",
|
||||
"movie-match-for-tv",
|
||||
"own-anilist-id-kept",
|
||||
],
|
||||
)
|
||||
def test_a_western_match_takes_its_mapped_anilist_id(
|
||||
anime_map: list[bool], hit: MetadataResult, kind: str, anime: bool, season: int | None, expected: int | None
|
||||
) -> None:
|
||||
providers.fill_anilist_id(hit, kind, anime, season)
|
||||
|
||||
assert hit.external_ids.anilist_id == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"hit",
|
||||
[western_hit("en"), western_hit(None), western_hit(anilist_id=7), western_hit(tmdb_id=None, tmdb_kind=None)],
|
||||
ids=["english", "no-language", "has-anilist-id", "no-western-id"],
|
||||
)
|
||||
def test_a_match_outside_anime_never_reads_the_map(anime_map: list[bool], hit: MetadataResult) -> None:
|
||||
providers.fill_anilist_id(hit, "tv")
|
||||
|
||||
assert anime_map == []
|
||||
|
||||
|
||||
def test_the_tagging_lookup_picks_the_anilist_entry_of_the_season(
|
||||
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
||||
) -> None:
|
||||
patch_providers(monkeypatch, {"tmdb": FakeProvider("tmdb", western_hit(tmdb_id=1429))})
|
||||
|
||||
result = providers.resolve_by_ids(1429, kind="tv", prefer="tmdb", season=2)
|
||||
|
||||
assert result is not None
|
||||
assert result.external_ids.anilist_id == 20958
|
||||
|
||||
|
||||
def test_a_supplied_anilist_id_wins_over_the_mapped_one(monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]) -> None:
|
||||
patch_providers(monkeypatch, {"tmdb": FakeProvider("tmdb", western_hit(tmdb_id=1429))})
|
||||
|
||||
result = providers.resolve_by_ids(1429, anilist_id=16498, kind="tv", prefer="tmdb", season=2)
|
||||
|
||||
assert result is not None
|
||||
assert result.external_ids.anilist_id == 16498
|
||||
|
||||
|
||||
def test_a_search_hit_on_tmdb_takes_the_mapped_anilist_id(
|
||||
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
||||
) -> None:
|
||||
monkeypatch.setattr(config, "disable_metadata", False)
|
||||
monkeypatch.setattr(providers, "provider_order", lambda *a: [lambda: SearchHit("tmdb", western_hit())])
|
||||
monkeypatch.setattr(providers, "search_metadata", search_metadata)
|
||||
|
||||
result = providers.resolve_by_ids(title="One Piece", kind="tv")
|
||||
|
||||
assert result is not None
|
||||
assert result.external_ids == ExternalIds(None, 37854, "tv", None, 21)
|
||||
|
||||
|
||||
def test_the_tagging_search_picks_the_anilist_entry_of_the_season(
|
||||
monkeypatch: pytest.MonkeyPatch, anime_map: list[bool]
|
||||
) -> None:
|
||||
monkeypatch.setattr(providers, "search_metadata", lambda *a, **kw: western_hit(tmdb_id=1429))
|
||||
|
||||
result = providers.resolve_by_ids(title="EXAMPLE", kind="tv", season=2)
|
||||
|
||||
assert result is not None
|
||||
assert result.external_ids.anilist_id == 20958
|
||||
|
||||
@@ -152,3 +152,24 @@ def test_the_tag_lookup_passes_the_service_provider(calls: Calls, monkeypatch: p
|
||||
)
|
||||
|
||||
assert seen and seen[0]["prefer"] == "tmdb"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("title", "season"),
|
||||
[
|
||||
(Episode(id_="episode-0001", service=DummyService, title="Some Show", season=3, number=2), 3),
|
||||
(Movie(id_="movie-0001", service=DummyService, name="Some Movie"), None),
|
||||
],
|
||||
ids=["episode", "movie"],
|
||||
)
|
||||
def test_the_tag_lookup_passes_the_season(
|
||||
calls: Calls, monkeypatch: pytest.MonkeyPatch, title: Any, season: int | None
|
||||
) -> None:
|
||||
seen: list[dict[str, Any]] = []
|
||||
monkeypatch.setattr(config, "tag_imdb_tmdb", True)
|
||||
monkeypatch.setattr(tags_module, "get_available_providers", lambda: [object()])
|
||||
monkeypatch.setattr(tags_module, "resolve_by_ids", lambda *a, **kw: seen.append(kw) or None)
|
||||
|
||||
tags_module.tag_file(Path("Some Title.mkv"), title)
|
||||
|
||||
assert seen and seen[0]["season"] == season
|
||||
|
||||
Reference in New Issue
Block a user