diff --git a/plexapi/playlist.py b/plexapi/playlist.py index c42517b16..ccd123033 100644 --- a/plexapi/playlist.py +++ b/plexapi/playlist.py @@ -184,17 +184,22 @@ def item(self, title): @cached_data_property def _items(self): """ Cache for items. """ + return self._fetchItems() + + def _fetchItems(self, libtype=None): + """ Returns a list of all items in the playlist, optionally filtered by library type. """ if self.radio: return [] - key = self._buildQueryKey(f'{self.key}/items') + params = {'type': utils.searchType(libtype)} if libtype else {} + key = self._buildQueryKey(f'{self.key}/items', **params) items = self.fetchItems(key) # Cache server connections to avoid reconnecting for each item _servers = {} for item in items: - if item.sourceURI: - serverID = item.sourceURI.split('/')[2] + if sourceURI := getattr(item, 'sourceURI', None): + serverID = sourceURI.split('/')[2] if serverID not in _servers: try: _servers[serverID] = self._server.myPlexAccount().resource(serverID).connect() @@ -205,8 +210,16 @@ def _items(self): return items - def items(self): - """ Returns a list of all items in the playlist. """ + def items(self, libtype=None): + """ Returns a list of all items in the playlist. + + Parameters: + libtype (str): Optional filter to return items grouped by a specific library type + (movie, show, season, episode, artist, album, track, photoalbum, photo). + (e.g. shows in a playlist, or albums in a playlist) + """ + if libtype: + return self._fetchItems(libtype=libtype) return self._items def get(self, title): diff --git a/tests/test_playlist.py b/tests/test_playlist.py index 7c2b1be5a..34cb83706 100644 --- a/tests/test_playlist.py +++ b/tests/test_playlist.py @@ -101,7 +101,7 @@ def test_Playlist_edit(plex, movie): playlist.delete() -def test_Playlist_item(plex, show): +def test_Playlist_items(plex, show): title = 'test_playlist_item' episodes = show.episodes() try: @@ -117,6 +117,24 @@ def test_Playlist_item(plex, show): playlist.delete() +def test_Playlist_items_libtype(plex, show): + title = 'test_playlist_group_items' + episodes = show.episodes() + try: + playlist = plex.createPlaylist(title, items=episodes[:3]) + playlist_shows = playlist.items(libtype='show') + assert len(playlist_shows) == 1 + assert playlist_shows[0] == show + playlist_seasons = playlist.items(libtype='season') + assert len(playlist_seasons) == 1 + assert playlist_seasons[0] == show.season(1) + playlist_episodes = playlist.items(libtype='episode') + assert len(playlist_episodes) == 3 + assert playlist_episodes == episodes[:3] + finally: + playlist.delete() + + @pytest.mark.client def test_Playlist_play(plex, client, artist, album): try: