Skip to content

Commit

Permalink
Move TV shows sync logic to sync command
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Apr 7, 2021
1 parent a9dffe7 commit 1caccbb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
61 changes: 57 additions & 4 deletions plex_trakt_sync/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from plex_trakt_sync.config import CONFIG
from plex_trakt_sync.decorators import measure_time
from plex_trakt_sync.main import process_show_section
from plex_trakt_sync.plex_api import PlexApi
from plex_trakt_sync.plex_api import PlexApi, PlexLibraryItem
from plex_trakt_sync.trakt_api import TraktApi
from plex_trakt_sync.trakt_list_util import TraktListUtil
from plex_trakt_sync.logging import logger
Expand All @@ -22,6 +22,19 @@ def sync_collection(pm, tm, trakt: TraktApi, trakt_movie_collection):
trakt.add_to_collection(tm)


def sync_show_collection(pm, tm, pe, te, trakt: TraktApi):
if not CONFIG['sync']['collection']:
return

collected = trakt.collected(tm)
is_collected = collected.get_completed(pe.seasonNumber, pe.index)
if is_collected:
return

logger.info(f"Add to Trakt Collection: {pm} S{pe.seasonNumber:02}E{pe.index:02}")
trakt.add_to_collection(te.instance)


def sync_ratings(pm, tm, plex: PlexApi, trakt: TraktApi):
if not CONFIG['sync']['ratings']:
return
Expand Down Expand Up @@ -60,6 +73,25 @@ def sync_watched(pm, tm, plex: PlexApi, trakt: TraktApi, trakt_watched_movies):
plex.mark_watched(pm.item)


def sync_show_watched(pm, tm, pe, te, trakt_watched_shows, plex: PlexApi, trakt: TraktApi):
if not CONFIG['sync']['watched_status']:
return

watched_on_plex = pe.isWatched
watched_on_trakt = trakt_watched_shows.get_completed(tm.trakt, pe.seasonNumber, pe.index)

if watched_on_plex == watched_on_trakt:
return

if watched_on_plex:
logger.info(f"Marking as watched in Trakt: {pm} S{pe.seasonNumber:02}E{pe.index:02}")
m = PlexLibraryItem(pe)
trakt.mark_watched(te.instance, m.seen_date)
elif watched_on_trakt:
logger.info(f"Marking as watched in Plex: {pm} S{pe.seasonNumber:02}E{pe.index:02}")
plex.mark_watched(pe)


def for_each_pair(sections, trakt: TraktApi):
for section in sections:
with measure_time(f"Processing section {section.title}"):
Expand All @@ -76,6 +108,24 @@ def for_each_pair(sections, trakt: TraktApi):
yield pm, tm


def for_each_episode(sections, trakt: TraktApi):
for pm, tm in for_each_pair(sections, trakt):
lookup = trakt.lookup(tm)

# loop over episodes in plex db
for pe in pm.item.episodes():
try:
te = lookup[pe.seasonNumber][pe.index]
except KeyError:
try:
logger.warning(f"Show [{pm}: Key not found: S{pe.seasonNumber:02}E{pe.seasonNumber:02}")
except TypeError:
logger.error(f"Show [{pm}]: Invalid episode: {pe}")
continue

yield pm, tm, pe, te


def sync_all(movies=True, tv=True):
with requests_cache.disabled():
server = get_plex_server()
Expand Down Expand Up @@ -108,9 +158,12 @@ def sync_all(movies=True, tv=True):
sync_watched(pm, tm, plex, trakt, trakt_watched_movies)

if tv:
for section in plex.show_sections:
with measure_time("Processing section %s" % section.title):
process_show_section(section, trakt_watched_shows, listutil)
for pm, tm, pe, te in for_each_episode(plex.show_sections, trakt):
sync_show_collection(pm, tm, pe, te, trakt)
sync_show_watched(pm, tm, pe, te, trakt_watched_shows, plex, trakt)

# add to plex lists
listutil.addPlexItemToLists(te.instance.trakt, pe)

with measure_time("Updated plex watchlist"):
listutil.updatePlexLists(server)
Expand Down
2 changes: 1 addition & 1 deletion plex_trakt_sync/plex_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def show_sections(self):
for section in self.library_sections:
if not type(section) is ShowSection:
continue
result.append(section)
result.append(PlexLibrarySection(section))

return result

Expand Down

0 comments on commit 1caccbb

Please sign in to comment.