Add random sorting option

This commit is contained in:
JonnyWong16 2021-05-15 11:25:26 -07:00
parent 2af4f6d3eb
commit 6d4e7bed95
2 changed files with 25 additions and 0 deletions

View file

@ -2,6 +2,7 @@
import re
import weakref
from urllib.parse import quote_plus, urlencode
from xml.etree import ElementTree
from plexapi import log, utils
from plexapi.exceptions import BadRequest, NotFound, UnknownType, Unsupported
@ -130,6 +131,19 @@ class PlexObject(object):
return True
return False
def _manuallyLoadXML(self, xml, cls=None):
""" Manually load an XML string as a :class:`~plexapi.base.PlexObject`.
Parameters:
xml (str): The XML string to load.
cls (:class:`~plexapi.base.PlexObject`): If you know the class of the
items to be fetched, passing this in will help the parser ensure
it only returns those items. By default we convert the xml elements
with the best guess PlexObjects based on tag and type attrs.
"""
elem = ElementTree.fromstring(xml)
return self._buildItemOrNone(elem, cls)
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

View file

@ -1836,6 +1836,11 @@ class FilteringType(PlexObject):
self.title = data.attrib.get('title')
self.type = data.attrib.get('type')
# Random is a valid sorting that is not exposed on the Plex server.
# Manually add the random sort XML.
_randomSortXML = '<Sort defaultDirection="desc" descKey="random" key="random" title="Random" />'
self.sorts.append(self._manuallyLoadXML(_randomSortXML, FilteringSort))
class FilteringFilter(PlexObject):
""" Represents a single Filter object for a :class:`~plexapi.library.FilteringType`.
@ -1864,6 +1869,9 @@ class FilteringSort(PlexObject):
Attributes:
TAG (str): 'Sort'
active (bool): True if the sort is currently active.
activeDirection (str): The currently active sorting direction.
default (str): The currently active default sorting direction.
defaultDirection (str): The default sorting direction.
descKey (str): The URL key for sorting with desc.
firstCharacterKey (str): API URL path for first character endpoint.
@ -1875,6 +1883,9 @@ class FilteringSort(PlexObject):
def _loadData(self, data):
""" Load attribute values from Plex XML response. """
self._data = data
self.active = utils.cast(bool, data.attrib.get('active', '0'))
self.activeDirection = data.attrib.get('activeDirection')
self.default = data.attrib.get('default')
self.defaultDirection = data.attrib.get('defaultDirection')
self.descKey = data.attrib.get('descKey')
self.firstCharacterKey = data.attrib.get('firstCharacterKey')