Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 18 additions & 5 deletions plexapi/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down
20 changes: 19 additions & 1 deletion tests/test_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Comment on lines +134 to +135
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

playlist.delete() in the finally block can raise UnboundLocalError if plex.createPlaylist(...) fails before playlist is assigned. Initialize playlist = None before the try and guard the delete (e.g., if playlist: playlist.delete()).

Copilot uses AI. Check for mistakes.


@pytest.mark.client
def test_Playlist_play(plex, client, artist, album):
try:
Expand Down
Loading