diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index d363cc9c0f..5633bec507 100644 --- a/plextraktsync/trakt_api.py +++ b/plextraktsync/trakt_api.py @@ -546,11 +546,28 @@ def from_guid(self, guid: PlexGuid): Find Trakt Episode from Guid of Plex Episode """ te = self.from_number(guid.pm.season_number, guid.pm.episode_number) - if not te or (str(te.ids.get(guid.provider)) != guid.id and not guid.pm.is_legacy_agent): + if self.invalid_match(guid, te): te = self.from_id(guid.provider, guid.id) return te + @staticmethod + def invalid_match(guid: PlexGuid, episode: Optional[TVEpisode]) -> bool: + """ + Checks if guid and episode don't match by comparing trakt provided id + """ + + if not episode: + # nothing to compare with + return True + if guid.pm.is_legacy_agent: + # check can not be performed + return False + id_from_trakt = getattr(episode, guid.provider, None) + if str(id_from_trakt) != guid.id: + return True + return False + def from_number(self, season, number): try: ep = self.table[season][number] diff --git a/tests/test_tv_lookup.py b/tests/test_tv_lookup.py index 02db27a99d..442d67b0ae 100755 --- a/tests/test_tv_lookup.py +++ b/tests/test_tv_lookup.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -m pytest from trakt.tv import TVShow -from plextraktsync.plex_api import PlexLibraryItem +from plextraktsync.plex_api import PlexGuid, PlexLibraryItem from plextraktsync.trakt_api import TraktLookup from tests.conftest import factory, make @@ -39,9 +39,64 @@ def test_show_episodes(): episodes = seasons[1].episodes assert episodes[0].title == "Winter Is Coming" + +def test_show_episodes_attack_on_titan(): + show = TVShow("Attack on Titan") + + assert len(show.seasons) > 0 + seasons = show.seasons + episodes = seasons[1].episodes + assert episodes[0].title == "To You, in 2000 Years: The Fall of Shiganshina (1)" + + pe = PlexLibraryItem( + make( + cls="Episode", + guid="imdb://tt2825724", + type="episode", + seasonNumber=1, + index=1, + ) + ) + + lookup = TraktLookup(show) + guid = pe.guids[0] + te = trakt.find_episode_guid(guid, lookup) + assert te.season == 1 + assert te.episode == 1 + assert te.imdb == "tt2825724" + + +def test_show_episodes_attack_on_titan_new_agent(): + show = TVShow("Attack on Titan") + + assert len(show.seasons) > 0 seasons = show.seasons - assert len(seasons) == 9 - assert seasons[1].episodes[0].title == "Winter Is Coming" + episodes = seasons[1].episodes + assert episodes[0].title == "To You, in 2000 Years: The Fall of Shiganshina (1)" + + guid = PlexGuid( + "imdb://tt2825724", + "episode", + ) + pe = PlexLibraryItem( + make( + cls="Episode", + guid="plex://foo", + guids=[ + guid, + ], + type="episode", + seasonNumber=1, + index=1, + ) + ) + + lookup = TraktLookup(show) + guid = pe.guids[0] + te = trakt.find_episode_guid(guid, lookup) + assert te.season == 1 + assert te.episode == 1 + assert te.imdb == "tt2825724" def test_tv_lookup_by_episode_id(): @@ -62,11 +117,7 @@ def test_tv_lookup_by_episode_id(): def test_find_episode(): - tm = make( - cls="TVShow", - # trakt=4965066, - trakt=176447, - ) + show = TVShow("Frank of Ireland") pe = PlexLibraryItem( make( @@ -79,7 +130,7 @@ def test_find_episode(): ) guid = pe.guids[0] - lookup = trakt.lookup(tm) + lookup = TraktLookup(show) te = trakt.find_episode_guid(guid, lookup) assert te.season == 1 assert te.episode == 1 @@ -88,3 +139,4 @@ def test_find_episode(): if __name__ == '__main__': test_show_episodes() + test_show_episodes_attack_on_titan()