Utils plexapi.utils

class plexapi.utils.SecretsFilter(secrets=None)[source]

Bases: logging.Filter

Logging filter to hide secrets.


Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.


Registry of library types we may come across when parsing XML. This allows us to define a few helper functions to dynamically convert the XML into objects. See buildItem() below for an example.

plexapi.utils.cast(func, value)[source]

Cast the specified value to the specified type (returned by func). Currently this only support str, int, float, bool. Should be extended if needed.

  • func (func) – Callback function to used cast to type (int, bool, float).
  • value (any) – value to be cast and returned.

Returns a query string (uses for HTTP URLs) where only the value is URL encoded. Example return value: ‘?genre=action&type=1337’.

Parameters:args (dict) – Arguments to include in query string.
plexapi.utils.rget(obj, attrstr, default=None, delim='.')[source]

Returns the value at the specified attrstr location within a nested tree of dicts, lists, tuples, functions, classes, etc. The lookup is done recursively for each key in attrstr (split by by the delimiter) This function is heavily influenced by the lookups used in Django templates.

  • obj (any) – Object to start the lookup in (dict, obj, list, tuple, etc).
  • attrstr (str) – String to lookup (ex: ‘foo.bar.baz.value’)
  • default (any) – Default value to return if not found.
  • delim (str) – Delimiter separating keys in attrstr.

Returns the integer value of the library string type.

Parameters:libtype (str) – LibType to lookup (See SEARCHTYPES)
Raises:NotFound – Unknown libtype

Returns the string value of the library type.

Parameters:libtype (int) – Integer value of the library type.
Raises:NotFound – Unknown libtype

Returns the integer value of the library tag type.

Parameters:tag (str) – Tag to lookup (See TAGTYPES)
Raises:NotFound – Unknown tag

Returns the string value of the library tag type.

Parameters:tag (int) – Integer value of the library tag type.
Raises:NotFound – Unknown tag
plexapi.utils.threaded(callback, listargs)[source]

Returns the result of <callback> for each set of *args in listargs. Each call to <callback> is called concurrently in their own separate threads.

  • callback (func) – Callback function to apply to each set of *args.
  • listargs (list) – List of lists; *args to pass each thread.
plexapi.utils.toDatetime(value, format=None)[source]

Returns a datetime object from the specified value.

  • value (str) – value to return as a datetime
  • format (str) – Format to pass strftime (optional; if value is a str).

Returns human readable time duration [D day[s], ]HH:MM:SS.UUU from milliseconds.

Parameters:milliseconds (str, int) – time duration in milliseconds.
plexapi.utils.toList(value, itemcast=None, delim=', ')[source]

Returns a list of strings from the specified value.

  • value (str) – comma delimited string to convert to list.
  • itemcast (func) – Function to cast each list item to (default str).
  • delim (str) – string delimiter (optional; default ‘,’).
plexapi.utils.downloadSessionImages(server, filename=None, height=150, width=150, opacity=100, saturation=100)[source]

Helper to download a bif image or thumb.url from plex.server.sessions.

  • filename (str) – default to None,
  • height (int) – Height of the image.
  • width (int) – width of the image.
  • opacity (int) – Opacity of the resulting image (possibly deprecated).
  • saturation (int) – Saturating of the resulting image.

{‘filepath’: ‘<filepath>’, ‘url’: ‘http://<url>’}, {‘<username>’: {filepath, url}}, …

Return type:


plexapi.utils.download(url, token, filename=None, savepath=None, session=None, chunksize=4096, unpack=False, mocked=False, showstatus=False)[source]
Helper to download a thumb, videofile or other media item. Returns the local
path to the downloaded file.
  • url (str) – URL where the content be reached.
  • token (str) – Plex auth token to include in headers.
  • filename (str) – Filename of the downloaded file, default None.
  • savepath (str) – Defaults to current working dir.
  • chunksize (int) – What chunksize read/write at the time.
  • mocked (bool) – Helper to do everything except write the file.
  • unpack (bool) – Unpack the zip file.
  • showstatus – Display a progressbar.

Helper function tries to get a MyPlex Account instance by checking the the following locations for a username and password. This is useful to create user-friendly command line tools. 1. command-line options (opts). 2. environment variables and config.ini 3. Prompt on the command line.

plexapi.utils.createMyPlexDevice(headers, account, timeout=10)[source]

Helper function to create a new MyPlexDevice. Returns a new MyPlexDevice instance.

  • headers (dict) – Provide the X-Plex- headers for the new device. A unique X-Plex-Client-Identifier is required.
  • account (MyPlexAccount) – The Plex account to create the device on.
  • timeout (int) – Timeout in seconds to wait for device login.
plexapi.utils.plexOAuth(headers, forwardUrl=None, timeout=120)[source]

Helper function for Plex OAuth login. Returns a new MyPlexAccount instance.

  • headers (dict) – Provide the X-Plex- headers for the new device. A unique X-Plex-Client-Identifier is required.
  • forwardUrl (str, optional) – The url to redirect the client to after login.
  • timeout (int, optional) – Timeout in seconds to wait for device login. Default 120 seconds.
plexapi.utils.choose(msg, items, attr)[source]

Command line helper to display a list of choices, asking the user to choose one of the options.

plexapi.utils.getAgentIdentifier(section, agent)[source]

Return the full agent identifier from a short identifier, name, or confirm full identifier.

plexapi.utils.iterXMLBFS(root, tag=None)[source]

Iterate through an XML tree using a breadth-first search. If tag is specified, only return nodes with that tag.

plexapi.utils.toJson(obj, **kwargs)[source]

Convert an object to a JSON string.

  • obj (object) – The object to convert.
  • **kwargs (dict) – Keyword arguments to pass to json.dumps().

Return the SHA1 hash of a guid.