This commit is contained in:
croneter 2018-02-03 13:44:16 +01:00
parent bd85bb445e
commit a2193ab01f

View file

@ -5,7 +5,7 @@ from logging import getLogger
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import requests import requests
from utils import settings, window, language as lang, dialog from utils import window, language as lang, dialog
import clientinfo as client import clientinfo as client
import state import state
@ -16,7 +16,7 @@ import state
import requests.packages.urllib3 import requests.packages.urllib3
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
log = getLogger("PLEX."+__name__) LOG = getLogger("PLEX." + __name__)
############################################################################### ###############################################################################
@ -46,7 +46,7 @@ class DownloadUtils():
Reserved for userclient only Reserved for userclient only
""" """
self.server = server self.server = server
log.debug("Set server: %s" % server) LOG.debug("Set server: %s", server)
def setToken(self, token): def setToken(self, token):
""" """
@ -54,9 +54,9 @@ class DownloadUtils():
""" """
self.token = token self.token = token
if token == '': if token == '':
log.debug('Set token: empty token!') LOG.debug('Set token: empty token!')
else: else:
log.debug("Set token: xxxxxxx") LOG.debug("Set token: xxxxxxx")
def setSSL(self, verifySSL=None, certificate=None): def setSSL(self, verifySSL=None, certificate=None):
""" """
@ -74,8 +74,8 @@ class DownloadUtils():
self.s.verify = verifySSL self.s.verify = verifySSL
if certificate: if certificate:
self.s.cert = certificate self.s.cert = certificate
log.debug("Verify SSL certificates set to: %s", verifySSL) LOG.debug("Verify SSL certificates set to: %s", verifySSL)
log.debug("SSL client side certificate set to: %s", certificate) LOG.debug("SSL client side certificate set to: %s", certificate)
def startSession(self, reset=False): def startSession(self, reset=False):
""" """
@ -107,18 +107,18 @@ class DownloadUtils():
self.s.mount("http://", requests.adapters.HTTPAdapter(max_retries=1)) self.s.mount("http://", requests.adapters.HTTPAdapter(max_retries=1))
self.s.mount("https://", requests.adapters.HTTPAdapter(max_retries=1)) self.s.mount("https://", requests.adapters.HTTPAdapter(max_retries=1))
log.info("Requests session started on: %s" % self.server) LOG.info("Requests session started on: %s", self.server)
def stopSession(self): def stopSession(self):
try: try:
self.s.close() self.s.close()
except: except:
log.info("Requests session already closed") LOG.info("Requests session already closed")
try: try:
del self.s del self.s
except: except:
pass pass
log.info('Request session stopped') LOG.info('Request session stopped')
def getHeader(self, options=None): def getHeader(self, options=None):
header = client.getXArgsDeviceInfo() header = client.getXArgsDeviceInfo()
@ -164,7 +164,7 @@ class DownloadUtils():
try: try:
s = self.s s = self.s
except AttributeError: except AttributeError:
log.info("Request session does not exist: start one") LOG.info("Request session does not exist: start one")
self.startSession() self.startSession()
s = self.s s = self.s
# Replace for the real values # Replace for the real values
@ -201,38 +201,38 @@ class DownloadUtils():
# THE EXCEPTIONS # THE EXCEPTIONS
except requests.exceptions.SSLError as e: except requests.exceptions.SSLError as e:
log.warn("Invalid SSL certificate for: %s" % url) LOG.warn("Invalid SSL certificate for: %s", url)
log.warn(e) LOG.warn(e)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
# Connection error # Connection error
log.warn("Server unreachable at: %s" % url) LOG.warn("Server unreachable at: %s", url)
log.warn(e) LOG.warn(e)
except requests.exceptions.Timeout as e: except requests.exceptions.Timeout as e:
log.warn("Server timeout at: %s" % url) LOG.warn("Server timeout at: %s", url)
log.warn(e) LOG.warn(e)
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
log.warn('HTTP Error at %s' % url) LOG.warn('HTTP Error at %s', url)
log.warn(e) LOG.warn(e)
except requests.exceptions.TooManyRedirects as e: except requests.exceptions.TooManyRedirects as e:
log.warn("Too many redirects connecting to: %s" % url) LOG.warn("Too many redirects connecting to: %s", url)
log.warn(e) LOG.warn(e)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
log.warn("Unknown error connecting to: %s" % url) LOG.warn("Unknown error connecting to: %s", url)
log.warn(e) LOG.warn(e)
except SystemExit: except SystemExit:
log.info('SystemExit detected, aborting download') LOG.info('SystemExit detected, aborting download')
self.stopSession() self.stopSession()
except: except:
log.warn('Unknown error while downloading. Traceback:') LOG.warn('Unknown error while downloading. Traceback:')
import traceback import traceback
log.warn(traceback.format_exc()) LOG.warn(traceback.format_exc())
# THE RESPONSE ##### # THE RESPONSE #####
else: else:
@ -254,19 +254,19 @@ class DownloadUtils():
# Called when checking a connect - no need for rash action # Called when checking a connect - no need for rash action
return 401 return 401
r.encoding = 'utf-8' r.encoding = 'utf-8'
log.warn('HTTP error 401 from PMS %s' % url) LOG.warn('HTTP error 401 from PMS %s', url)
log.info(r.text) LOG.info(r.text)
if '401 Unauthorized' in r.text: if '401 Unauthorized' in r.text:
# Truly unauthorized # Truly unauthorized
window('countUnauthorized', window('countUnauthorized',
value=str(int(window('countUnauthorized')) + 1)) value=str(int(window('countUnauthorized')) + 1))
if (int(window('countUnauthorized')) >= if (int(window('countUnauthorized')) >=
self.unauthorizedAttempts): self.unauthorizedAttempts):
log.warn('We seem to be truly unauthorized for PMS' LOG.warn('We seem to be truly unauthorized for PMS'
' %s ' % url) ' %s ', url)
if state.PMS_STATUS not in ('401', 'Auth'): if state.PMS_STATUS not in ('401', 'Auth'):
# Tell userclient token has been revoked. # Tell userclient token has been revoked.
log.debug('Setting PMS server status to ' LOG.debug('Setting PMS server status to '
'unauthorized') 'unauthorized')
state.PMS_STATUS = '401' state.PMS_STATUS = '401'
window('plex_serverStatus', value="401") window('plex_serverStatus', value="401")
@ -276,7 +276,7 @@ class DownloadUtils():
icon='{error}') icon='{error}')
else: else:
# there might be other 401 where e.g. PMS under strain # there might be other 401 where e.g. PMS under strain
log.info('PMS might only be under strain') LOG.info('PMS might only be under strain')
return 401 return 401
elif r.status_code in (200, 201): elif r.status_code in (200, 201):
@ -304,18 +304,18 @@ class DownloadUtils():
# update # update
pass pass
else: else:
log.warn("Unable to convert the response for: " LOG.warn("Unable to convert the response for: "
"%s" % url) "%s", url)
log.warn("Received headers were: %s" % r.headers) LOG.warn("Received headers were: %s", r.headers)
log.warn('Received text: %s', r.text) LOG.warn('Received text: %s', r.text)
return True return True
elif r.status_code == 403: elif r.status_code == 403:
# E.g. deleting a PMS item # E.g. deleting a PMS item
log.warn('PMS sent 403: Forbidden error for url %s' % url) LOG.warn('PMS sent 403: Forbidden error for url %s', url)
return None return None
else: else:
r.encoding = 'utf-8' r.encoding = 'utf-8'
log.warn('Unknown answer from PMS %s with status code %s. ', LOG.warn('Unknown answer from PMS %s with status code %s. ',
url, r.status_code) url, r.status_code)
return True return True
@ -326,8 +326,8 @@ class DownloadUtils():
window('countError', window('countError',
value=str(int(window('countError')) + 1)) value=str(int(window('countError')) + 1))
if int(window('countError')) >= self.connectionAttempts: if int(window('countError')) >= self.connectionAttempts:
log.warn('Failed to connect to %s too many times. ' LOG.warn('Failed to connect to %s too many times. '
'Declare PMS dead' % url) 'Declare PMS dead', url)
window('plex_online', value="false") window('plex_online', value="false")
except: except:
# 'countError' not yet set # 'countError' not yet set