Cleanup unused process_movie_section from main.py
MODIFY
plex_trakt_sync/commands/sync.py
plex_trakt_sync/commands/sync.py
from plex_trakt_sync.plex_server import get_plex_server
from plex_trakt_sync.config import CONFIG
from plex_trakt_sync.decorators import measure_time
-from plex_trakt_sync.main import process_movie_section, process_show_section
+from plex_trakt_sync.main import process_show_section
from plex_trakt_sync.plex_api import PlexApi
from plex_trakt_sync.trakt_api import TraktApi
from plex_trakt_sync.trakt_list_util import TraktListUtil

MODIFY
plex_trakt_sync/main.py
plex_trakt_sync/main.py
trakt_post_wait = 1.2 # delay in sec between trakt post requests to respect rate limit
-def sync_ratings(pm, tm, plex: PlexApi, trakt: TraktApi):
- trakt_rating = trakt.rating(tm)
- plex_rating = pm.rating
- if plex_rating is trakt_rating:
- return
-
- # Plex rating takes precedence over Trakt rating
- if plex_rating is not None:
- logging.info(f"Rating {pm} with {plex_rating} on Trakt")
- trakt.rate(tm, plex_rating)
- elif trakt_rating is not None:
- logging.info(f"Rating {pm} with {trakt_rating} on Plex")
- 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 sync_collection(pm, tm, trakt: TraktApi, trakt_movie_collection):
- if tm.trakt in trakt_movie_collection:
- return
-
- logging.info(f"Add to Trakt Collection: {pm}")
- trakt.add_to_collection(tm)
-
-
-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)
-
- ###############
- # Sync movies with trakt
- ###############
-
- logging.info("Now working on movie section {} containing {} elements".format(section.title, len(section)))
- for it in section.items():
- movie = it.item
- if not it.provider:
- logging.error('Movie [{} ({})]: Unrecognized GUID {}'.format(movie.title, movie.year, movie.guid))
- continue
-
- # search and sync movie
- m = trakt_api.find_movie(it)
- if m is None:
- logging.warning(f"Movie [{movie.title} ({movie.year})]: Not found. Skipping")
- continue
-
- guid = it.guid
- try:
- # add to collection if necessary
- if CONFIG['sync']['collection']:
- sync_collection(it, m, trakt_api, collection)
-
- # compare ratings
- if CONFIG['sync']['ratings']:
- sync_ratings(it, m, plex_api, trakt_api)
-
- # sync watch status
- if CONFIG['sync']['watched_status']:
- sync_watched(it, m, plex_api, trakt_api, watched_set)
-
- # add to plex lists
- listutil.addPlexItemToLists(m.trakt, movie)
-
- logging.debug("Movie [{} ({})]: Finished sync".format(
- movie.title, movie.year))
- except trakt.errors.NotFoundException:
- logging.error(
- "Movie [{} ({})]: GUID {} not found on trakt".format(movie.title, movie.year, it.guid))
- except trakt.errors.RateLimitException as e:
- delay = int(e.response.headers.get("Retry-After", 1))
- logging.warning(
- "Movie [{} ({})]: Rate Limited. Sleeping {} sec from trakt (GUID: {})".format(movie.title, movie.year, delay, guid))
- sleep(delay)
- except Exception as e:
- logging.error(
- "Movie [{} ({})]: {} (GUID: {})".format(movie.title, movie.year, e, it.guid))
-
-
def process_show_section(s, watched_set, listutil):
with requests_cache.disabled():
allShows = s.all()

Cleanup unused process_movie_section from main.py