Add tv episodes on playlist sync trakt->plex
before that, only movies were synced
MODIFY
main.py
main.py
def process_movie_section(s, watched_set, ratings_dict, listutil, collection):
- # args: a section of plex movies, a set comprised of the slugs of all watched movies and a dict with key=slug and value=rating (1-10)
+ # 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
@@ -75,7 +75,7 @@ def process_movie_section(s, watched_set, ratings_dict, listutil, collection):
if CONFIG['sync']['collection']:
# add to collection if necessary
- if m.slug not in collection:
+ if m.trakt not in collection:
logging.info('Movie [{} ({})]: Added to trakt collection'.format(
movie.title, movie.year))
m.add_to_library()
@@ -124,7 +124,7 @@ def process_movie_section(s, watched_set, ratings_dict, listutil, collection):
with requests_cache.disabled():
movie.markWatched()
# add to plex lists
- listutil.addPlexMovieToLists(m.slug, movie)
+ listutil.addPlexItemToLists(m.trakt, movie)
logging.info("Movie [{} ({})]: Finished sync".format(
movie.title, movie.year))
@@ -136,7 +136,7 @@ def process_movie_section(s, watched_set, ratings_dict, listutil, collection):
"Movie [{} ({})]: bad response from trakt (GUID: {})".format(movie.title, movie.year, guid))
-def process_show_section(s, watched_set):
+def process_show_section(s, watched_set, listutil):
with requests_cache.disabled():
allShows = s.all()
logging.info("Now working on show section {} containing {} elements".format(s.title, len(allShows)))
@@ -241,6 +241,8 @@ def process_show_section(s, watched_set):
episode.isWatched, watched, episode.isWatched != watched))
logging.debug("Show [{} ({})]: Synced episode S{:02}E{:02}".format(
show.title, show.year, episode.seasonNumber, episode.index))
+ # add to plex lists
+ listutil.addPlexItemToLists(eps.instance.trakt, episode)
logging.info("Show [{} ({})]: Finished sync".format(
show.title, show.year))
except trakt.errors.NotFoundException:
@@ -284,15 +286,14 @@ def main():
logging.debug("Watched movies from trakt: {}".format(
trakt_watched_movies))
trakt_movie_collection = set(
- map(lambda m: m.slug, trakt_user.movie_collection))
+ map(lambda m: m.trakt, trakt_user.movie_collection))
# logging.debug("Movie collection from trakt:", trakt_movie_collection)
trakt_watched_shows = pytrakt_extensions.allwatched()
if CONFIG['sync']['watchlist']:
- listutil.addList(None, "Trakt Watchlist", slug_list=list(
- map(lambda m: m.slug, trakt_user.watchlist_movies)))
+ listutil.addList(None, "Trakt Watchlist", traktid_list=list(
+ map(lambda m: m.trakt, trakt_user.watchlist_movies)))
# logging.debug("Movie watchlist from trakt:", trakt_movie_watchlist)
user_ratings = trakt_user.get_ratings(media_type='movies')
-
if CONFIG['sync']['liked_lists']:
for lst in liked_lists:
listutil.addList(lst['username'], lst['listname'])
@@ -334,7 +335,7 @@ def main():
# process show sections
elif type(section) is plexapi.library.ShowSection:
print("Processing section", section.title)
- process_show_section(section, trakt_watched_shows)
+ process_show_section(section, trakt_watched_shows, listutil)
else:
continue

MODIFY
trakt_list_util.py
trakt_list_util.py
from trakt.users import UserList
from trakt.errors import NotFoundException
from trakt.movies import Movie
+from trakt.tv import TVEpisode
+from plexapi.video import Episode
import requests_cache
import logging
from plexapi.exceptions import BadRequest, NotFound
from itertools import count
+
class TraktList():
def __init__(self, username, listname):
self.name = listname
- self.plex_movies = []
- if username != None:
- self.slugs = dict(zip(map(lambda m: m.slug, [elem for elem in UserList._get(listname, username).get_items() if type(elem) == Movie]),count()))
+ self.plex_items = []
+ if username is not None:
+ self.traktids = dict(zip(map(lambda e: e.trakt, [elem for elem in UserList._get(listname, username).get_items() if isinstance(elem, (Movie, TVEpisode))]), count(1)))
@staticmethod
- def from_slug_list(listname, slug_list):
+ def from_traktid_list(listname, traktid_list):
l = TraktList(None, listname)
- l.slugs = dict(zip(slug_list, count()))
+ l.traktids = dict(zip(traktid_list, count(1)))
return l
- def addPlexMovie(self, slug, plex_movie):
- rank = self.slugs.get(slug)
+ def addPlexItem(self, traktid, plex_item):
+ rank = self.traktids.get(traktid)
if rank is not None:
- self.plex_movies.append((rank, plex_movie))
- logging.info('Movie [{} ({})]: added to list {}'.format(plex_movie.title, plex_movie.year, self.name))
+ self.plex_items.append((rank, plex_item))
+ if isinstance(plex_item, Episode):
+ logging.info('Show [{} ({})]: {} added to list {}'.format(plex_item.show().title, plex_item.show().year, plex_item.seasonEpisode, self.name))
+ else:
+ logging.info('Movie [{} ({})]: added to list {}'.format(plex_item.title, plex_item.year, self.name))
def updatePlexList(self, plex):
with requests_cache.disabled():
@@ -32,18 +38,18 @@ def updatePlexList(self, plex):
except (NotFound, BadRequest):
logging.error("Playlist %s not found, so it could not be deleted. Actual playlists: %s" % (self.name, plex.playlists()))
pass
- if len(self.plex_movies) > 0:
- _, plex_movies_sorted = zip(*sorted(self.plex_movies))
- plex.createPlaylist(self.name, items=plex_movies_sorted)
+ if len(self.plex_items) > 0:
+ _, plex_items_sorted = zip(*sorted(self.plex_items))
+ plex.createPlaylist(self.name, items=plex_items_sorted)
class TraktListUtil():
def __init__(self):
self.lists = []
- def addList(self, username, listname, slug_list = None):
- if slug_list is not None:
- self.lists.append(TraktList.from_slug_list(listname, slug_list))
+ def addList(self, username, listname, traktid_list=None):
+ if traktid_list is not None:
+ self.lists.append(TraktList.from_traktid_list(listname, traktid_list))
logging.info("Downloaded List {}".format(listname))
return
try:
@@ -52,9 +58,9 @@ def addList(self, username, listname, slug_list = None):
except NotFoundException:
logging.warning("Failed to get list {} by user {}".format(listname, username))
- def addPlexMovieToLists(self, slug, plex_movie):
+ def addPlexItemToLists(self, traktid, plex_item):
for l in self.lists:
- l.addPlexMovie(slug, plex_movie)
+ l.addPlexItem(traktid, plex_item)
def updatePlexLists(self, plex):
for l in self.lists:

Add tv episodes on playlist sync trakt->plex

before that, only movies were synced