Skip to content

Commit

Permalink
Merge pull request #617 from JonnyWong16/feature/library_collections_…
Browse files Browse the repository at this point in the history
…playlists

Change LibrarySection collections method to plural and add playlists method
  • Loading branch information
Hellowlol authored Dec 5, 2020
2 parents 0b66396 + 422f82e commit 16a63f1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
35 changes: 23 additions & 12 deletions plexapi/library.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import warnings
from urllib.parse import quote, quote_plus, unquote, urlencode

from plexapi import X_PLEX_CONTAINER_SIZE, log, utils
Expand All @@ -7,6 +8,8 @@
from plexapi.media import MediaTag
from plexapi.settings import Setting

warnings.simplefilter('default')


class Library(PlexObject):
""" Represents a PlexServer library. This contains all sections of media defined
Expand Down Expand Up @@ -842,6 +845,23 @@ def history(self, maxresults=9999999, mindate=None):
"""
return self._server.history(maxresults=maxresults, mindate=mindate, librarySectionID=self.key, accountID=1)

def collection(self, **kwargs):
msg = 'LibrarySection.collection() will be deprecated in the future, use collections() (plural) instead.'
warnings.warn(msg, DeprecationWarning)
log.warning(msg)
return self.collections()

def collections(self, **kwargs):
""" Returns a list of collections from this library section.
See description of :func:`plexapi.library.LibrarySection.search` for details about filtering / sorting.
"""
return self.search(libtype='collection', **kwargs)

def playlists(self, **kwargs):
""" Returns a list of playlists from this library section. """
key = '/playlists?type=15&playlistType=%s&sectionID=%s' % (self.CONTENT_TYPE, self.key)
return self.fetchItems(key, **kwargs)


class MovieSection(LibrarySection):
""" Represents a :class:`~plexapi.library.LibrarySection` section containing movies.
Expand All @@ -855,10 +875,6 @@ class MovieSection(LibrarySection):
METADATA_TYPE = 'movie'
CONTENT_TYPE = 'video'

def collection(self, **kwargs):
""" Returns a list of collections from this library section. """
return self.search(libtype='collection', **kwargs)

def sync(self, videoQuality, limit=None, unwatched=False, **kwargs):
""" Add current Movie library section as sync item for specified device.
See description of :func:`~plexapi.library.LibrarySection.search` for details about filtering / sorting and
Expand Down Expand Up @@ -924,10 +940,6 @@ def recentlyAdded(self, libtype='episode', maxresults=50):
"""
return self.search(sort='episode.addedAt:desc', libtype=libtype, maxresults=maxresults)

def collection(self, **kwargs):
""" Returns a list of collections from this library section. """
return self.search(libtype='collection', **kwargs)

def sync(self, videoQuality, limit=None, unwatched=False, **kwargs):
""" Add current Show library section as sync item for specified device.
See description of :func:`~plexapi.library.LibrarySection.search` for details about filtering / sorting and
Expand Down Expand Up @@ -999,10 +1011,6 @@ def searchTracks(self, **kwargs):
""" Search for a track. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='track', **kwargs)

def collection(self, **kwargs):
""" Returns a list of collections from this library section. """
return self.search(libtype='collection', **kwargs)

def sync(self, bitrate, limit=None, **kwargs):
""" Add current Music library section as sync item for specified device.
See description of :func:`~plexapi.library.LibrarySection.search` for details about filtering / sorting and
Expand Down Expand Up @@ -1050,6 +1058,9 @@ class PhotoSection(LibrarySection):
CONTENT_TYPE = 'photo'
METADATA_TYPE = 'photo'

def collections(self, **kwargs):
raise NotImplementedError('Collections are not available for a Photo library.')

def searchAlbums(self, title, **kwargs):
""" Search for an album. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='photoalbum', title=title, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ def movie(movies):
@pytest.fixture()
def collection(plex):
try:
return plex.library.section("Movies").collection()[0]
return plex.library.section("Movies").collections()[0]
except IndexError:
movie = plex.library.section("Movies").get("Elephants Dream")
movie.addCollection(["marvel"])

n = plex.library.section("Movies").reload()
return n.collection()[0]
return n.collections()[0]


@pytest.fixture()
Expand Down
23 changes: 23 additions & 0 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def test_library_MovieSection_analyze(movies):
movies.analyze()


def test_library_MovieSection_collections(movies, collection):
assert len(movies.collections())


def test_library_ShowSection_searchShows(tvshows):
assert tvshows.searchShows(title="The 100")

Expand All @@ -193,6 +197,15 @@ def test_library_ShowSection_recentlyAdded(tvshows):
assert len(tvshows.recentlyAdded())


def test_library_ShowSection_playlists(plex, tvshows, show):
episodes = show.episodes()
playlist = plex.createPlaylist("test_library_ShowSection_playlists", episodes[:3])
try:
assert len(tvshows.playlists())
finally:
playlist.delete()


def test_library_MusicSection_albums(music):
assert len(music.albums())

Expand Down Expand Up @@ -277,6 +290,16 @@ def test_library_Colletion_edit(collection):
collection.edit(**{'titleSort.value': collectionTitleSort, 'titleSort.locked': 0})


def test_library_Collection_delete(movies, movie):
delete_collection = 'delete_collection'
movie.addCollection(delete_collection)
collections = movies.collections(title=delete_collection)
assert len(collections) == 1
collections[0].delete()
collections = movies.collections(title=delete_collection)
assert len(collections) == 0


def test_search_with_weird_a(plex):
ep_title = "Coup de Grâce"
result_root = plex.search(ep_title)
Expand Down

0 comments on commit 16a63f1

Please sign in to comment.