mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 20:45:11 +00:00
feat(providers): fill the western IDs of an AniList match from the anime-lists ID map
This commit is contained in:
@@ -14,6 +14,7 @@ import pytest
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core import session as http_session
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.providers import anilist
|
||||
from unshackle.core.proxies.proxy import CATALOGUES
|
||||
from unshackle.core.update_checker import UpdateChecker
|
||||
|
||||
@@ -46,6 +47,12 @@ def no_update_check(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(UpdateChecker, "check_for_updates_sync", lambda *args, **kwargs: None)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_anime_id_map(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Keep the AniList ID map download and its cache file out of every test."""
|
||||
monkeypatch.setattr(anilist, "western_id_map", dict)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def client_process(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Start as a client process, so a test that ran serve or a job worker cannot turn response bodies off."""
|
||||
|
||||
@@ -2,13 +2,21 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from unshackle.core.cacher import Cacher
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.providers import anilist
|
||||
from unshackle.core.providers.anilist import AniListProvider, parse_anilist_ref
|
||||
|
||||
real_id_map = anilist.western_id_map
|
||||
|
||||
ONE_PIECE = {
|
||||
"id": 21,
|
||||
"idMal": 21,
|
||||
@@ -29,10 +37,13 @@ ONE_PIECE_MOVIE = {
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, body: dict) -> None:
|
||||
def __init__(self, body: Any) -> None:
|
||||
self._body = body
|
||||
|
||||
def json(self) -> dict:
|
||||
def raise_for_status(self) -> None:
|
||||
pass
|
||||
|
||||
def json(self) -> Any:
|
||||
return self._body
|
||||
|
||||
|
||||
@@ -258,3 +269,98 @@ def test_a_cached_node_rebuilds_with_the_read_time_title_language(monkeypatch: p
|
||||
assert result.kind == "tv"
|
||||
assert result.external_ids.anilist_id == 21
|
||||
assert result.original_language == "ja"
|
||||
|
||||
|
||||
def test_a_cached_node_keeps_the_ids_enrichment_filled() -> None:
|
||||
from unshackle.core.providers import cached_to_result
|
||||
|
||||
enriched = {"imdb_id": "tt0388629", "tmdb_id": 37854, "tmdb_kind": "tv", "tvdb_id": 81797, "anilist_id": 21}
|
||||
result = cached_to_result({**ONE_PIECE, "_enriched_ids": enriched}, "anilist", "tv")
|
||||
|
||||
assert result is not None
|
||||
assert result.external_ids == anilist.ExternalIds("tt0388629", 37854, "tv", 81797, 21)
|
||||
|
||||
|
||||
MAP_ENTRIES: list[Any] = [
|
||||
{"anilist_id": 21, "imdb_id": ["tt0388629"], "themoviedb_id": {"tv": 37854}, "tvdb_id": 81797},
|
||||
{"anilist_id": 199, "imdb_id": ["tt0245429"], "themoviedb_id": {"movie": [129]}},
|
||||
{"anilist_id": 3269, "imdb_id": ["tt1164545"], "themoviedb_id": {"tv": 8864}, "tvdb_id": 79099},
|
||||
{"anilist_id": 500, "imdb_id": ["tt1", "tt2"], "themoviedb_id": {"movie": [1, 2]}},
|
||||
{"anilist_id": 600, "tvdb_id": "81797", "themoviedb_id": [37854], "imdb_id": ["tt12x"]},
|
||||
{"mal_id": 7, "tvdb_id": 1},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def id_map(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Iterator[list[str]]:
|
||||
"""The real ID map loader with its cache in tmp_path and its download stubbed; returns the fetched URLs."""
|
||||
fetched: list[str] = []
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(
|
||||
requests.Session, "get", lambda self, url, **kwargs: fetched.append(url) or FakeResponse(MAP_ENTRIES)
|
||||
)
|
||||
monkeypatch.setattr(anilist, "western_id_map", real_id_map)
|
||||
real_id_map.cache_clear()
|
||||
yield fetched
|
||||
real_id_map.cache_clear()
|
||||
|
||||
|
||||
def test_the_id_map_keeps_only_unambiguous_western_ids(id_map: list[str]) -> None:
|
||||
assert anilist.western_id_map() == {
|
||||
"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797},
|
||||
"199": {"imdb": "tt0245429", "tmdb_movie": 129},
|
||||
"3269": {"imdb": "tt1164545", "tmdb_tv": 8864, "tvdb": 79099},
|
||||
}
|
||||
|
||||
|
||||
def test_the_id_map_downloads_once_and_then_reads_the_cache(id_map: list[str]) -> None:
|
||||
first = anilist.western_id_map()
|
||||
real_id_map.cache_clear()
|
||||
|
||||
assert anilist.western_id_map() == first
|
||||
assert id_map == [anilist.ID_MAP_URL]
|
||||
|
||||
|
||||
def test_a_failed_download_gives_an_empty_map(monkeypatch: pytest.MonkeyPatch, id_map: list[str]) -> None:
|
||||
def down(self: requests.Session, url: str, **kwargs: Any) -> FakeResponse:
|
||||
raise requests.ConnectionError("down")
|
||||
|
||||
monkeypatch.setattr(requests.Session, "get", down)
|
||||
|
||||
assert anilist.western_id_map() == {}
|
||||
|
||||
|
||||
def test_an_expired_map_still_answers_when_the_download_fails(
|
||||
monkeypatch: pytest.MonkeyPatch, id_map: list[str]
|
||||
) -> None:
|
||||
stale = {"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797}}
|
||||
Cacher("global", anilist.ID_MAP_CACHE_KEY).set(stale, expiration=datetime.now() - timedelta(days=1))
|
||||
monkeypatch.setattr(requests.Session, "get", lambda self, url, **kwargs: FakeResponse("not a list"))
|
||||
|
||||
assert anilist.western_id_map() == stale
|
||||
|
||||
|
||||
def test_a_damaged_map_cache_is_downloaded_again(id_map: list[str]) -> None:
|
||||
path = Cacher("global", anilist.ID_MAP_CACHE_KEY).path
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("{}")
|
||||
|
||||
assert "21" in anilist.western_id_map()
|
||||
assert id_map == [anilist.ID_MAP_URL]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("anilist_id", "kind", "expected"),
|
||||
[
|
||||
(21, "tv", anilist.ExternalIds("tt0388629", 37854, "tv", 81797)),
|
||||
(199, "movie", anilist.ExternalIds("tt0245429", 129, "movie")),
|
||||
(199, "tv", anilist.ExternalIds("tt0245429")),
|
||||
(3269, "movie", anilist.ExternalIds("tt1164545")),
|
||||
(500, "movie", anilist.ExternalIds()),
|
||||
(999, "tv", anilist.ExternalIds()),
|
||||
],
|
||||
)
|
||||
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
|
||||
|
||||
@@ -6,6 +6,7 @@ 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
|
||||
|
||||
|
||||
@@ -63,3 +64,16 @@ def test_no_ids_returns_none(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(providers, "get_provider", lambda name: FakeProvider(name))
|
||||
|
||||
assert providers.resolve_by_ids(title="Futurama", year=1999, kind="tv") is None
|
||||
|
||||
|
||||
def test_a_supplied_anilist_id_still_takes_the_mapped_western_ids(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
anilist_provider = FakeProvider("anilist", MetadataResult(kind="tv", external_ids=ExternalIds(anilist_id=21)))
|
||||
monkeypatch.setattr(providers, "get_provider", lambda name: {"anilist": anilist_provider}.get(name))
|
||||
monkeypatch.setattr(
|
||||
anilist, "western_id_map", lambda: {"21": {"imdb": "tt0388629", "tmdb_tv": 37854, "tvdb": 81797}}
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -239,6 +239,19 @@ def test_a_listing_prints_the_search_match(monkeypatch: pytest.MonkeyPatch) -> N
|
||||
assert "Search -> The Show (ID 1396)" in captured.get()
|
||||
|
||||
|
||||
def test_an_anilist_search_hit_keeps_its_anilist_id_for_the_tags(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
hit = MetadataResult(
|
||||
title="The Show", external_ids=ExternalIds(tmdb_id=37854, tmdb_kind="tv", anilist_id=21), source="anilist"
|
||||
)
|
||||
Providers(monkeypatch, hit)
|
||||
monkeypatch.setattr(providers, "result_matches", lambda result, title: result.title == title)
|
||||
metadata = TitleMetadata()
|
||||
with console.capture() as captured:
|
||||
metadata.search(episode(1, 1), listing=True)
|
||||
assert (metadata.tmdb_id, metadata.anilist_id) == (37854, 21)
|
||||
assert "Search -> The Show (ID 37854)" in captured.get()
|
||||
|
||||
|
||||
def test_a_listing_with_no_movie_match_says_so(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
Providers(monkeypatch, None)
|
||||
with console.capture() as captured:
|
||||
|
||||
Reference in New Issue
Block a user