2016-03-21 04:26:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-12-29 03:21:58 +00:00
|
|
|
"""
|
|
|
|
PlexServer
|
|
|
|
"""
|
2016-12-15 23:06:12 +00:00
|
|
|
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
2015-09-05 14:09:15 +00:00
|
|
|
import requests
|
2014-12-29 03:21:58 +00:00
|
|
|
from requests.status_codes import _codes as codes
|
2016-12-15 23:06:12 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
from plexapi import BASE_HEADERS, TIMEOUT
|
2016-03-17 04:51:20 +00:00
|
|
|
from plexapi import log, utils
|
2016-04-10 03:59:47 +00:00
|
|
|
from plexapi import audio, video, photo, playlist # noqa; required
|
2016-04-02 06:19:32 +00:00
|
|
|
from plexapi.client import PlexClient
|
2016-12-15 23:06:12 +00:00
|
|
|
from plexapi.compat import quote
|
2014-12-29 03:21:58 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound
|
|
|
|
from plexapi.library import Library
|
2016-04-11 03:49:23 +00:00
|
|
|
from plexapi.playlist import Playlist
|
2014-12-29 03:21:58 +00:00
|
|
|
from plexapi.playqueue import PlayQueue
|
|
|
|
|
|
|
|
|
|
|
|
class PlexServer(object):
|
2016-12-15 23:06:12 +00:00
|
|
|
"""Main class to interact with plexapi
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>>> plex = PlexServer(token=12345)
|
|
|
|
>>>> for client in plex.clients():
|
|
|
|
>>>> print(client.title)
|
|
|
|
|
|
|
|
Note:
|
|
|
|
See test/example.py for more examples
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, baseurl='http://localhost:32400', token=None, session=None):
|
|
|
|
"""Args:
|
|
|
|
baseurl (string): Base url for PMS
|
|
|
|
token (string): X-Plex-Token, using for authenication with PMS
|
|
|
|
session (requests.Session, optional): Use your own session object if you want
|
|
|
|
to cache the http responses from PMS
|
|
|
|
"""
|
|
|
|
self.baseurl = baseurl
|
2014-12-29 03:21:58 +00:00
|
|
|
self.token = token
|
2016-03-16 03:53:04 +00:00
|
|
|
self.session = session or requests.Session()
|
2014-12-29 03:21:58 +00:00
|
|
|
data = self._connect()
|
|
|
|
self.friendlyName = data.attrib.get('friendlyName')
|
|
|
|
self.machineIdentifier = data.attrib.get('machineIdentifier')
|
|
|
|
self.myPlex = bool(data.attrib.get('myPlex'))
|
|
|
|
self.myPlexMappingState = data.attrib.get('myPlexMappingState')
|
|
|
|
self.myPlexSigninState = data.attrib.get('myPlexSigninState')
|
|
|
|
self.myPlexSubscription = data.attrib.get('myPlexSubscription')
|
|
|
|
self.myPlexUsername = data.attrib.get('myPlexUsername')
|
|
|
|
self.platform = data.attrib.get('platform')
|
|
|
|
self.platformVersion = data.attrib.get('platformVersion')
|
2015-06-15 02:45:22 +00:00
|
|
|
self.transcoderActiveVideoSessions = int(data.attrib.get('transcoderActiveVideoSessions', 0))
|
|
|
|
self.updatedAt = int(data.attrib.get('updatedAt', 0))
|
2014-12-29 03:21:58 +00:00
|
|
|
self.version = data.attrib.get('version')
|
2016-04-12 02:43:21 +00:00
|
|
|
self._library = None # cached library
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-04-02 06:19:32 +00:00
|
|
|
return '<%s:%s>' % (self.__class__.__name__, self.baseurl)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def _connect(self):
|
|
|
|
try:
|
|
|
|
return self.query('/')
|
2015-02-17 20:35:17 +00:00
|
|
|
except Exception as err:
|
2016-04-02 06:19:32 +00:00
|
|
|
log.error('%s: %s', self.baseurl, err)
|
|
|
|
raise NotFound('No server found at: %s' % self.baseurl)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def library(self):
|
2016-04-12 02:43:21 +00:00
|
|
|
if not self._library:
|
|
|
|
self._library = Library(self, self.query('/library/'))
|
|
|
|
return self._library
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def account(self):
|
|
|
|
data = self.query('/myplex/account')
|
2016-04-07 05:39:04 +00:00
|
|
|
return Account(self, data)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def clients(self):
|
2016-12-15 23:06:12 +00:00
|
|
|
"""Querys PMS for all clients connected to PMS
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: of clients connnected to PMS
|
|
|
|
|
|
|
|
"""
|
2014-12-29 03:21:58 +00:00
|
|
|
items = []
|
|
|
|
for elem in self.query('/clients'):
|
2016-12-15 23:06:12 +00:00
|
|
|
baseurl = 'http://%s:%s' % (elem.attrib['address'],
|
|
|
|
elem.attrib['port'])
|
2016-04-06 03:32:49 +00:00
|
|
|
items.append(PlexClient(baseurl, server=self, data=elem))
|
2014-12-29 03:21:58 +00:00
|
|
|
return items
|
|
|
|
|
|
|
|
def client(self, name):
|
2016-12-15 23:06:12 +00:00
|
|
|
"""Querys PMS for all clients connected to PMS
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Plexclient
|
|
|
|
|
|
|
|
"""
|
2014-12-29 03:21:58 +00:00
|
|
|
for elem in self.query('/clients'):
|
|
|
|
if elem.attrib.get('name').lower() == name.lower():
|
2016-12-15 23:06:12 +00:00
|
|
|
baseurl = 'http://%s:%s' % (
|
|
|
|
elem.attrib['address'], elem.attrib['port'])
|
2016-04-06 03:32:49 +00:00
|
|
|
return PlexClient(baseurl, server=self, data=elem)
|
2014-12-29 03:21:58 +00:00
|
|
|
raise NotFound('Unknown client name: %s' % name)
|
|
|
|
|
2016-04-11 03:49:23 +00:00
|
|
|
def createPlaylist(self, title, items):
|
|
|
|
return Playlist.create(self, title, items)
|
|
|
|
|
2016-03-21 04:26:02 +00:00
|
|
|
def createPlayQueue(self, item):
|
|
|
|
return PlayQueue.create(self, item)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def headers(self):
|
|
|
|
headers = BASE_HEADERS
|
|
|
|
if self.token:
|
|
|
|
headers['X-Plex-Token'] = self.token
|
|
|
|
return headers
|
2016-12-15 23:06:12 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def history(self):
|
|
|
|
return utils.listItems(self, '/status/sessions/history/all')
|
2016-12-15 23:06:12 +00:00
|
|
|
|
2016-03-22 03:52:58 +00:00
|
|
|
def playlists(self):
|
2016-04-11 03:49:23 +00:00
|
|
|
# TODO: Add sort and type options?
|
|
|
|
# /playlists/all?type=15&sort=titleSort%3Aasc&playlistType=video&smart=0
|
2016-03-22 03:52:58 +00:00
|
|
|
return utils.listItems(self, '/playlists')
|
2016-12-15 23:06:12 +00:00
|
|
|
|
2016-03-22 03:52:58 +00:00
|
|
|
def playlist(self, title=None): # noqa
|
|
|
|
for item in self.playlists():
|
|
|
|
if item.title == title:
|
|
|
|
return item
|
|
|
|
raise NotFound('Invalid playlist title: %s' % title)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2016-04-06 03:32:49 +00:00
|
|
|
def query(self, path, method=None, headers=None, **kwargs):
|
2014-12-29 03:21:58 +00:00
|
|
|
url = self.url(path)
|
2016-03-16 03:53:04 +00:00
|
|
|
method = method or self.session.get
|
2015-02-24 03:42:29 +00:00
|
|
|
log.info('%s %s', method.__name__.upper(), url)
|
2016-12-15 23:06:12 +00:00
|
|
|
h = headers.copy()
|
|
|
|
h.update(headers or {})
|
|
|
|
response = method(url, headers=h, timeout=TIMEOUT, **kwargs)
|
2014-12-29 03:21:58 +00:00
|
|
|
if response.status_code not in [200, 201]:
|
|
|
|
codename = codes.get(response.status_code)[0]
|
|
|
|
raise BadRequest('(%s) %s' % (response.status_code, codename))
|
|
|
|
data = response.text.encode('utf8')
|
|
|
|
return ElementTree.fromstring(data) if data else None
|
2016-12-15 23:06:12 +00:00
|
|
|
|
2016-01-19 10:59:00 +00:00
|
|
|
def search(self, query, mediatype=None):
|
2016-03-31 20:52:48 +00:00
|
|
|
""" Searching within a library section is much more powerful. """
|
2016-03-21 04:26:02 +00:00
|
|
|
items = utils.listItems(self, '/search?query=%s' % quote(query))
|
2016-01-28 12:09:36 +00:00
|
|
|
if mediatype:
|
|
|
|
return [item for item in items if item.type == mediatype]
|
|
|
|
return items
|
|
|
|
|
2015-02-17 20:35:17 +00:00
|
|
|
def sessions(self):
|
2016-03-21 04:26:02 +00:00
|
|
|
return utils.listItems(self, '/status/sessions')
|
2015-02-17 20:35:17 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
def url(self, path):
|
2015-06-08 16:41:47 +00:00
|
|
|
if self.token:
|
|
|
|
delim = '&' if '?' in path else '?'
|
2016-04-02 06:19:32 +00:00
|
|
|
return '%s%s%sX-Plex-Token=%s' % (self.baseurl, path, delim, self.token)
|
|
|
|
return '%s%s' % (self.baseurl, path)
|
2016-04-07 05:39:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Account(object):
|
2016-12-15 23:06:12 +00:00
|
|
|
"""This is the locally cached MyPlex account information. The properties provided don't match
|
|
|
|
the myplex.MyPlexAccount object very well. I believe this is here because access to myplex
|
|
|
|
is not required to get basic plex information.
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
|
|
|
|
def __init__(self, server, data):
|
|
|
|
self.authToken = data.attrib.get('authToken')
|
|
|
|
self.username = data.attrib.get('username')
|
|
|
|
self.mappingState = data.attrib.get('mappingState')
|
|
|
|
self.mappingError = data.attrib.get('mappingError')
|
|
|
|
self.mappingErrorMessage = data.attrib.get('mappingErrorMessage')
|
|
|
|
self.signInState = data.attrib.get('signInState')
|
|
|
|
self.publicAddress = data.attrib.get('publicAddress')
|
|
|
|
self.publicPort = data.attrib.get('publicPort')
|
|
|
|
self.privateAddress = data.attrib.get('privateAddress')
|
|
|
|
self.privatePort = data.attrib.get('privatePort')
|
|
|
|
self.subscriptionFeatures = data.attrib.get('subscriptionFeatures')
|
|
|
|
self.subscriptionActive = data.attrib.get('subscriptionActive')
|
|
|
|
self.subscriptionState = data.attrib.get('subscriptionState')
|