Skip to content

Commit

Permalink
Allow customizing XML includes when reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyWong16 committed Nov 21, 2020
1 parent 6430f06 commit a23f0ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
25 changes: 22 additions & 3 deletions plexapi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ def _buildItemOrNone(self, elem, cls=None, initpath=None):
except UnknownType:
return None

def _buildDetailsKey(self, **kwargs):
""" Builds the details key with the XML include parameters.
All parameters are included by default with the option to override each parameter
or disable each parameter individually by setting it to False or 0.
"""
if hasattr(self, '_includes'):
includes = {}
for k, v in self._includes.items():
value = kwargs.get(k, v)
if value not in [False, 0, '0']:
includes[k] = value
self._details_key = self.key + '?' + urlencode(includes, doseq=True)
return self._details_key

def fetchItem(self, ekey, cls=None, **kwargs):
""" Load the specified key to find and build the first item with the
specified tag and attrs. If no tag or attrs are specified then
Expand Down Expand Up @@ -203,9 +217,14 @@ def listAttrs(self, data, attr, **kwargs):
results.append(elem.attrib.get(attr))
return results

def reload(self, key=None):
""" Reload the data for this object from self.key. """
key = key or self._details_key or self.key
def reload(self, key=None, **kwargs):
""" Reload the data for this object from self.key.
Parameters:
key (string, optional): The key to reload.
**kwargs (dict): A dictionary of XML include parameters.
"""
key = key or self._buildDetailsKey(**kwargs) or self.key
if not key:
raise Unsupported('Cannot reload an object not built from a URL.')
self._initpath = key
Expand Down
34 changes: 20 additions & 14 deletions plexapi/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ class Video(PlexPartialObject):
updatedAt (datatime): Datetime this item was updated.
viewCount (int): Count of times this item was accessed.
"""
_includes = {
'checkFiles': 1,
'includeAllConcerts': 1,
'includeBandwidths': 1,
'includeChapters': 1,
'includeChildren': 1,
'includeConcerts': 1,
'includeExtras': 1,
'includeFields': [1, 'thumbBlurHash', 'artBlurHash'],
'includeGeolocation': 1,
'includeLoudnessRamps': 1,
'includeMarkers': 1,
'includeOnDeck': 1,
'includePopularLeaves': 1,
'includePreferences': 1,
'includeRelated': 1,
'includeRelatedCount': 1,
'includeReviews': 1,
'includeStations': 1
}

def _loadData(self, data):
""" Load attribute values from Plex XML response. """
Expand Down Expand Up @@ -286,16 +306,12 @@ class Movie(Playable, Video):
TAG = 'Video'
TYPE = 'movie'
METADATA_TYPE = 'movie'
_include = ('?checkFiles=1&includeExtras=1&includeRelated=1'
'&includeOnDeck=1&includeChapters=1&includePopularLeaves=1'
'&includeConcerts=1&includePreferences=1')

def _loadData(self, data):
""" Load attribute values from Plex XML response. """
Video._loadData(self, data)
Playable._loadData(self, data)

self._details_key = self.key + self._include
self.art = data.attrib.get('art')
self.audienceRating = utils.cast(float, data.attrib.get('audienceRating'))
self.audienceRatingImage = data.attrib.get('audienceRatingImage')
Expand Down Expand Up @@ -401,10 +417,6 @@ class Show(Video):
TYPE = 'show'
METADATA_TYPE = 'episode'

_include = ('?checkFiles=1&includeExtras=1&includeRelated=1'
'&includeOnDeck=1&includeChapters=1&includePopularLeaves=1'
'&includeMarkers=1&includeConcerts=1&includePreferences=1')

def __iter__(self):
for season in self.seasons():
yield season
Expand All @@ -414,7 +426,6 @@ def _loadData(self, data):
Video._loadData(self, data)
# fix key if loaded from search
self.key = self.key.replace('/children', '')
self._details_key = self.key + self._include
self.art = data.attrib.get('art')
self.banner = data.attrib.get('banner')
self.childCount = utils.cast(int, data.attrib.get('childCount'))
Expand Down Expand Up @@ -707,15 +718,10 @@ class Episode(Playable, Video):
TYPE = 'episode'
METADATA_TYPE = 'episode'

_include = ('?checkFiles=1&includeExtras=1&includeRelated=1'
'&includeOnDeck=1&includeChapters=1&includePopularLeaves=1'
'&includeMarkers=1&includeConcerts=1&includePreferences=1')

def _loadData(self, data):
""" Load attribute values from Plex XML response. """
Video._loadData(self, data)
Playable._loadData(self, data)
self._details_key = self.key + self._include
self._seasonNumber = None # cached season number
self.art = data.attrib.get('art')
self.chapterSource = data.attrib.get('chapterSource')
Expand Down

0 comments on commit a23f0ef

Please sign in to comment.