Refactor trakt_api.lookup not to memoize
Instead memoize on Media object, which would be per instance, not global
MODIFY
plextraktsync/media.py
plextraktsync/media.py
@@ -142,7 +142,7 @@ def __init__(self, plex: PlexApi, trakt: TraktApi):
self.plex = plex
self.trakt = trakt
- def resolve_any(self, pm: PlexLibraryItem, tm=None):
+ def resolve_any(self, pm: PlexLibraryItem, show: Media = None):
try:
guids = pm.guids
except (PlexApiException, RequestException) as e:
@@ -150,13 +150,13 @@ def resolve_any(self, pm: PlexLibraryItem, tm=None):
return None
for guid in guids:
- m = self.resolve_guid(guid, tm)
+ m = self.resolve_guid(guid, show)
if m:
return m
return None
- def resolve_guid(self, guid: PlexGuid, tm=None):
+ def resolve_guid(self, guid: PlexGuid, show: Media = None):
if guid.provider in ["local", "none", "agents.none"]:
logger.warning(f"{guid.pm.item}: Skipping guid {guid} because provider {guid.provider} has no external Id")
@@ -169,8 +169,8 @@ def resolve_guid(self, guid: PlexGuid, tm=None):
return None
try:
- if tm:
- tm = self.trakt.find_episode_guid(tm, guid)
+ if show:
+ tm = self.trakt.find_episode_guid(guid, show.seasons)
else:
tm = self.trakt.find_by_guid(guid)
except (TraktException, RequestException) as e:

MODIFY
plextraktsync/trakt_api.py
plextraktsync/trakt_api.py
@@ -232,7 +232,6 @@ def add_to_collection(self, m, pm: PlexLibraryItem):
def collected(self, tm: TVShow):
return pytrakt_extensions.collected(tm.trakt)
- @memoize
@nocache
@rate_limit()
def lookup(self, tm: TVShow):
@@ -245,7 +244,9 @@ def lookup(self, tm: TVShow):
def find_by_guid(self, guid: PlexGuid):
if guid.type == "episode" and guid.is_episode:
ts = self.search_by_id(guid.show_id, id_type=guid.provider, media_type="show")
- return self.find_episode_guid(ts, guid)
+ lookup = self.lookup(ts)
+
+ return self.find_episode_guid(guid, lookup)
return self.search_by_id(guid.id, id_type=guid.provider, media_type=guid.type)
@@ -293,11 +294,10 @@ def valid_trakt_id(media_id: str):
# must be shorter than 12 numbers
return len(media_id) < 12
- def find_episode_guid(self, tm: TVShow, guid: PlexGuid, lookup=None):
+ def find_episode_guid(self, guid: PlexGuid, lookup):
"""
Find Trakt Episode from Guid of Plex Episode
"""
- lookup = lookup if lookup else self.lookup(tm)
try:
return lookup[guid.pm.season_number][guid.pm.episode_number].instance
except KeyError:

MODIFY
plextraktsync/walker.py
plextraktsync/walker.py
@@ -253,7 +253,7 @@ def get_plex_episodes(self, episodes):
show = self.mf.resolve_guid(guid)
if not show:
continue
- me = self.mf.resolve_any(PlexLibraryItem(pe), show.trakt)
+ me = self.mf.resolve_any(PlexLibraryItem(pe), show)
if not me:
continue
@@ -274,7 +274,7 @@ def media_from_items(self, libtype: str, items: List):
def episode_from_show(self, show: Media):
for pe in show.plex.episodes():
- me = self.mf.resolve_any(pe, show.trakt)
+ me = self.mf.resolve_any(pe, show)
if not me:
continue

MODIFY
tests/test_tv_lookup.py
tests/test_tv_lookup.py
@@ -46,7 +46,8 @@ def test_find_episode():
))
guid = pe.guids[0]
- te = trakt.find_episode_guid(tm, guid)
+ lookup = trakt.lookup(tm)
+ te = trakt.find_episode_guid(guid, lookup)
assert te.season == 1
assert te.episode == 1
assert te.imdb == "tt11909222"

Refactor trakt_api.lookup not to memoize

Instead memoize on Media object, which would be per instance, not global