2016-03-21 04:26:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-01-02 21:06:40 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info <= (3, 3):
|
|
|
|
try:
|
|
|
|
from xml.etree import cElementTree as ElementTree
|
|
|
|
except ImportError:
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
else:
|
|
|
|
# py 3.3 and above selects the fastest automatically
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
|
|
|
from requests.status_codes import _codes as codes
|
|
|
|
|
|
|
|
import plexapi
|
|
|
|
import requests
|
2015-09-29 18:14:30 +00:00
|
|
|
from plexapi import TIMEOUT, log, utils
|
2015-02-24 03:42:29 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
|
2016-04-02 06:19:32 +00:00
|
|
|
from plexapi.client import PlexClient
|
|
|
|
from plexapi.server import PlexServer
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
class MyPlexAccount(object):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Your personal MyPlex account and profile information
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
authenticationToken (TYPE): Description
|
|
|
|
BASEURL (str): Description
|
|
|
|
certificateVersion (TYPE): Description
|
|
|
|
cloudSyncDevice (TYPE): Description
|
|
|
|
email (TYPE): Description
|
|
|
|
entitlements (TYPE): Description
|
|
|
|
guest (TYPE): Description
|
|
|
|
home (TYPE): Description
|
|
|
|
homeSize (TYPE): Description
|
|
|
|
id (TYPE): Description
|
|
|
|
locale (TYPE): Description
|
|
|
|
mailing_list_status (TYPE): Description
|
|
|
|
maxHomeSize (TYPE): Description
|
|
|
|
queueEmail (TYPE): Description
|
|
|
|
queueUid (TYPE): Description
|
|
|
|
restricted (TYPE): Description
|
|
|
|
roles (TYPE): Description
|
|
|
|
scrobbleTypes (TYPE): Description
|
|
|
|
secure (TYPE): Description
|
|
|
|
SIGNIN (str): Description
|
|
|
|
subscriptionActive (TYPE): Description
|
|
|
|
subscriptionFeatures (TYPE): Description
|
|
|
|
subscriptionPlan (TYPE): Description
|
|
|
|
subscriptionStatus (TYPE): Description
|
|
|
|
thumb (TYPE): Description
|
|
|
|
title (TYPE): Description
|
|
|
|
username (TYPE): Description
|
|
|
|
uuid (TYPE): Description
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
BASEURL = 'https://plex.tv/users/account'
|
2016-04-04 02:46:25 +00:00
|
|
|
SIGNIN = 'https://my.plexapp.com/users/sign_in.xml'
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2015-02-17 20:35:17 +00:00
|
|
|
def __init__(self, data, initpath=None):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Sets the attrs.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (Element): XML response from PMS as a Element
|
|
|
|
initpath (string, optional): relative path.
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
self.authenticationToken = data.attrib.get('authenticationToken')
|
|
|
|
self.certificateVersion = data.attrib.get('certificateVersion')
|
|
|
|
self.cloudSyncDevice = data.attrib.get('cloudSyncDevice')
|
2014-12-29 03:21:58 +00:00
|
|
|
self.email = data.attrib.get('email')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.guest = utils.cast(bool, data.attrib.get('guest'))
|
|
|
|
self.home = utils.cast(bool, data.attrib.get('home'))
|
|
|
|
self.homeSize = utils.cast(int, data.attrib.get('homeSize'))
|
2014-12-29 03:21:58 +00:00
|
|
|
self.id = data.attrib.get('id')
|
2016-04-04 02:46:25 +00:00
|
|
|
self.locale = data.attrib.get('locale')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.mailing_list_status = data.attrib.get('mailing_list_status')
|
|
|
|
self.maxHomeSize = utils.cast(int, data.attrib.get('maxHomeSize'))
|
2014-12-29 03:21:58 +00:00
|
|
|
self.queueEmail = data.attrib.get('queueEmail')
|
|
|
|
self.queueUid = data.attrib.get('queueUid')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.restricted = utils.cast(bool, data.attrib.get('restricted'))
|
|
|
|
self.scrobbleTypes = data.attrib.get('scrobbleTypes')
|
|
|
|
self.secure = utils.cast(bool, data.attrib.get('secure'))
|
|
|
|
self.thumb = data.attrib.get('thumb')
|
|
|
|
self.title = data.attrib.get('title')
|
|
|
|
self.username = data.attrib.get('username')
|
|
|
|
self.uuid = data.attrib.get('uuid')
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-04 02:46:25 +00:00
|
|
|
# TODO: Complete these items!
|
|
|
|
self.subscriptionActive = None # renamed on server
|
|
|
|
self.subscriptionStatus = None # renamed on server
|
|
|
|
self.subscriptionPlan = None # renmaed on server
|
|
|
|
self.subscriptionFeatures = None # renamed on server
|
|
|
|
self.roles = None
|
|
|
|
self.entitlements = None
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def __repr__(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Pretty print."""
|
2016-04-07 05:39:04 +00:00
|
|
|
return '<%s:%s:%s>' % (self.__class__.__name__, self.id, self.username.encode('utf8'))
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def devices(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Return a all devices connected to the plex account.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: of MyPlexDevice
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
return _listItems(MyPlexDevice.BASEURL, self.authenticationToken, MyPlexDevice)
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def device(self, name):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Return a device wth a matching name.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): Name to match against.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
class: MyPlexDevice
|
|
|
|
"""
|
2016-04-02 06:19:32 +00:00
|
|
|
return _findItem(self.devices(), name)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def resources(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Resources.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List: of MyPlexResource
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
return _listItems(MyPlexResource.BASEURL, self.authenticationToken, MyPlexResource)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def resource(self, name):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Find resource ny name.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): to find
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
class: MyPlexResource
|
|
|
|
"""
|
2016-04-02 06:19:32 +00:00
|
|
|
return _findItem(self.resources(), name)
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def users(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""List of users.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List: of MyPlexuser
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
return _listItems(MyPlexUser.BASEURL, self.authenticationToken, MyPlexUser)
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def user(self, email):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Find a user by email.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
email (str): Username to match against.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
class: User
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
return _findItem(self.users(), email, ['username', 'email'])
|
2015-09-29 18:14:30 +00:00
|
|
|
|
2015-02-17 20:35:17 +00:00
|
|
|
@classmethod
|
|
|
|
def signin(cls, username, password):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
username (str): username
|
|
|
|
password (str): password
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
class: MyPlexAccount
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
BadRequest: (HTTPCODE) http codename
|
|
|
|
Unauthorized: (HTTPCODE) http codename
|
|
|
|
"""
|
2015-02-24 03:42:29 +00:00
|
|
|
if 'X-Plex-Token' in plexapi.BASE_HEADERS:
|
|
|
|
del plexapi.BASE_HEADERS['X-Plex-Token']
|
2015-02-17 20:35:17 +00:00
|
|
|
auth = (username, password)
|
2016-04-04 02:46:25 +00:00
|
|
|
log.info('POST %s', cls.SIGNIN)
|
2017-01-02 21:06:40 +00:00
|
|
|
response = requests.post(
|
|
|
|
cls.SIGNIN, headers=plexapi.BASE_HEADERS, auth=auth, timeout=TIMEOUT)
|
2015-02-17 20:35:17 +00:00
|
|
|
if response.status_code != requests.codes.created:
|
|
|
|
codename = codes.get(response.status_code)[0]
|
2015-02-24 03:42:29 +00:00
|
|
|
if response.status_code == 401:
|
2017-01-02 21:06:40 +00:00
|
|
|
raise Unauthorized('(%s) %s' %
|
|
|
|
(response.status_code, codename))
|
2015-02-17 20:35:17 +00:00
|
|
|
raise BadRequest('(%s) %s' % (response.status_code, codename))
|
|
|
|
data = ElementTree.fromstring(response.text.encode('utf8'))
|
2016-04-07 05:39:04 +00:00
|
|
|
return cls(data, cls.SIGNIN)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
2017-01-02 21:06:40 +00:00
|
|
|
# Not to be confused with the MyPlexAccount, this represents
|
2016-04-07 05:39:04 +00:00
|
|
|
# non-signed in users such as friends and linked accounts.
|
|
|
|
class MyPlexUser(object):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Class to other users.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
allowCameraUpload (bool): True if this user can upload images
|
|
|
|
allowChannels (bool): True if this user has access to channels
|
|
|
|
allowSync (bool): True if this user can sync
|
|
|
|
BASEURL (str): Description
|
|
|
|
email (str): user@gmail.com
|
|
|
|
filterAll (str): Description
|
|
|
|
filterMovies (str): Description
|
|
|
|
filterMusic (str): Description
|
|
|
|
filterPhotos (str): Description
|
|
|
|
filterTelevision (str): Description
|
|
|
|
home (bool):
|
|
|
|
id (int): 1337
|
|
|
|
protected (False): Is this if ssl? check it
|
|
|
|
recommendationsPlaylistId (str): Description
|
|
|
|
restricted (str): fx 0
|
|
|
|
thumb (str): Link to the users avatar
|
|
|
|
title (str): Hellowlol
|
|
|
|
username (str): Hellowlol
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
BASEURL = 'https://plex.tv/api/users/'
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def __init__(self, data, initpath=None):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (Element): XML repsonse as Element
|
|
|
|
initpath (None, optional): Relative url str
|
|
|
|
"""
|
|
|
|
self.allowCameraUpload = utils.cast(
|
|
|
|
bool, data.attrib.get('allowCameraUpload'))
|
2016-04-07 05:39:04 +00:00
|
|
|
self.allowChannels = utils.cast(bool, data.attrib.get('allowChannels'))
|
|
|
|
self.allowSync = utils.cast(bool, data.attrib.get('allowSync'))
|
|
|
|
self.email = data.attrib.get('email')
|
|
|
|
self.filterAll = data.attrib.get('filterAll')
|
|
|
|
self.filterMovies = data.attrib.get('filterMovies')
|
|
|
|
self.filterMusic = data.attrib.get('filterMusic')
|
|
|
|
self.filterPhotos = data.attrib.get('filterPhotos')
|
|
|
|
self.filterTelevision = data.attrib.get('filterTelevision')
|
|
|
|
self.home = utils.cast(bool, data.attrib.get('home'))
|
|
|
|
self.id = utils.cast(int, data.attrib.get('id'))
|
|
|
|
self.protected = utils.cast(bool, data.attrib.get('protected'))
|
2017-01-02 21:06:40 +00:00
|
|
|
self.recommendationsPlaylistId = data.attrib.get(
|
|
|
|
'recommendationsPlaylistId')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.restricted = data.attrib.get('restricted')
|
|
|
|
self.thumb = data.attrib.get('thumb')
|
|
|
|
self.title = data.attrib.get('title')
|
2014-12-29 03:21:58 +00:00
|
|
|
self.username = data.attrib.get('username')
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def __repr__(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Pretty repr."""
|
2016-04-07 05:39:04 +00:00
|
|
|
return '<%s:%s:%s>' % (self.__class__.__name__, self.id, self.username)
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2015-11-05 03:57:08 +00:00
|
|
|
class MyPlexResource(object):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Summary
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
accessToken (str): This resource accesstoken.
|
|
|
|
BASEURL (TYPE): Description
|
|
|
|
clientIdentifier (str): 1f2fe128794fd...
|
|
|
|
connections (list): of ResourceConnection
|
|
|
|
createdAt (datetime): Description
|
|
|
|
device (str): pc
|
|
|
|
home (None): Dunno wtf this can me
|
|
|
|
lastSeenAt (datetime): Description
|
|
|
|
name (str): Pretty name fx S-PC
|
|
|
|
owned (bool): True if this is your own.
|
|
|
|
platform (str): Windows
|
|
|
|
platformVersion (str): fx. 6.1 (Build 7601)
|
|
|
|
presence (bool): True if online
|
|
|
|
product (str): Plex Media Server
|
|
|
|
productVersion (str): 1.3.3.3148-b38628e
|
|
|
|
provides (str): fx server
|
|
|
|
synced (bool): Description
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
BASEURL = 'https://plex.tv/api/resources?includeHttps=1'
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def __init__(self, data):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (Element): XML response as Element
|
|
|
|
"""
|
2014-12-29 03:21:58 +00:00
|
|
|
self.name = data.attrib.get('name')
|
2015-06-08 16:41:47 +00:00
|
|
|
self.accessToken = data.attrib.get('accessToken')
|
|
|
|
self.product = data.attrib.get('product')
|
|
|
|
self.productVersion = data.attrib.get('productVersion')
|
|
|
|
self.platform = data.attrib.get('platform')
|
|
|
|
self.platformVersion = data.attrib.get('platformVersion')
|
|
|
|
self.device = data.attrib.get('device')
|
|
|
|
self.clientIdentifier = data.attrib.get('clientIdentifier')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.createdAt = utils.toDatetime(data.attrib.get('createdAt'))
|
|
|
|
self.lastSeenAt = utils.toDatetime(data.attrib.get('lastSeenAt'))
|
2015-06-08 16:41:47 +00:00
|
|
|
self.provides = data.attrib.get('provides')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.owned = utils.cast(bool, data.attrib.get('owned'))
|
|
|
|
self.home = utils.cast(bool, data.attrib.get('home'))
|
|
|
|
self.synced = utils.cast(bool, data.attrib.get('synced'))
|
|
|
|
self.presence = utils.cast(bool, data.attrib.get('presence'))
|
2017-01-02 21:06:40 +00:00
|
|
|
self.connections = [ResourceConnection(
|
|
|
|
elem) for elem in data if elem.tag == 'Connection']
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Pretty repr."""
|
2014-12-29 03:21:58 +00:00
|
|
|
return '<%s:%s>' % (self.__class__.__name__, self.name.encode('utf8'))
|
|
|
|
|
2015-06-09 04:17:55 +00:00
|
|
|
def connect(self, ssl=None):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Connect.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ssl (None, optional): Use ssl.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
class: Plexserver
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
NotFound: Unable to connect to resource: name
|
|
|
|
"""
|
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
# Sort connections from (https, local) to (http, remote)
|
2015-06-08 16:41:47 +00:00
|
|
|
# Only check non-local connections unless we own the resource
|
2016-04-02 06:19:32 +00:00
|
|
|
forcelocal = lambda c: self.owned or c.local
|
2017-01-02 21:06:40 +00:00
|
|
|
connections = sorted(
|
|
|
|
self.connections, key=lambda c: c.local, reverse=True)
|
2016-04-02 06:19:32 +00:00
|
|
|
https = [c.uri for c in self.connections if forcelocal(c)]
|
|
|
|
http = [c.httpuri for c in self.connections if forcelocal(c)]
|
|
|
|
connections = https + http
|
2015-06-08 16:41:47 +00:00
|
|
|
# Try connecting to all known resource connections in parellel, but
|
2014-12-29 03:21:58 +00:00
|
|
|
# only return the first server (in order) that provides a response.
|
2016-04-02 06:19:32 +00:00
|
|
|
listargs = [[c] for c in connections]
|
|
|
|
results = utils.threaded(self._connect, listargs)
|
|
|
|
# At this point we have a list of result tuples containing (url, token, PlexServer)
|
2017-01-02 21:06:40 +00:00
|
|
|
# or (url, token, None) in the case a connection could not be
|
|
|
|
# established.
|
2016-04-02 06:19:32 +00:00
|
|
|
for url, token, result in results:
|
|
|
|
okerr = 'OK' if result else 'ERR'
|
2017-01-02 21:06:40 +00:00
|
|
|
log.info(
|
|
|
|
'Testing resource connection: %s?X-Plex-Token=%s %s', url, token, okerr)
|
|
|
|
|
|
|
|
results = [r[2] for r in results if r and r is not None]
|
2015-06-08 16:41:47 +00:00
|
|
|
if not results:
|
|
|
|
raise NotFound('Unable to connect to resource: %s' % self.name)
|
2017-01-02 21:06:40 +00:00
|
|
|
log.info('Connecting to server: %s?X-Plex-Token=%s',
|
|
|
|
results[0].baseurl, results[0].token)
|
|
|
|
|
2015-06-08 16:41:47 +00:00
|
|
|
return results[0]
|
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def _connect(self, url, results, i):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Connect.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url (str): url to the resource
|
|
|
|
results (TYPE): Description
|
|
|
|
i (TYPE): Description
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TYPE: Description
|
|
|
|
"""
|
2014-12-29 03:21:58 +00:00
|
|
|
try:
|
2017-01-02 21:06:40 +00:00
|
|
|
results[i] = (url, self.accessToken,
|
|
|
|
PlexServer(url, self.accessToken))
|
2014-12-29 03:21:58 +00:00
|
|
|
except NotFound:
|
2016-04-02 06:19:32 +00:00
|
|
|
results[i] = (url, self.accessToken, None)
|
2015-06-08 16:41:47 +00:00
|
|
|
|
|
|
|
|
2015-11-05 03:57:08 +00:00
|
|
|
class ResourceConnection(object):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""ResourceConnection.
|
2015-06-08 16:41:47 +00:00
|
|
|
|
2017-01-02 21:06:40 +00:00
|
|
|
Attributes:
|
|
|
|
address (str): Local ip adress
|
|
|
|
httpuri (str): Full local address
|
|
|
|
local (bool): True if local
|
|
|
|
port (int): 32400
|
|
|
|
protocol (str): http or https
|
|
|
|
uri (str): External adress
|
|
|
|
"""
|
2015-06-08 16:41:47 +00:00
|
|
|
def __init__(self, data):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Set attrs.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (Element): XML response as Element from PMS.
|
|
|
|
"""
|
2015-06-08 16:41:47 +00:00
|
|
|
self.protocol = data.attrib.get('protocol')
|
|
|
|
self.address = data.attrib.get('address')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.port = utils.cast(int, data.attrib.get('port'))
|
2015-06-08 16:41:47 +00:00
|
|
|
self.uri = data.attrib.get('uri')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.local = utils.cast(bool, data.attrib.get('local'))
|
2016-04-02 06:19:32 +00:00
|
|
|
self.httpuri = 'http://%s:%s' % (self.address, self.port)
|
2015-06-09 04:17:55 +00:00
|
|
|
|
2015-06-08 16:41:47 +00:00
|
|
|
def __repr__(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Pretty repr."""
|
2015-06-08 16:41:47 +00:00
|
|
|
return '<%s:%s>' % (self.__class__.__name__, self.uri.encode('utf8'))
|
2015-02-20 20:06:41 +00:00
|
|
|
|
|
|
|
|
2015-11-05 03:57:08 +00:00
|
|
|
class MyPlexDevice(object):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Device connected.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
BASEURL (str): Plex.tv XML device url
|
|
|
|
clientIdentifier (str): 0x685d43d...
|
|
|
|
connections (list):
|
|
|
|
device (str): fx Windows
|
|
|
|
id (str): 123
|
|
|
|
model (str):
|
|
|
|
name (str): fx Computername
|
|
|
|
platform (str): Windows
|
|
|
|
platformVersion (str): Fx 8
|
|
|
|
product (str): Fx PlexAPI
|
|
|
|
productVersion (string): 2.0.2
|
|
|
|
provides (str): fx controller
|
|
|
|
publicAddress (str): Public ip address
|
|
|
|
screenDensity (str): Description
|
|
|
|
screenResolution (str): Description
|
|
|
|
token (str): Auth token
|
|
|
|
vendor (str): Description
|
|
|
|
version (str): fx 2.0.2
|
|
|
|
"""
|
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
BASEURL = 'https://plex.tv/devices.xml'
|
2015-09-29 18:14:30 +00:00
|
|
|
|
|
|
|
def __init__(self, data):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Set attrs
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (Element): XML response as Element from PMS
|
|
|
|
"""
|
2015-09-29 18:14:30 +00:00
|
|
|
self.name = data.attrib.get('name')
|
|
|
|
self.publicAddress = data.attrib.get('publicAddress')
|
|
|
|
self.product = data.attrib.get('product')
|
|
|
|
self.productVersion = data.attrib.get('productVersion')
|
|
|
|
self.platform = data.attrib.get('platform')
|
|
|
|
self.platformVersion = data.attrib.get('platformVersion')
|
|
|
|
self.device = data.attrib.get('device')
|
|
|
|
self.model = data.attrib.get('model')
|
|
|
|
self.vendor = data.attrib.get('vendor')
|
2016-04-07 05:39:04 +00:00
|
|
|
self.provides = data.attrib.get('provides')
|
2015-09-29 18:14:30 +00:00
|
|
|
self.clientIdentifier = data.attrib.get('clientIdentifier')
|
|
|
|
self.version = data.attrib.get('version')
|
|
|
|
self.id = data.attrib.get('id')
|
|
|
|
self.token = data.attrib.get('token')
|
|
|
|
self.screenResolution = data.attrib.get('screenResolution')
|
|
|
|
self.screenDensity = data.attrib.get('screenDensity')
|
2017-01-02 21:06:40 +00:00
|
|
|
self.connections = [connection.attrib.get(
|
|
|
|
'uri') for connection in data.iter('Connection')]
|
2015-09-29 18:14:30 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Pretty repr."""
|
2015-09-29 18:14:30 +00:00
|
|
|
return '<%s:%s:%s>' % (self.__class__.__name__, self.name.encode('utf8'), self.product.encode('utf8'))
|
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def connect(self, ssl=None):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Connect to the first server.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ssl (None, optional): Use SSL?
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TYPE: Plexserver
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
NotFound: Unable to connect to resource: name
|
|
|
|
"""
|
2016-04-02 06:19:32 +00:00
|
|
|
# Try connecting to all known resource connections in parellel, but
|
|
|
|
# only return the first server (in order) that provides a response.
|
|
|
|
listargs = [[c] for c in self.connections]
|
|
|
|
results = utils.threaded(self._connect, listargs)
|
|
|
|
# At this point we have a list of result tuples containing (url, token, PlexServer)
|
2017-01-02 21:06:40 +00:00
|
|
|
# or (url, token, None) in the case a connection could not be
|
|
|
|
# established.
|
2016-04-02 06:19:32 +00:00
|
|
|
for url, token, result in results:
|
|
|
|
okerr = 'OK' if result else 'ERR'
|
2017-01-02 21:06:40 +00:00
|
|
|
log.info('Testing device connection: %s?X-Plex-Token=%s %s',
|
|
|
|
url, token, okerr)
|
|
|
|
results = [r[2] for r in results if r and r[2] is not None]
|
2016-04-02 06:19:32 +00:00
|
|
|
if not results:
|
|
|
|
raise NotFound('Unable to connect to resource: %s' % self.name)
|
2017-01-02 21:06:40 +00:00
|
|
|
log.info('Connecting to server: %s?X-Plex-Token=%s',
|
|
|
|
results[0].baseurl, results[0].token)
|
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
return results[0]
|
2015-09-29 18:14:30 +00:00
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def _connect(self, url, results, i):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url (TYPE): Description
|
|
|
|
results (TYPE): Description
|
|
|
|
i (TYPE): Description
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TYPE: Description
|
|
|
|
"""
|
2016-04-02 06:19:32 +00:00
|
|
|
try:
|
|
|
|
results[i] = (url, self.token, PlexClient(url, self.token))
|
|
|
|
except NotFound as err:
|
|
|
|
results[i] = (url, self.token, None)
|
|
|
|
|
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def _findItem(items, value, attrs=None):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Simple helper to find something using attrs
|
|
|
|
|
|
|
|
Args:
|
|
|
|
items (cls): list of Object to get the attrs from
|
|
|
|
value (str): value to match against
|
|
|
|
attrs (None, optional): attr to match against value.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TYPE: Description
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
NotFound: Description
|
|
|
|
"""
|
2016-04-07 05:39:04 +00:00
|
|
|
attrs = attrs or ['name']
|
2016-04-02 06:19:32 +00:00
|
|
|
for item in items:
|
2016-04-07 05:39:04 +00:00
|
|
|
for attr in attrs:
|
|
|
|
if value.lower() == getattr(item, attr).lower():
|
|
|
|
return item
|
|
|
|
raise NotFound('Unable to find item %s' % value)
|
2016-04-02 06:19:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _listItems(url, token, cls):
|
2017-01-02 21:06:40 +00:00
|
|
|
"""Helper that builds list of classes from a XML response.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url (str): Description
|
|
|
|
token (str): Description
|
|
|
|
cls (class): Class to initate
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List: of classes
|
|
|
|
"""
|
2016-04-02 06:19:32 +00:00
|
|
|
headers = plexapi.BASE_HEADERS
|
|
|
|
headers['X-Plex-Token'] = token
|
|
|
|
log.info('GET %s?X-Plex-Token=%s', url, token)
|
|
|
|
response = requests.get(url, headers=headers, timeout=TIMEOUT)
|
|
|
|
data = ElementTree.fromstring(response.text.encode('utf8'))
|
|
|
|
return [cls(elem) for elem in data]
|