forked from Taxel/PlexTraktSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trakt_list_util.py
58 lines (49 loc) · 2.09 KB
/
trakt_list_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from trakt.users import UserList
from trakt.errors import NotFoundException
from trakt.movies import Movie
import requests_cache
import logging
from plexapi.exceptions import BadRequest, NotFound
class TraktList():
def __init__(self, username, listname):
self.name = listname
self.plex_movies = []
if username != None:
self.slugs = set(map(lambda m: m.slug, [elem for elem in UserList._get(listname, username).get_items() if type(elem) == Movie]))
@staticmethod
def from_set(listname, list_set):
l = TraktList(None, listname)
l.slugs = list_set
return l
def addPlexMovie(self, slug, plex_movie):
if slug in self.slugs:
self.plex_movies.append(plex_movie)
logging.info('Movie [{} ({})]: added to list {}'.format(plex_movie.title, plex_movie.year, self.name))
def updatePlexList(self, plex):
with requests_cache.disabled():
try:
plex.playlist(self.name).delete()
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.createPlaylist(self.name, items=self.plex_movies)
class TraktListUtil():
def __init__(self):
self.lists = []
def addList(self, username, listname, list_set = None):
if list_set is not None:
self.lists.append(TraktList.from_set(listname, list_set))
logging.info("Downloaded List {}".format(listname))
return
try:
self.lists.append(TraktList(username, listname))
logging.info("Downloaded List {}".format(listname))
except NotFoundException:
logging.warning("Failed to get list {} by user {}".format(listname, username))
def addPlexMovieToLists(self, slug, plex_movie):
for l in self.lists:
l.addPlexMovie(slug, plex_movie)
def updatePlexLists(self, plex):
for l in self.lists:
l.updatePlexList(plex)