Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --show option to sync, to sync only specific show #247

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions plex_trakt_sync/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,33 @@ 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 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 sync_all(movies=True, tv=True):
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, show=None):
with requests_cache.disabled():
server = get_plex_server()
listutil = TraktListUtil()
Expand Down Expand Up @@ -168,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)

Expand All @@ -182,25 +200,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)
sync_all(movies=movies, tv=tv, show=show)