Add --dry-run option to sync
MODIFY
plex_trakt_sync/commands/sync.py
plex_trakt_sync/commands/sync.py
CONFIG = factory.config()
-def sync_all(walker: Walker, trakt: TraktApi, plex: PlexApi):
+def sync_all(walker: Walker, trakt: TraktApi, plex: PlexApi, dry_run: bool):
listutil = TraktListUtil()
with measure_time("Loaded Trakt lists"):
@@ -37,11 +37,13 @@ def sync_all(walker: Walker, trakt: TraktApi, plex: PlexApi):
click.echo(f"Server has {len(plex.library_sections)} libraries: {plex.library_section_names}")
runner = Sync(CONFIG)
- runner.sync(walker, listutil)
+ runner.sync(walker, listutil, dry_run=dry_run)
+ if not dry_run:
with measure_time("Updated plex watchlist"):
listutil.updatePlexLists(plex)
+ if not dry_run:
trakt.flush()
@@ -72,7 +74,14 @@ def sync_all(walker: Walker, trakt: TraktApi, plex: PlexApi):
default=1, show_default=True,
help="Batch size for collection submit queue"
)
-def sync(sync_option: str, library: str, show: str, movie: str, batch_size: int):
+@click.option(
+ "--dry-run", "dry_run",
+ type=bool,
+ default=False,
+ is_flag=True,
+ help="Dry run: Do not make changes"
+)
+def sync(sync_option: str, library: str, show: str, movie: str, batch_size: int, dry_run: bool):
"""
Perform sync between Plex and Trakt
"""
@@ -106,7 +115,9 @@ def sync(sync_option: str, library: str, show: str, movie: str, batch_size: int)
click.echo("Nothing to sync, this is likely due conflicting options given.")
return
+ if dry_run:
+ print("Enabled dry-run mode: not making actual changes")
w.walk_details(print=tqdm.write)
with measure_time("Completed full sync"):
- sync_all(walker=w, trakt=trakt, plex=plex)
+ sync_all(walker=w, trakt=trakt, plex=plex, dry_run=dry_run)

MODIFY
plex_trakt_sync/sync.py
plex_trakt_sync/sync.py
@@ -11,19 +11,19 @@ def __init__(self, config: Config):
self.sync_ratings_enabled = config["sync"]["ratings"]
self.sync_watched_status_enabled = config["sync"]["watched_status"]
- def sync(self, walker: Walker, listutil: TraktListUtil):
+ def sync(self, walker: Walker, listutil: TraktListUtil, dry_run=False):
for movie in walker.find_movies():
- self.sync_collection(movie)
- self.sync_ratings(movie)
- self.sync_watched(movie)
+ self.sync_collection(movie, dry_run=dry_run)
+ self.sync_ratings(movie, dry_run=dry_run)
+ self.sync_watched(movie, dry_run=dry_run)
listutil.addPlexItemToLists(movie)
for episode in walker.find_episodes():
- self.sync_collection(episode)
- self.sync_watched(episode)
+ self.sync_collection(episode, dry_run=dry_run)
+ self.sync_watched(episode, dry_run=dry_run)
listutil.addPlexItemToLists(episode)
- def sync_collection(self, m: Media):
+ def sync_collection(self, m: Media, dry_run=False):
if not self.sync_collection_enabled:
return
@@ -31,9 +31,10 @@ def sync_collection(self, m: Media):
return
logger.info(f"To be added to collection: {m}")
+ if not dry_run:
m.add_to_collection()
- def sync_ratings(self, m: Media):
+ def sync_ratings(self, m: Media, dry_run=False):
if not self.sync_ratings_enabled:
return
@@ -43,12 +44,14 @@ def sync_ratings(self, m: Media):
# Plex rating takes precedence over Trakt rating
if m.plex_rating is not None:
logger.info(f"Rating {m} with {m.plex_rating} on Trakt")
+ if not dry_run:
m.trakt_rate()
elif m.trakt_rating is not None:
logger.info(f"Rating {m} with {m.trakt_rating} on Plex")
+ if not dry_run:
m.plex_rate()
- def sync_watched(self, m: Media):
+ def sync_watched(self, m: Media, dry_run=False):
if not self.sync_watched_status_enabled:
return
@@ -57,7 +60,9 @@ def sync_watched(self, m: Media):
if m.watched_on_plex:
logger.info(f"Marking as watched in Trakt: {m}")
+ if not dry_run:
m.mark_watched_trakt()
elif m.watched_on_trakt:
logger.info(f"Marking as watched in Plex: {m}")
+ if not dry_run:
m.mark_watched_plex()

Add --dry-run option to sync