From b211df60fbcdd4c06cbcebd79d9727f6214692a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 16 Apr 2021 14:20:30 +0300 Subject: [PATCH 1/3] Split internal logic of for_each_episode to iterate episodes --- plex_trakt_sync/commands/sync.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/plex_trakt_sync/commands/sync.py b/plex_trakt_sync/commands/sync.py index 80be2c9824..2978a7d9da 100644 --- a/plex_trakt_sync/commands/sync.py +++ b/plex_trakt_sync/commands/sync.py @@ -123,17 +123,22 @@ def for_each_pair(sections, trakt: TraktApi): def for_each_episode(sections, trakt: TraktApi): for pm, tm in for_each_pair(sections, trakt): - lookup = trakt.lookup(tm) + for tm, pe, te in for_each_show_episode(pm, tm, trakt): + yield tm, pe, te - # loop over episodes in plex db - for pe in pm.episodes(): - try: - te = lookup[pe.season_number][pe.episode_number] - except KeyError: - logger.warning(f"Skipping {pe}: Not found on Trakt") - continue - yield tm, pe, te.instance +def for_each_show_episode(pm, tm, trakt: TraktApi): + lookup = trakt.lookup(tm) + + # loop over episodes in plex db + for pe in pm.episodes(): + try: + te = lookup[pe.season_number][pe.episode_number] + except KeyError: + logger.warning(f"Skipping {pe}: Not found on Trakt") + continue + + yield tm, pe, te.instance def sync_all(movies=True, tv=True): From b24a8dedd63aadc385337fd5a10585dafb865647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 16 Apr 2021 14:22:32 +0300 Subject: [PATCH 2/3] Add --show option to filter by show name --- plex_trakt_sync/commands/sync.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/plex_trakt_sync/commands/sync.py b/plex_trakt_sync/commands/sync.py index 2978a7d9da..843460fe6c 100644 --- a/plex_trakt_sync/commands/sync.py +++ b/plex_trakt_sync/commands/sync.py @@ -187,25 +187,36 @@ def sync_all(movies=True, tv=True): @click.command() +@click.option( + "--show", "show", + type=str, + show_default=True, help="Sync specific show only" +) @click.option( "--sync", "sync_option", type=click.Choice(["all", "movies", "tv"], case_sensitive=False), default="all", show_default=True, help="Specify what to sync" ) -def sync(sync_option: str): +def sync(sync_option: str, show: str): """ Perform sync between Plex and Trakt """ + logger.info(f"Syncing with Plex {CONFIG['PLEX_USERNAME']} and Trakt {CONFIG['TRAKT_USERNAME']}") + movies = sync_option in ["all", "movies"] tv = sync_option in ["all", "tv"] - if not movies and not tv: + + if show: + movies = False + tv = True + logger.info(f"Syncing Show: {show}") + elif not movies and not tv: click.echo("Nothing to sync!") return - - logger.info(f"Syncing with Plex {CONFIG['PLEX_USERNAME']} and Trakt {CONFIG['TRAKT_USERNAME']}") - logger.info(f"Syncing TV={tv}, Movies={movies}") + else: + logger.info(f"Syncing TV={tv}, Movies={movies}") with measure_time("Completed full sync"): sync_all(movies=movies, tv=tv) From c640c52b2fde97d9f76317d71f1bcc57d608576d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 16 Apr 2021 14:22:55 +0300 Subject: [PATCH 3/3] Implement syncing only specific show --- plex_trakt_sync/commands/sync.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plex_trakt_sync/commands/sync.py b/plex_trakt_sync/commands/sync.py index 843460fe6c..cea8640b55 100644 --- a/plex_trakt_sync/commands/sync.py +++ b/plex_trakt_sync/commands/sync.py @@ -127,6 +127,14 @@ def for_each_episode(sections, trakt: TraktApi): yield tm, pe, te +def find_show_episodes(show, plex: PlexApi, trakt: TraktApi): + search = plex.search(show, libtype='show') + for pm in search: + tm = trakt.find_by_media(pm) + for tm, pe, te in for_each_show_episode(pm, tm, trakt): + yield tm, pe, te + + def for_each_show_episode(pm, tm, trakt: TraktApi): lookup = trakt.lookup(tm) @@ -141,7 +149,7 @@ def for_each_show_episode(pm, tm, trakt: TraktApi): yield tm, pe, te.instance -def sync_all(movies=True, tv=True): +def sync_all(movies=True, tv=True, show=None): with requests_cache.disabled(): server = get_plex_server() listutil = TraktListUtil() @@ -173,7 +181,12 @@ def sync_all(movies=True, tv=True): sync_watched(pm, tm, plex, trakt, trakt_watched_movies) if tv: - for tm, pe, te in for_each_episode(plex.show_sections, trakt): + if show: + it = find_show_episodes(show, plex, trakt) + else: + it = for_each_episode(plex.show_sections, trakt) + + for tm, pe, te in it: sync_show_collection(tm, pe, te, trakt) sync_show_watched(tm, pe, te, trakt_watched_shows, plex, trakt) @@ -219,4 +232,4 @@ def sync(sync_option: str, show: str): logger.info(f"Syncing TV={tv}, Movies={movies}") with measure_time("Completed full sync"): - sync_all(movies=movies, tv=tv) + sync_all(movies=movies, tv=tv, show=show)