Skip to content
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
38 changes: 29 additions & 9 deletions plexapi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def sectionByID(self, sectionID):
""" Returns the :class:`~plexapi.library.LibrarySection` that matches the specified sectionID.

Parameters:
sectionID (str): ID of the section to return.
sectionID (int): ID of the section to return.
"""
if not self._sectionsByID or sectionID not in self._sectionsByID:
self.sections()
Expand Down Expand Up @@ -355,7 +355,6 @@ def _loadData(self, data):
# Private attrs as we dont want a reload.
self._filterTypes = None
self._fieldTypes = None
self._totalSize = None
self._totalViewSize = None

def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, **kwargs):
Expand Down Expand Up @@ -394,13 +393,31 @@ def fetchItems(self, ekey, cls=None, container_start=None, container_size=None,

@property
def totalSize(self):
""" Returns the total number of items in the library. """
if self._totalSize is None:
part = '/library/sections/%s/all?X-Plex-Container-Start=0&X-Plex-Container-Size=0' % self.key
data = self._server.query(part)
self._totalSize = int(data.attrib.get("totalSize"))
""" Returns the total number of items in the library for the default library type. """
return self.totalViewSize(libtype=self.TYPE, includeCollections=False)

return self._totalSize
def totalViewSize(self, libtype=None, includeCollections=True):
""" Returns the total number of items in the library for a specified libtype.
The number of items for the default library type will be returned if no libtype is specified.
(e.g. Specify ``libtype='episode'`` for the total number of episodes
or ``libtype='albums'`` for the total number of albums.)

Parameters:
libtype (str, optional): The type of items to return the total number for (movie, show, season, episode,
artist, album, track, photoalbum). Default is the main library type.
includeCollections (bool, optional): True or False to include collections in the total number.
Default is True.
"""
args = {
'includeCollections': int(bool(includeCollections)),
'X-Plex-Container-Start': 0,
'X-Plex-Container-Size': 0
}
if libtype is not None:
args['type'] = utils.searchType(libtype)
part = '/library/sections/%s/all%s' % (self.key, utils.joinArgs(args))
data = self._server.query(part)
return utils.cast(int, data.attrib.get("totalSize"))

def delete(self):
""" Delete a library section. """
Expand Down Expand Up @@ -1082,7 +1099,10 @@ def search(self, title=None, sort=None, maxresults=None,
filter_args.append(self._validateFilterField(field, values, libtype))
del kwargs[field]
if title is not None:
args['title'] = title
if isinstance(title, (list, tuple)):
filter_args.append(self._validateFilterField('title', title, libtype))
else:
args['title'] = title
if sort is not None:
args['sort'] = self._validateSortField(sort, libtype)
if libtype is not None:
Expand Down
18 changes: 9 additions & 9 deletions plexapi/myplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,28 +802,28 @@ def history(self, maxresults=9999999, mindate=None):

class Section(PlexObject):
""" This refers to a shared section. The raw xml for the data presented here
can be found at: https://plex.tv/api/servers/{machineId}/shared_servers/{serverId}
can be found at: https://plex.tv/api/servers/{machineId}/shared_servers

Attributes:
TAG (str): section
id (int): shared section id
sectionKey (str): what key we use for this section
id (int): The shared section ID
key (int): The shared library section key
shared (bool): If this section is shared with the user
title (str): Title of the section
sectionId (str): shared section id
type (str): movie, tvshow, artist
shared (bool): If this section is shared with the user

"""
TAG = 'Section'

def _loadData(self, data):
self._data = data
# self.id = utils.cast(int, data.attrib.get('id')) # Havnt decided if this should be changed.
self.sectionKey = data.attrib.get('key')
self.id = utils.cast(int, data.attrib.get('id'))
self.key = utils.cast(int, data.attrib.get('key'))
self.shared = utils.cast(bool, data.attrib.get('shared', '0'))
self.title = data.attrib.get('title')
self.sectionId = data.attrib.get('id')
self.type = data.attrib.get('type')
self.shared = utils.cast(bool, data.attrib.get('shared'))
self.sectionId = self.id # For backwards compatibility
self.sectionKey = self.key # For backwards compatibility

def history(self, maxresults=9999999, mindate=None):
""" Get all Play History for a user for this section in this shared server.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ def test_library_section_movies_all(movies):
assert len(movies.all(container_start=0, container_size=1, maxresults=1)) == 1


def test_library_section_totalViewSize(tvshows):
assert tvshows.totalViewSize() == 2
assert tvshows.totalViewSize(libtype="show") == 2
assert tvshows.totalViewSize(libtype="season") == 4
assert tvshows.totalViewSize(libtype="episode") == 49
show = tvshows.get("The 100")
show.addCollection("test_view_size")
assert tvshows.totalViewSize() == 3
assert tvshows.totalViewSize(includeCollections=False) == 2
show.removeCollection("test_view_size", locked=False)


def test_library_section_delete(movies, patched_http_call):
movies.delete()

Expand Down