fix(providers): match a later season's year and TVDB translated names in metadata searches

This commit is contained in:
imSp4rky
2026-11-29 11:18:48 -07:00
parent 1571236991
commit d2eea0c4c5
8 changed files with 194 additions and 26 deletions
+62 -1
View File
@@ -3,9 +3,10 @@ import pytest
from unshackle.core import providers
from unshackle.core.config import config
from unshackle.core.providers import MetadataResult, fuzzy_match, result_matches
from unshackle.core.providers._base import clean, title_ratio
from unshackle.core.providers._base import clean, strip_year, title_ratio
from unshackle.core.providers.anilist import AniListProvider
from unshackle.core.providers.imdb import IMDBProvider
from unshackle.core.providers.tmdb import TMDBProvider
IMDB_NODE = {
"id": "tt2560140",
@@ -135,3 +136,63 @@ def test_search_metadata_accepts_an_anilist_result_found_by_another_title_varian
result = providers.search_metadata("Shingeki no Kyojin", None, "tv", anime=True)
assert result is not None and (result.title, result.external_ids.anilist_id) == ("Attack on Titan", 16498)
@pytest.mark.parametrize(
("title", "year", "expected"),
[
("EXAMPLE (2019)", None, "EXAMPLE"),
("EXAMPLE 2011", 2011, "EXAMPLE"),
("1917", None, "1917"),
("EXAMPLE 2049", 2017, "EXAMPLE 2049"),
("EXAMPLE 12023", 2023, "EXAMPLE 12023"),
],
)
def test_strip_year_keeps_a_title_that_is_only_a_year_or_ends_in_another_number(
title: str, year: int | None, expected: str
) -> None:
assert strip_year(title, year) == expected
class TMDBResponse:
def __init__(self, results: list[dict]) -> None:
self.results = results
def raise_for_status(self) -> None:
pass
def json(self) -> dict:
return {"results": self.results}
def tmdb_search(monkeypatch: pytest.MonkeyPatch, results: list[dict], year: int | None) -> tuple[int | None, dict]:
sent: dict = {}
def get(url: str, params: dict, timeout: int) -> TMDBResponse:
sent.update(params)
return TMDBResponse(results)
p = TMDBProvider()
monkeypatch.setattr(config, "tmdb_api_key", "key")
monkeypatch.setattr(p, "_session", type("S", (), {"get": staticmethod(get)})())
monkeypatch.setattr(p, "fetch_detail", lambda tmdb_id, kind: None)
monkeypatch.setattr(p, "fetch_external_ids_raw", lambda tmdb_id, kind: None)
result = p.search("EXAMPLE", year, "tv")
return (result.external_ids.tmdb_id if result else None), sent
def test_tmdb_tv_search_matches_the_year_on_any_air_date(monkeypatch: pytest.MonkeyPatch) -> None:
_, sent = tmdb_search(monkeypatch, [{"id": 1, "name": "EXAMPLE", "first_air_date": "2016-04-04"}], 2020)
assert sent["year"] == 2020 and "first_air_date_year" not in sent
@pytest.mark.parametrize(("year", "expected"), [(2023, 2), (1999, 1), (2024, 1), (None, 1)])
def test_tmdb_prefers_the_series_that_first_aired_in_the_year_on_a_title_tie(
monkeypatch: pytest.MonkeyPatch, year: int | None, expected: int
) -> None:
results = [
{"id": 1, "name": "EXAMPLE", "first_air_date": "1999-10-20"},
{"id": 2, "name": "EXAMPLE", "first_air_date": "2023-08-31"},
{"id": 3, "name": "EXAMPLE OTHER", "first_air_date": "2024-01-01"},
]
assert tmdb_search(monkeypatch, results, year)[0] == expected