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

Use LazyUserList to fetch Trakt Lists faster #493

Merged
merged 1 commit into from
Sep 29, 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
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get override should not be needed after glensc/python-pytrakt#3

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