Skip to content

Commit

Permalink
Merge pull request #493 from twolaw/get-trakt-lists-faster
Browse files Browse the repository at this point in the history
Use LazyUserList to fetch Trakt Lists faster
  • Loading branch information
glensc authored Sep 29, 2021
2 parents 278bb27 + 8f02ab0 commit b2e4b60
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion plex_trakt_sync/trakt_list_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,45 @@
from trakt.movies import Movie
from trakt.tv import TVEpisode
from trakt.users import UserList
from trakt.core import get
from trakt.utils import slugify, extract_ids

from plex_trakt_sync.logging import logger
from plex_trakt_sync.plex_api import PlexApi


class LazyUserList(UserList):
@get
def get_items(self):
data = yield 'users/{user}/lists/{id}/items'.format(
user=slugify(self.creator), id=self.slug)
for item in data:
if 'type' not in item:
continue
item_type = item['type']
item_data = item.pop(item_type)
extract_ids(item_data)
self._items.append((item_type + 's', item_data['trakt']))
yield self._items

@classmethod
@get
def _get(cls, title, creator):
data = yield 'users/{user}/lists/{id}'.format(user=slugify(creator),
id=slugify(title))
extract_ids(data)
ulist = LazyUserList(creator=creator, **data)
ulist.get_items()
yield ulist


class TraktList():
def __init__(self, username, listname):
self.name = listname
self.plex_items = []
if username is not None:
self.trakt_items = dict(zip([(elem.media_type, elem.trakt) for elem in UserList._get(listname, username)._items if isinstance(elem, (Movie, TVEpisode))], count(1)))
prelist = [(elem[0], elem[1]) for elem in LazyUserList._get(listname, username)._items if elem[0] in ["movies", "episodes"]]
self.trakt_items = dict(zip(prelist, count(1)))

@staticmethod
def from_trakt_list(listname, trakt_list):
Expand Down

0 comments on commit b2e4b60

Please sign in to comment.