From f6e01b05a0136f0c2d6a920d48af7374c7a7e5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 24 Oct 2022 18:32:11 +0300 Subject: [PATCH 1/5] tests/test_tv_lookup: Add lookup with new agent --- tests/test_tv_lookup.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/test_tv_lookup.py b/tests/test_tv_lookup.py index 02db27a99d..ccf90dc3d6 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 @@ -44,6 +44,39 @@ def test_show_episodes(): assert seasons[1].episodes[0].title == "Winter Is Coming" +def test_show_episodes_attack_on_titan_new_agent(): + 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)" + + 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(): pe = PlexLibraryItem( make( From a9d2c8e8fd70488dde6959e3e41cbfd9a0af6c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 24 Oct 2022 16:57:35 +0300 Subject: [PATCH 2/5] tests/test_tv_lookup: Fix test_find_episode test --- tests/test_tv_lookup.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/test_tv_lookup.py b/tests/test_tv_lookup.py index ccf90dc3d6..89dae86e0b 100755 --- a/tests/test_tv_lookup.py +++ b/tests/test_tv_lookup.py @@ -95,11 +95,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( @@ -112,7 +108,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 From c6dc41380f67f1e6afa34302411f1120d712c4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 24 Oct 2022 16:41:14 +0300 Subject: [PATCH 3/5] tests/test_tv_lookup: Add test_show_episodes_attack_on_titan test --- tests/test_tv_lookup.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/test_tv_lookup.py b/tests/test_tv_lookup.py index 89dae86e0b..442d67b0ae 100755 --- a/tests/test_tv_lookup.py +++ b/tests/test_tv_lookup.py @@ -39,9 +39,31 @@ 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 - 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)" + + 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(): @@ -117,3 +139,4 @@ def test_find_episode(): if __name__ == '__main__': test_show_episodes() + test_show_episodes_attack_on_titan() From cc484e3f5cbc594a1f47c19091c46c56bccc0ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 24 Oct 2022 18:30:48 +0300 Subject: [PATCH 4/5] TraktLookup: Extract invalid match logic --- plextraktsync/trakt_api.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index d363cc9c0f..990b36402e 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 = episode.ids.get(guid.provider) + if str(id_from_trakt) != guid.id: + return True + return False + def from_number(self, season, number): try: ep = self.table[season][number] From 3c7373dfef2a018f76102f2896dfabf62fda4192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 24 Oct 2022 22:23:04 +0300 Subject: [PATCH 5/5] Fix id_from_trakt fetching --- plextraktsync/trakt_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plextraktsync/trakt_api.py b/plextraktsync/trakt_api.py index 990b36402e..5633bec507 100644 --- a/plextraktsync/trakt_api.py +++ b/plextraktsync/trakt_api.py @@ -563,7 +563,7 @@ def invalid_match(guid: PlexGuid, episode: Optional[TVEpisode]) -> bool: if guid.pm.is_legacy_agent: # check can not be performed return False - id_from_trakt = episode.ids.get(guid.provider) + id_from_trakt = getattr(episode, guid.provider, None) if str(id_from_trakt) != guid.id: return True return False