2016-03-21 04:26:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-02-06 04:52:10 +00:00
|
|
|
import requests
|
|
|
|
from requests.status_codes import _codes as codes
|
|
|
|
from plexapi import BASE_HEADERS, CONFIG, TIMEOUT
|
|
|
|
from plexapi import log, logfilter, utils
|
|
|
|
from plexapi.base import PlexObject
|
|
|
|
from plexapi.exceptions import BadRequest, NotFound
|
2016-04-02 06:19:32 +00:00
|
|
|
from plexapi.client import PlexClient
|
2017-01-22 04:06:55 +00:00
|
|
|
from plexapi.compat import ElementTree
|
2016-04-02 06:19:32 +00:00
|
|
|
from plexapi.server import PlexServer
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
class MyPlexAccount(PlexObject):
|
2017-01-10 06:54:19 +00:00
|
|
|
""" MyPlex account and profile information. The easiest way to build
|
2017-01-22 06:37:39 +00:00
|
|
|
this object is by calling the staticmethod :func:`~plexapi.myplex.MyPlexAccount.signin`
|
2017-01-09 06:35:29 +00:00
|
|
|
with your username and password. This object represents the data found Account on
|
|
|
|
the myplex.tv servers at the url https://plex.tv/users/account.
|
|
|
|
|
|
|
|
Attributes:
|
2017-01-10 06:54:19 +00:00
|
|
|
authenticationToken (str): <Unknown>
|
|
|
|
certificateVersion (str): <Unknown>
|
|
|
|
cloudSyncDevice (str):
|
|
|
|
email (str): Your current Plex email address.
|
|
|
|
entitlements (List<str>): List of devices your allowed to use with this account.
|
|
|
|
guest (bool): <Unknown>
|
|
|
|
home (bool): <Unknown>
|
|
|
|
homeSize (int): <Unknown>
|
|
|
|
id (str): Your Plex account ID.
|
|
|
|
locale (str): Your Plex locale
|
|
|
|
mailing_list_status (str): Your current mailing list status.
|
|
|
|
maxHomeSize (int): <Unknown>
|
|
|
|
queueEmail (str): Email address to add items to your `Watch Later` queue.
|
|
|
|
queueUid (str): <Unknown>
|
|
|
|
restricted (bool): <Unknown>
|
|
|
|
roles: (List<str>) Lit of account roles. Plexpass membership listed here.
|
|
|
|
scrobbleTypes (str): Description
|
|
|
|
secure (bool): Description
|
|
|
|
subscriptionActive (bool): True if your subsctiption is active.
|
|
|
|
subscriptionFeatures: (List<str>) List of features allowed on your subscription.
|
|
|
|
subscriptionPlan (str): Name of subscription plan.
|
|
|
|
subscriptionStatus (str): String representation of `subscriptionActive`.
|
|
|
|
thumb (str): URL of your account thumbnail.
|
|
|
|
title (str): <Unknown> - Looks like an alias for `username`.
|
|
|
|
username (str): Your account username.
|
|
|
|
uuid (str): <Unknown>
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2016-04-04 02:46:25 +00:00
|
|
|
SIGNIN = 'https://my.plexapp.com/users/sign_in.xml'
|
2017-02-06 04:52:10 +00:00
|
|
|
key = 'https://plex.tv/users/account'
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def __init__(self, username=None, password=None, session=None):
|
2017-01-09 14:21:54 +00:00
|
|
|
self._session = session or requests.Session()
|
2017-02-06 04:52:10 +00:00
|
|
|
self._token = None
|
|
|
|
username = username or CONFIG.get('authentication.myplex_username')
|
|
|
|
password = password or CONFIG.get('authentication.myplex_password')
|
2017-02-09 04:29:17 +00:00
|
|
|
data = self.query(self.SIGNIN, method=self._session.post, auth=(username, password))
|
2017-02-06 04:52:10 +00:00
|
|
|
super(MyPlexAccount, self).__init__(self, data, self.SIGNIN)
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
|
|
|
self._data = data
|
|
|
|
self._token = data.attrib.get('authenticationToken')
|
|
|
|
if self._token:
|
|
|
|
logfilter.add_secret(self._token)
|
|
|
|
self.authenticationToken = self._token
|
2016-04-07 05:39:04 +00:00
|
|
|
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-02-02 03:53:05 +00:00
|
|
|
# TODO: Fetch missing MyPlexAccount attributes
|
2017-01-22 05:19:52 +00:00
|
|
|
self.subscriptionActive = None # renamed on server
|
|
|
|
self.subscriptionStatus = None # renamed on server
|
|
|
|
self.subscriptionPlan = None # renmaed on server
|
|
|
|
self.subscriptionFeatures = None # renamed on server
|
2016-04-04 02:46:25 +00:00
|
|
|
self.roles = None
|
|
|
|
self.entitlements = None
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def device(self, name):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Returns the :class:`~plexapi.myplex.MyPlexDevice` that matches the name specified.
|
|
|
|
|
|
|
|
Parameters:
|
2017-01-22 04:06:55 +00:00
|
|
|
name (str): Name to match against.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-06 04:52:10 +00:00
|
|
|
for device in self.devices():
|
|
|
|
if device.name.lower() == name.lower():
|
|
|
|
return device
|
|
|
|
raise NotFound('Unable to find device %s' % name)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2017-01-22 04:06:55 +00:00
|
|
|
def devices(self):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Returns a list of all :class:`~plexapi.myplex.MyPlexDevice` objects connected to the server. """
|
2017-02-09 04:29:17 +00:00
|
|
|
data = self.query(MyPlexDevice.key)
|
2017-02-06 04:52:10 +00:00
|
|
|
return [MyPlexDevice(self, elem) for elem in data]
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2017-02-09 04:29:17 +00:00
|
|
|
def query(self, url, method=None, headers=None, **kwargs):
|
|
|
|
method = method or self._session.get
|
|
|
|
delim = '&' if '?' in url else '?'
|
|
|
|
url = '%s%sX-Plex-Token=%s' % (url, delim, self._token)
|
|
|
|
log.debug('%s %s', method.__name__.upper(), url)
|
|
|
|
allheaders = BASE_HEADERS.copy()
|
|
|
|
allheaders.update(headers or {})
|
|
|
|
response = method(url, headers=allheaders, timeout=TIMEOUT, **kwargs)
|
|
|
|
if response.status_code not in (200, 201):
|
|
|
|
codename = codes.get(response.status_code)[0]
|
|
|
|
log.warn('BadRequest (%s) %s %s' % (response.status_code, codename, response.url))
|
|
|
|
raise BadRequest('(%s) %s %s' % (response.status_code, codename, response.url))
|
|
|
|
text = response.text.encode('utf8')
|
|
|
|
return ElementTree.fromstring(text) if text else None
|
|
|
|
|
2016-04-02 06:19:32 +00:00
|
|
|
def resource(self, name):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Returns the :class:`~plexapi.myplex.MyPlexResource` that matches the name specified.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-01-22 05:19:52 +00:00
|
|
|
Parameters:
|
|
|
|
name (str): Name to match against.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-06 04:52:10 +00:00
|
|
|
for resource in self.resources():
|
|
|
|
if resource.name.lower() == name.lower():
|
|
|
|
return resource
|
|
|
|
raise NotFound('Unable to find resource %s' % name)
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def resources(self):
|
|
|
|
""" Returns a list of all :class:`~plexapi.myplex.MyPlexResource` objects connected to the server. """
|
2017-02-09 04:29:17 +00:00
|
|
|
data = self.query(MyPlexResource.key)
|
2017-02-06 04:52:10 +00:00
|
|
|
return [MyPlexResource(self, elem) for elem in data]
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-04-07 05:39:04 +00:00
|
|
|
def user(self, email):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Returns the :class:`~myplex.MyPlexUser` that matches the email or username specified.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-01-22 05:19:52 +00:00
|
|
|
Parameters:
|
|
|
|
email (str): Username or email to match against.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-06 04:52:10 +00:00
|
|
|
for user in self.users():
|
|
|
|
if email.lower() in (user.username.lower(), user.email.lower()):
|
|
|
|
return user
|
|
|
|
raise NotFound('Unable to find user %s' % email)
|
2015-09-29 18:14:30 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def users(self):
|
|
|
|
""" Returns a list of all :class:`~plexapi.myplex.MyPlexUser` objects connected to your account. """
|
2017-02-09 04:29:17 +00:00
|
|
|
data = self.query(MyPlexUser.key)
|
2017-02-06 04:52:10 +00:00
|
|
|
return [MyPlexUser(self, elem) for elem in data]
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2017-02-06 04:52:10 +00:00
|
|
|
class MyPlexUser(PlexObject):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" This object represents non-signed in users such as friends and linked
|
|
|
|
accounts. NOTE: This should not be confused with the :class:`~myplex.MyPlexAccount`
|
|
|
|
which is your specific account. The raw xml for the data presented here
|
|
|
|
can be found at: https://plex.tv/api/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
|
|
|
|
email (str): User's email address (user@gmail.com)
|
|
|
|
filterAll (str): Unknown
|
|
|
|
filterMovies (str): Unknown
|
|
|
|
filterMusic (str): Unknown
|
|
|
|
filterPhotos (str): Unknown
|
|
|
|
filterTelevision (str): Unknown
|
|
|
|
home (bool): Unknown
|
|
|
|
id (int): User's Plex account ID.
|
|
|
|
protected (False): Unknown (possibly SSL enabled?)
|
|
|
|
recommendationsPlaylistId (str): Unknown
|
|
|
|
restricted (str): Unknown
|
|
|
|
thumb (str): Link to the users avatar
|
|
|
|
title (str): Seems to be an aliad for username
|
|
|
|
username (str): User's username
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'User'
|
2017-02-06 04:52:10 +00:00
|
|
|
key = 'https://plex.tv/api/users/'
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def _loadData(self, data):
|
2017-02-04 08:08:47 +00:00
|
|
|
self._data = data
|
2017-01-22 05:19:52 +00:00
|
|
|
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-22 05:19:52 +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
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
#@utils.registerPlexObject
|
2017-02-06 04:52:10 +00:00
|
|
|
class MyPlexResource(PlexObject):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" This object represents resources connected to your Plex server that can provide
|
|
|
|
content such as Plex Media Servers, iPhone or Android clients, etc. The raw xml
|
|
|
|
for the data presented here can be found at: https://plex.tv/api/resources?includeHttps=1
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
accessToken (str): This resources accesstoken.
|
|
|
|
clientIdentifier (str): Unique ID for this resource.
|
|
|
|
connections (list): List of :class:`~myplex.ResourceConnection` objects
|
|
|
|
for this resource.
|
|
|
|
createdAt (datetime): Timestamp this resource first connected to your server.
|
|
|
|
device (str): Best guess on the type of device this is (PS, iPhone, Linux, etc).
|
|
|
|
home (bool): Unknown
|
|
|
|
lastSeenAt (datetime): Timestamp this resource last connected.
|
|
|
|
name (str): Descriptive name of this resource.
|
|
|
|
owned (bool): True if this resource is one of your own (you logged into it).
|
|
|
|
platform (str): OS the resource is running (Linux, Windows, Chrome, etc.)
|
|
|
|
platformVersion (str): Version of the platform.
|
|
|
|
presence (bool): True if the resource is online
|
|
|
|
product (str): Plex product (Plex Media Server, Plex for iOS, Plex Web, etc.)
|
|
|
|
productVersion (str): Version of the product.
|
|
|
|
provides (str): List of services this resource provides (client, server,
|
|
|
|
player, pubsub-player, etc.)
|
|
|
|
synced (bool): Unknown (possibly True if the resource has synced content?)
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Device'
|
2017-02-06 04:52:10 +00:00
|
|
|
key = 'https://plex.tv/api/resources?includeHttps=1'
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def _loadData(self, data):
|
2017-02-04 08:08:47 +00:00
|
|
|
self._data = data
|
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')
|
2017-01-26 06:33:01 +00:00
|
|
|
if self.accessToken:
|
|
|
|
logfilter.add_secret(self.accessToken)
|
2015-06-08 16:41:47 +00:00
|
|
|
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-02-13 02:55:55 +00:00
|
|
|
self.connections = self.findItems(data, ResourceConnection)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def connect(self, ssl=None, safe=False):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Returns a new :class:`~server.PlexServer` object. Often times there is more than
|
|
|
|
one address specified for a server or client. This function will prioritize local
|
|
|
|
connections before remote and HTTPS before HTTP. After trying to connect to all
|
|
|
|
available addresses for this resource and assuming at least one connection was
|
|
|
|
successful, the PlexServer object is built and returned.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
ssl (optional): Set True to only connect to HTTPS connections. Set False to
|
|
|
|
only connect to HTTP connections. Set None (default) to connect to any
|
|
|
|
HTTP or HTTPS connection.
|
2017-02-06 04:52:10 +00:00
|
|
|
safe (bool): If True this function will return None if unable to connect
|
|
|
|
instead of raising an exception (default False).
|
2017-01-22 05:19:52 +00:00
|
|
|
|
|
|
|
Raises:
|
2017-01-23 05:15:51 +00:00
|
|
|
:class:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this resource.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
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-22 05:19:52 +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)]
|
2017-01-22 05:19:52 +00:00
|
|
|
# Force ssl, no ssl, or any (default)
|
|
|
|
if ssl is True: connections = https
|
|
|
|
elif ssl is False: connections = http
|
|
|
|
else: 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-22 05:19:52 +00:00
|
|
|
log.info('Testing resource connection: %s?X-Plex-Token=%s %s', url, token, okerr)
|
2017-01-17 07:55:48 +00:00
|
|
|
results = [r[2] for r in results if r and r[2] is not None]
|
2015-06-08 16:41:47 +00:00
|
|
|
if not results:
|
2017-02-06 04:52:10 +00:00
|
|
|
if safe: return log.warn('Unable to connect to resource: %s' % self.name)
|
2015-06-08 16:41:47 +00:00
|
|
|
raise NotFound('Unable to connect to resource: %s' % self.name)
|
2017-02-06 04:52:10 +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):
|
2014-12-29 03:21:58 +00:00
|
|
|
try:
|
2017-01-22 05:19:52 +00:00
|
|
|
results[i] = (url, self.accessToken, PlexServer(url, self.accessToken))
|
2017-02-06 04:52:10 +00:00
|
|
|
except Exception as err:
|
|
|
|
log.error('%s: %s', url, err)
|
2016-04-02 06:19:32 +00:00
|
|
|
results[i] = (url, self.accessToken, None)
|
2015-06-08 16:41:47 +00:00
|
|
|
|
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2017-02-06 04:52:10 +00:00
|
|
|
class ResourceConnection(PlexObject):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Represents a Resource Connection object found within the
|
|
|
|
:class:`~myplex.MyPlexResource` objects.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
address (str): Local IP address
|
|
|
|
httpuri (str): Full local address
|
|
|
|
local (bool): True if local
|
|
|
|
port (int): 32400
|
|
|
|
protocol (str): HTTP or HTTPS
|
|
|
|
uri (str): External address
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Connection'
|
2017-02-06 04:52:10 +00:00
|
|
|
|
|
|
|
def _loadData(self, data):
|
2017-02-04 08:08:47 +00:00
|
|
|
self._data = data
|
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-02-20 20:06:41 +00:00
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
#@utils.registerPlexObject
|
2017-02-06 04:52:10 +00:00
|
|
|
class MyPlexDevice(PlexObject):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" This object represents resources connected to your Plex server that provide
|
|
|
|
playback ability from your Plex Server, iPhone or Android clients, Plex Web,
|
|
|
|
this API, etc. The raw xml for the data presented here can be found at:
|
|
|
|
https://plex.tv/devices.xml
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
clientIdentifier (str): Unique ID for this resource.
|
|
|
|
connections (list): List of connection URIs for the device.
|
|
|
|
device (str): Best guess on the type of device this is (Linux, iPad, AFTB, etc).
|
|
|
|
id (str): MyPlex ID of the device.
|
|
|
|
model (str): Model of the device (bueller, Linux, x86_64, etc.)
|
|
|
|
name (str): Hostname of the device.
|
|
|
|
platform (str): OS the resource is running (Linux, Windows, Chrome, etc.)
|
|
|
|
platformVersion (str): Version of the platform.
|
|
|
|
product (str): Plex product (Plex Media Server, Plex for iOS, Plex Web, etc.)
|
|
|
|
productVersion (string): Version of the product.
|
|
|
|
provides (str): List of services this resource provides (client, controller,
|
|
|
|
sync-target, player, pubsub-player).
|
|
|
|
publicAddress (str): Public IP address.
|
|
|
|
screenDensity (str): Unknown
|
|
|
|
screenResolution (str): Screen resolution (750x1334, 1242x2208, etc.)
|
|
|
|
token (str): Plex authentication token for the device.
|
|
|
|
vendor (str): Device vendor (ubuntu, etc).
|
|
|
|
version (str): Unknown (1, 2, 1.3.3.3148-b38628e, 1.3.15, etc.)
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Device'
|
2017-02-06 04:52:10 +00:00
|
|
|
key = 'https://plex.tv/devices.xml'
|
2015-09-29 18:14:30 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def _loadData(self, data):
|
2017-02-04 08:08:47 +00:00
|
|
|
self._data = data
|
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')
|
2017-01-26 06:33:01 +00:00
|
|
|
if self.token:
|
|
|
|
logfilter.add_secret(self.token)
|
2015-09-29 18:14:30 +00:00
|
|
|
self.screenResolution = data.attrib.get('screenResolution')
|
|
|
|
self.screenDensity = data.attrib.get('screenDensity')
|
2017-01-22 05:19:52 +00:00
|
|
|
self.connections = [connection.attrib.get('uri') for connection in data.iter('Connection')]
|
2015-09-29 18:14:30 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
def connect(self, safe=False):
|
2017-01-22 05:19:52 +00:00
|
|
|
""" Returns a new :class:`~plexapi.client.PlexClient` object. Sometimes there is more than
|
|
|
|
one address specified for a server or client. After trying to connect to all
|
2017-02-06 04:52:10 +00:00
|
|
|
available addresses for this client and assuming at least one connection was
|
2017-01-22 05:19:52 +00:00
|
|
|
successful, the PlexClient object is built and returned.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-02-06 04:52:10 +00:00
|
|
|
Parameters:
|
|
|
|
safe (bool): If True this function will return None if unable to connect
|
|
|
|
instead of raising an exception (default False).
|
|
|
|
|
2017-01-22 05:19:52 +00:00
|
|
|
Raises:
|
2017-01-23 05:15:51 +00:00
|
|
|
:class:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this device.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-06 04:52:10 +00:00
|
|
|
# Try connecting to all known clients in parellel, but
|
2016-04-02 06:19:32 +00:00
|
|
|
# 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-22 05:19:52 +00:00
|
|
|
log.info('Testing device connection: %s?X-Plex-Token=%s %s', url, token, okerr)
|
2017-01-02 21:06:40 +00:00
|
|
|
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:
|
2017-02-06 04:52:10 +00:00
|
|
|
if safe: return log.warn('Unable to connect to client: %s' % self.name)
|
|
|
|
raise NotFound('Unable to connect to client: %s' % self.name)
|
|
|
|
log.info('Connecting to client: %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):
|
|
|
|
try:
|
|
|
|
results[i] = (url, self.token, PlexClient(url, self.token))
|
2017-02-06 04:52:10 +00:00
|
|
|
except Exception as err:
|
|
|
|
log.error('%s: %s', url, err)
|
2016-04-02 06:19:32 +00:00
|
|
|
results[i] = (url, self.token, None)
|