Include plex token on all urls

This commit is contained in:
Michael Shepanski 2015-02-23 22:42:29 -05:00
parent 0489efdb1f
commit edb29fa0d7
6 changed files with 25 additions and 11 deletions

View file

@ -30,7 +30,7 @@ BASE_HEADERS = {
# Logging Configuration
log = logging.getLogger('plexapi')
logfile = os.path.join('/tmp', 'plexapi.log')
logformat = logging.Formatter('%(asctime)s %(module)-12s %(levelname)-6s %(message)s')
logformat = logging.Formatter('%(asctime)s %(module)12s:%(lineno)-4s %(levelname)-9s %(message)s')
filehandler = RotatingFileHandler(logfile, 'a', 512000, 3)
filehandler.setFormatter(logformat)
log.addHandler(filehandler)

View file

@ -13,3 +13,6 @@ class UnknownType(Exception):
class Unsupported(Exception):
pass
class Unauthorized(Exception):
pass

View file

@ -3,7 +3,7 @@ PlexAPI MyPlex
"""
import plexapi, requests
from plexapi import TIMEOUT, log
from plexapi.exceptions import BadRequest, NotFound
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
from plexapi.utils import addrToIP, cast, toDatetime
from requests.status_codes import _codes as codes
from threading import Thread
@ -39,11 +39,15 @@ class MyPlexUser:
@classmethod
def signin(cls, username, password):
if 'X-Plex-Token' in plexapi.BASE_HEADERS:
del plexapi.BASE_HEADERS['X-Plex-Token']
auth = (username, password)
log.info('POST %s', cls.SIGNIN)
response = requests.post(cls.SIGNIN, headers=plexapi.BASE_HEADERS, auth=auth, timeout=TIMEOUT)
if response.status_code != requests.codes.created:
codename = codes.get(response.status_code)[0]
if response.status_code == 401:
raise Unauthorized('(%s) %s' % (response.status_code, codename))
raise BadRequest('(%s) %s' % (response.status_code, codename))
data = ElementTree.fromstring(response.text.encode('utf8'))
return cls(data)

View file

@ -81,9 +81,10 @@ class PlexServer(object):
return headers
def query(self, path, method=requests.get):
global TOTAL_QUERIES; TOTAL_QUERIES += 1
global TOTAL_QUERIES
TOTAL_QUERIES += 1
url = self.url(path)
log.info('%s %s%s', method.__name__.upper(), url, '?X-Plex-Token=%s' % self.token if self.token else '')
log.info('%s %s', method.__name__.upper(), url)
response = method(url, headers=self.headers(), timeout=TIMEOUT)
if response.status_code not in [200, 201]:
codename = codes.get(response.status_code)[0]
@ -102,4 +103,6 @@ class PlexServer(object):
return video.list_items(self, '/status/sessions')
def url(self, path):
return 'http://%s:%s/%s' % (self.address, self.port, path.lstrip('/'))
url = 'http://%s:%s/%s' % (self.address, self.port, path.lstrip('/'))
if self.token: url += '?X-Plex-Token=%s' % self.token
return url

View file

@ -50,13 +50,9 @@ def addrToIP(addr):
return addr
except socket.error:
pass
# Remove any leading http crap
if addr.startswith('http://'):
addr = addr[7:]
if addr.startswith('https://'):
addr = addr[8:]
# Try getting the IP
try:
addr = addr.replace('http://', '')
return str(socket.gethostbyname(addr))
except socket.error:
return addr

View file

@ -44,6 +44,10 @@ class Video(PlexPartialObject):
self.actors = [Actor(self.server, elem) for elem in data if elem.tag == Actor.TYPE]
self.writers = [Writer(self.server, elem) for elem in data if elem.tag == Writer.TYPE]
@property
def thumbUrl(self):
return self.server.url(self.thumb)
def _find_user(self, data):
elem = data.find('User')
if elem is not None:
@ -101,7 +105,7 @@ class Movie(Video):
class Show(Video):
TYPE = 'show'
def _loadData(self, data):
super(Show, self)._loadData(data)
self.studio = data.attrib.get('studio', NA)
@ -191,6 +195,10 @@ class Episode(Video):
self.duration = cast(int, data.attrib.get('duration', NA))
self.originallyAvailableAt = toDatetime(data.attrib.get('originallyAvailableAt', NA), '%Y-%m-%d')
@property
def thumbUrl(self):
return self.server.url(self.grandparentThumb)
def season(self):
return list_items(self.server, self.parentKey)[0]