diff --git a/plex_trakt_sync/commands/sync.py b/plex_trakt_sync/commands/sync.py index c4f491f333..87b9f81fb1 100644 --- a/plex_trakt_sync/commands/sync.py +++ b/plex_trakt_sync/commands/sync.py @@ -137,6 +137,7 @@ def find_show_episodes(show, plex: PlexApi, trakt: TraktApi): def for_each_show_episode(pm, tm, trakt: TraktApi): + lookup = trakt.lookup(tm) for pe in pm.episodes(): try: provider = pe.provider @@ -148,7 +149,7 @@ def for_each_show_episode(pm, tm, trakt: TraktApi): logger.error(f"Skipping {pe}: Provider {provider} not supported") continue - te = trakt.find_episode(tm, pe) + te = trakt.find_episode(tm, pe, lookup) if te is None: logger.warning(f"Skipping {pe}: Not found on Trakt") continue diff --git a/plex_trakt_sync/trakt_api.py b/plex_trakt_sync/trakt_api.py index 854beaedd9..939c2e3871 100644 --- a/plex_trakt_sync/trakt_api.py +++ b/plex_trakt_sync/trakt_api.py @@ -216,14 +216,18 @@ def search_by_id(self, media_id: str, id_type: str, media_type: str): return None - def find_episode(self, tm: TVShow, pe: PlexLibraryItem): + def find_episode(self, tm: TVShow, pe: PlexLibraryItem, lookup=None): """ Find Trakt Episode from Plex Episode """ - lookup = self.lookup(tm) + lookup = lookup if lookup else self.lookup(tm) try: return lookup[pe.season_number][pe.episode_number].instance except KeyError: + # Retry using search for specific Plex Episode + logger.warning("Retry using search for specific Plex Episode") + if not pe.is_episode: + return self.find_by_media(pe) return None def flush(self): diff --git a/tests/test_tv_lookup.py b/tests/test_tv_lookup.py index 77823cac9f..e2159963d6 100755 --- a/tests/test_tv_lookup.py +++ b/tests/test_tv_lookup.py @@ -1,10 +1,13 @@ #!/usr/bin/env python3 -m pytest +from typing import Union + +from trakt.tv import TVShow from plex_trakt_sync.plex_api import PlexLibraryItem from plex_trakt_sync.trakt_api import TraktApi -def make(cls=None, **kwargs): +def make(cls=None, **kwargs) -> Union[TVShow]: cls = cls if cls is not None else "object" # https://stackoverflow.com/a/2827726/2314626 return type(cls, (object,), kwargs) @@ -34,3 +37,24 @@ def test_tv_lookup_by_episode_id(): te = trakt.find_by_media(pe) assert te.imdb == "tt0505457" assert te.tmdb == 511997 + + +def test_find_episode(): + tm = make( + cls='TVShow', + # trakt=4965066, + trakt=176447, + ) + + pe = PlexLibraryItem(make( + cls='Episode', + guid='imdb://tt11909222', + type='episode', + seasonNumber=1, + index=1, + )) + + te = trakt.find_episode(tm, pe) + assert te.season == 1 + assert te.episode == 1 + assert te.imdb == "tt11909222"