Use batch to submit add collection calls
MODIFY
plex_trakt_sync/commands/sync.py
plex_trakt_sync/commands/sync.py
@@ -178,6 +178,8 @@ def sync_all(movies=True, tv=True):
with measure_time("Updated plex watchlist"):
listutil.updatePlexLists(server)
+ trakt.flush()
+
@click.command()
@click.option(

MODIFY
plex_trakt_sync/trakt_api.py
plex_trakt_sync/trakt_api.py
@@ -51,6 +51,9 @@ class TraktApi:
Trakt API class abstracting common data access and dealing with requests cache.
"""
+ def __init__(self):
+ self.batch = TraktBatch(self)
+
@property
@memoize
@nocache
@@ -163,29 +166,23 @@ def scrobbler(self, media: Union[Movie, TVEpisode]) -> ScrobblerProxy:
def mark_watched(self, m, time):
m.mark_as_seen(time)
- @nocache
- @rate_limit(delay=TRAKT_POST_DELAY)
def add_to_collection(self, m, pm: PlexLibraryItem):
if m.media_type == "movies":
- json = {
- m.media_type: [dict(
+ item = dict(
title=m.title,
year=m.year,
**m.ids,
**pm.to_json(),
- )],
- }
+ )
elif m.media_type == "episodes":
- json = {
- m.media_type: [dict(
+ item = dict(
**m.ids,
- **pm.to_json(),
- )],
- }
+ **pm.to_json()
+ )
else:
raise ValueError(f"Unsupported media type: {m.media_type}")
- return trakt.sync.add_to_collection(json)
+ self.batch.add_to_collection(m.media_type, item)
@memoize
@nocache
@@ -219,6 +216,12 @@ def find_movie(self, media: PlexLibraryItem):
return None
+ def flush(self):
+ """
+ Submit all pending data
+ """
+ self.batch.submit_collection()
+
class TraktBatch:
def __init__(self, trakt: TraktApi):

Use batch to submit add collection calls