Skip to content

Commit

Permalink
Merge pull request #252 from glensc/episode-lookup-fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Apr 18, 2021
2 parents c182c84 + 8c3fb74 commit 5647f99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion plex_trakt_sync/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions plex_trakt_sync/trakt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 25 additions & 1 deletion tests/test_tv_lookup.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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"

0 comments on commit 5647f99

Please sign in to comment.