Skip to content

Commit

Permalink
Refactor to use Collection for trakt lists
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Feb 24, 2024
1 parent a0050c7 commit 5826766
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
21 changes: 12 additions & 9 deletions plextraktsync/plex/PlexPlaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from functools import cached_property
from typing import TYPE_CHECKING

from plexapi.library import LibrarySection
from plexapi.myplex import Section

from plextraktsync.decorators.flatten import flatten_dict
from plextraktsync.factory import logging
from plextraktsync.media.Media import Media
Expand All @@ -18,8 +21,8 @@
class PlexPlaylist(RichMarkup):
logger = logging.getLogger(__name__)

def __init__(self, server: PlexServer, name: str):
self.server = server
def __init__(self, section: LibrarySection, name: str):
self.section = section
self.name = name

def __iter__(self):
Expand All @@ -32,13 +35,13 @@ def __contains__(self, m: Media):
return m.plex_key in self.items

@cached_property
def playlist(self) -> Playlist | None:
def playlist(self):
try:
playlists = self.server.playlists(title=self.name, title__iexact=self.name)
playlist: Playlist = playlists[0]
playlists = self.section.collections(title=self.name, title__iexact=self.name)
playlist = playlists[0]
if len(playlists) > 1:
self.logger.warning(f"Found multiple playlists ({len(playlists)}) with same name: '{self.name}', "
f"Using first playlist with id {playlist.ratingKey}")
self.logger.warning(f"Found multiple collections ({len(playlists)}) with same name: '{self.name}', "
f"Using first collection with id {playlist.ratingKey}")
self.logger.debug(f"Loaded plex list: '{self.name}'")
return playlist
except IndexError:
Expand All @@ -53,7 +56,7 @@ def items(self) -> dict[int, PlexMedia]:
for m in self.playlist.items():
yield m.ratingKey, m

def update(self, items: list[PlexMedia], description=None) -> bool:
def update(self, section, items: list[PlexMedia], description=None) -> bool:
"""
Updates playlist (creates if name missing) replacing contents with items[]
"""
Expand All @@ -62,7 +65,7 @@ def update(self, items: list[PlexMedia], description=None) -> bool:
# Force reload
del self.__dict__["playlist"]
del self.__dict__["items"]
playlist = self.server.createPlaylist(self.name, items=items)
playlist = self.section.createCollection(self.name, section=section, items=items)
self.logger.info(f"Created plex playlist {self.title_link} with {len(items)} items", extra={"markup": True})

# Skip if playlist could not be made/retrieved
Expand Down
8 changes: 4 additions & 4 deletions plextraktsync/plex/PlexPlaylistCollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from plextraktsync.plex.PlexPlaylist import PlexPlaylist

if TYPE_CHECKING:
from plexapi.server import PlexServer
from plexapi.library import LibrarySection


class PlexPlaylistCollection(UserDict):
def __init__(self, server: PlexServer):
def __init__(self, section: LibrarySection):
super().__init__()
self.server = server
self.section = section

def __missing__(self, name: str):
self[name] = playlist = PlexPlaylist(self.server, name)
self[name] = playlist = PlexPlaylist(self.section, name)

return playlist
5 changes: 4 additions & 1 deletion plextraktsync/trakt/TraktUserList.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ def from_watchlist(cls, items: list[TraktPlayable]):

@cached_property
def plex_lists(self):
return factory.plex_lists
from plextraktsync.plex.PlexPlaylistCollection import \
PlexPlaylistCollection

return PlexPlaylistCollection(self.section)

@cached_property
def plex_list(self):
Expand Down

0 comments on commit 5826766

Please sign in to comment.