Sync watched using our Plex/Trakt api
MODIFY
plex_trakt_sync/main.py
plex_trakt_sync/main.py
@@ -37,6 +37,23 @@ def sync_ratings(pm, tm, plex: PlexApi, trakt: TraktApi):
plex.rate(pm.item, trakt_rating)
+def sync_watched(pm, tm, plex: PlexApi, trakt: TraktApi, trakt_watched_movies):
+ watched_on_plex = pm.item.isWatched
+ watched_on_trakt = tm.trakt in trakt_watched_movies
+ if watched_on_plex is watched_on_trakt:
+ return
+
+ # if watch status is not synced
+ # send watched status from plex to trakt
+ if watched_on_plex:
+ logging.info(f"Marking as watched on Trakt: {pm}")
+ trakt.mark_watched(tm, pm.seen_date)
+ # set watched status if movie is watched on Trakt
+ elif watched_on_trakt:
+ logging.info(f"Marking as watched in Plex: {pm}")
+ plex.mark_watched(pm.item)
+
+
def process_movie_section(section: PlexLibrarySection, watched_set, listutil, collection, trakt_api: TraktApi, plex_api: PlexApi):
# args: a section of plex movies, a set comprised of the trakt ids of all watched movies and a dict with key=slug and value=rating (1-10)
@@ -86,40 +103,8 @@ def process_movie_section(section: PlexLibrarySection, watched_set, listutil, co
# sync watch status
if CONFIG['sync']['watched_status']:
- watchedOnPlex = movie.isWatched
- watchedOnTrakt = m.trakt in watched_set
- if watchedOnPlex is not watchedOnTrakt:
- # if watch status is not synced
- # send watched status from plex to trakt
- if watchedOnPlex:
- retry = 0
- while retry < 5:
- try:
- last_time = respect_trakt_rate(last_time)
- with requests_cache.disabled():
- seen_date = (movie.lastViewedAt if movie.lastViewedAt else datetime.now())
- m.mark_as_seen(seen_date.astimezone(datetime.timezone.utc))
- logging.info("Movie [{} ({})]: marking as watched on Trakt...".format(
- movie.title, movie.year))
- break
- except ValueError: # for py<3.6
- with requests_cache.disabled():
- m.mark_as_seen(seen_date)
- except trakt.errors.RateLimitException as e:
- delay = int(e.response.headers.get("Retry-After", 1))
- logging.warning(
- "Movie [{} ({})]: Rate Limited on watched update. Sleeping {} sec from trakt (GUID: {})".format(movie.title, movie.year, delay, guid))
- sleep(delay)
- retry += retry
- if retry == 5:
- logging.warning(
- "Movie [{} ({})]: Rate Limited 5 times on watched update. Abort trakt request.".format(movie.title, movie.year))
- # set watched status if movie is watched on trakt
- elif watchedOnTrakt:
- logging.info("Movie [{} ({})]: marking as watched in Plex...".format(
- movie.title, movie.year))
- with requests_cache.disabled():
- movie.markWatched()
+ sync_watched(it, m, plex_api, trakt_api, watched_set)
+
# add to plex lists
listutil.addPlexItemToLists(m.trakt, movie)

Sync watched using our Plex/Trakt api