Utils plexapi.utils

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

Bases: logging.Filter

Logging filter to hide secrets.

filter(record)[source]

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.

plexapi.utils.registerPlexObject(cls)[source]

Registry of library types we may come across when parsing XML. This allows us to define a few helper functions to dynamically convery 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.

Parameters
  • func (func) – Calback function to used cast to type (int, bool, float).

  • value (any) – value to be cast and returned.

plexapi.utils.joinArgs(args)[source]

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 nexted 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.

Parameters
  • 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.

plexapi.utils.searchType(libtype)[source]

Returns the integer value of the library string type.

Parameters

libtype (str) – LibType to lookup (movie, show, season, episode, artist, album, track, collection)

Raises

NotFound – Unknown libtype

plexapi.utils.reverseSearchType(libtype)[source]

Returns the string value of the library type.

Parameters

libtype (int) – Integer value of the library type.

Raises

NotFound – Unknown libtype

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.

Parameters
  • 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.

Parameters
  • value (str) – value to return as a datetime

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

plexapi.utils.millisecondToHumanstr(milliseconds)[source]

Returns human readable time duration from milliseconds. HH:MM:SS:MMMM

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.

Parameters
  • 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.

Parameters
  • 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.

Returns

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

Return type

{‘hellowlol’

plexapi.utils.download(url, token, filename=None, savepath=None, session=None, chunksize=4024, 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.

Parameters
  • 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 evertything except write the file.

  • unpack (bool) – Unpack the zip file.

  • showstatus – Display a progressbar.

plexapi.utils.tag_helper(tag, items, locked=True, remove=False)[source]

Simple tag helper for editing a object.

plexapi.utils.getMyPlexAccount(opts=None)[source]

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.

Parameters
  • 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.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.