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

Add library recently added methods for each libtype #751

Merged
merged 2 commits into from
May 24, 2021
Merged
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
70 changes: 65 additions & 5 deletions plexapi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,16 @@ def onDeck(self):
key = '/library/sections/%s/onDeck' % self.key
return self.fetchItems(key)

def recentlyAdded(self, maxresults=50):
def recentlyAdded(self, maxresults=50, libtype=None):
""" Returns a list of media items recently added from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
libtype (str, optional): The library type to filter (movie, show, season, episode,
artist, album, track, photoalbum, photo). Default is the main library type.
"""
return self.search(sort='addedAt:desc', maxresults=maxresults)
libtype = libtype or self.TYPE
return self.search(sort='addedAt:desc', maxresults=maxresults, libtype=libtype)

def firstCharacter(self):
key = '/library/sections/%s/firstCharacter' % self.key
Expand Down Expand Up @@ -1399,6 +1402,14 @@ def searchMovies(self, **kwargs):
""" Search for a movie. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='movie', **kwargs)

def recentlyAddedMovies(self, maxresults=50):
""" Returns a list of recently added movies from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.recentlyAdded(maxresults=maxresults, libtype='movie')

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 @@ -1460,13 +1471,29 @@ def searchEpisodes(self, **kwargs):
""" Search for an episode. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='episode', **kwargs)

def recentlyAdded(self, maxresults=50):
def recentlyAddedShows(self, maxresults=50):
""" Returns a list of recently added shows from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.recentlyAdded(maxresults=maxresults, libtype='show')

def recentlyAddedSeasons(self, maxresults=50):
""" Returns a list of recently added seasons from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.recentlyAdded(maxresults=maxresults, libtype='season')

def recentlyAddedEpisodes(self, maxresults=50):
""" Returns a list of recently added episodes from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.search(sort='episode.addedAt:desc', maxresults=maxresults)
return self.recentlyAdded(maxresults=maxresults, libtype='episode')

def sync(self, videoQuality, limit=None, unwatched=False, **kwargs):
""" Add current Show library section as sync item for specified device.
Expand Down Expand Up @@ -1539,6 +1566,30 @@ def searchTracks(self, **kwargs):
""" Search for a track. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='track', **kwargs)

def recentlyAddedArtists(self, maxresults=50):
""" Returns a list of recently added artists from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.recentlyAdded(maxresults=maxresults, libtype='artist')

def recentlyAddedAlbums(self, maxresults=50):
""" Returns a list of recently added albums from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.recentlyAdded(maxresults=maxresults, libtype='album')

def recentlyAddedTracks(self, maxresults=50):
""" Returns a list of recently added tracks from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
return self.recentlyAdded(maxresults=maxresults, libtype='track')

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 @@ -1597,13 +1648,22 @@ 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. """
""" Search for a photo album. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='photoalbum', title=title, **kwargs)

def searchPhotos(self, title, **kwargs):
""" Search for a photo. See :func:`~plexapi.library.LibrarySection.search` for usage. """
return self.search(libtype='photo', title=title, **kwargs)

def recentlyAddedAlbums(self, maxresults=50):
""" Returns a list of recently added photo albums from this library section.

Parameters:
maxresults (int): Max number of items to return (default 50).
"""
# Use search() instead of recentlyAdded() because libtype=None
return self.search(sort='addedAt:desc', maxresults=maxresults)

def sync(self, resolution, 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
27 changes: 23 additions & 4 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ def test_library_MovieSection_searchMovies(movies):
assert movies.searchMovies(title="Elephants Dream")


def test_library_MovieSection_recentlyAdded(movies):
assert len(movies.recentlyAdded())
def test_library_MovieSection_recentlyAdded(movies, movie):
assert movie in movies.recentlyAdded()
assert movie in movies.recentlyAddedMovies()


def test_library_MovieSection_analyze(movies):
Expand All @@ -214,8 +215,13 @@ def test_library_ShowSection_searchEpisodes(tvshows):
assert tvshows.searchEpisodes(title="Winter Is Coming")


def test_library_ShowSection_recentlyAdded(tvshows):
assert len(tvshows.recentlyAdded())
def test_library_ShowSection_recentlyAdded(tvshows, show):
season = show.season(1)
episode = season.episode(1)
assert show in tvshows.recentlyAdded()
assert show in tvshows.recentlyAddedShows()
assert season in tvshows.recentlyAddedSeasons()
assert episode in tvshows.recentlyAddedEpisodes()


def test_library_ShowSection_playlists(plex, tvshows, show):
Expand All @@ -239,6 +245,15 @@ def test_library_MusicSection_searchAlbums(music):
assert len(music.searchAlbums(title="Layers"))


def test_library_MusicSection_recentlyAdded(music, artist):
album = artist.albums()[0]
track = album.tracks()[0]
assert artist in music.recentlyAdded()
assert artist in music.recentlyAddedArtists()
assert album in music.recentlyAddedAlbums()
assert track in music.recentlyAddedTracks()


def test_library_PhotoSection_searchAlbums(photos, photoalbum):
title = photoalbum.title
albums = photos.searchAlbums(title)
Expand All @@ -250,6 +265,10 @@ def test_library_PhotoSection_searchPhotos(photos, photoalbum):
assert len(photos.searchPhotos(title))


def test_library_PhotoSection_recentlyAdded(photos, photoalbum):
assert photoalbum in photos.recentlyAddedAlbums()


def test_library_and_section_search_for_movie(plex, movies):
find = "Elephants Dream"
l_search = plex.library.search(find)
Expand Down