Allow customizing XML includes when reloading

pkkid/python-plexapi#603
This commit is contained in:
JonnyWong16 2020-11-20 19:51:02 -08:00
parent 6430f06e6b
commit c171aa2745
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 42 additions and 17 deletions

View file

@ -89,6 +89,20 @@ class PlexObject(object):
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] = 1 if value is True else 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
@ -203,9 +217,14 @@ class PlexObject(object):
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

View file

@ -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. """
@ -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')
@ -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
@ -414,7 +426,6 @@ class Show(Video):
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'))
@ -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')