Merge conflicts downloadutils.py

This commit is contained in:
tomkat83 2016-08-29 19:57:58 +02:00
parent ccd3cac33a
commit bd2ffd0ea9

View file

@ -2,12 +2,13 @@
############################################################################### ###############################################################################
import logging
import requests import requests
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import xbmcgui import xbmcgui
from utils import logging, settings, window from utils import settings, window
import clientinfo import clientinfo
############################################################################### ###############################################################################
@ -16,10 +17,11 @@ import clientinfo
import requests.packages.urllib3 import requests.packages.urllib3
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
log = logging.getLogger("PLEX."+__name__)
############################################################################### ###############################################################################
@logging
class DownloadUtils(): class DownloadUtils():
""" """
Manages any up/downloads with PKC. Careful to initiate correctly Manages any up/downloads with PKC. Careful to initiate correctly
@ -45,21 +47,21 @@ class DownloadUtils():
Reserved for userclient only Reserved for userclient only
""" """
self.username = username self.username = username
self.logMsg("Set username: %s" % username, 0) log.debug("Set username: %s" % username)
def setUserId(self, userId): def setUserId(self, userId):
""" """
Reserved for userclient only Reserved for userclient only
""" """
self.userId = userId self.userId = userId
self.logMsg("Set userId: %s" % userId, 0) log.debug("Set userId: %s" % userId)
def setServer(self, server): def setServer(self, server):
""" """
Reserved for userclient only Reserved for userclient only
""" """
self.server = server self.server = server
self.logMsg("Set server: %s" % server, 0) log.debug("Set server: %s" % server)
def setToken(self, token): def setToken(self, token):
""" """
@ -67,9 +69,9 @@ class DownloadUtils():
""" """
self.token = token self.token = token
if token == '': if token == '':
self.logMsg('Set token: empty token!', 0) log.debug('Set token: empty token!')
else: else:
self.logMsg("Set token: xxxxxxx", 0) log.debug("Set token: xxxxxxx")
def setSSL(self, verifySSL=None, certificate=None): def setSSL(self, verifySSL=None, certificate=None):
""" """
@ -83,8 +85,8 @@ class DownloadUtils():
verifySSL = settings('sslverify') verifySSL = settings('sslverify')
if certificate is None: if certificate is None:
certificate = settings('sslcert') certificate = settings('sslcert')
self.logMsg("Verify SSL certificates set to: %s" % verifySSL, 0) log.debug("Verify SSL certificates set to: %s" % verifySSL)
self.logMsg("SSL client side certificate set to: %s" % certificate, 0) log.debug("SSL client side certificate set to: %s" % certificate)
if verifySSL != 'true': if verifySSL != 'true':
self.s.verify = False self.s.verify = False
if certificate != 'None': if certificate != 'None':
@ -123,18 +125,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))
self.logMsg("Requests session started on: %s" % self.server, 0) 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:
self.logMsg("Requests session already closed", 0) log.info("Requests session already closed")
try: try:
del self.s del self.s
except: except:
pass pass
self.logMsg('Request session stopped', 0) log.info('Request session stopped')
def getHeader(self, options=None): def getHeader(self, options=None):
header = clientinfo.ClientInfo().getXArgsDeviceInfo() header = clientinfo.ClientInfo().getXArgsDeviceInfo()
@ -142,7 +144,7 @@ class DownloadUtils():
header.update(options) header.update(options)
return header return header
def __doDownload(self, s, action_type, **kwargs): def _doDownload(self, s, action_type, **kwargs):
if action_type == "GET": if action_type == "GET":
r = s.get(**kwargs) r = s.get(**kwargs)
elif action_type == "POST": elif action_type == "POST":
@ -178,7 +180,7 @@ class DownloadUtils():
try: try:
s = self.s s = self.s
except AttributeError: except AttributeError:
self.logMsg("Request session does not exist: start one", 0) 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
@ -207,42 +209,42 @@ class DownloadUtils():
# ACTUAL DOWNLOAD HAPPENING HERE # ACTUAL DOWNLOAD HAPPENING HERE
try: try:
r = self.__doDownload(s, action_type, **kwargs) r = self._doDownload(s, action_type, **kwargs)
# THE EXCEPTIONS # THE EXCEPTIONS
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
# Connection error # Connection error
self.logMsg("Server unreachable at: %s" % url, -1) log.warn("Server unreachable at: %s" % url)
self.logMsg(e, 1) log.warn(e)
except requests.exceptions.ConnectTimeout as e: except requests.exceptions.ConnectTimeout as e:
self.logMsg("Server timeout at: %s" % url, -1) log.warn("Server timeout at: %s" % url)
self.logMsg(e, 1) log.warn(e)
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
self.logMsg('HTTP Error at %s' % url, -1) log.warn('HTTP Error at %s' % url)
self.logMsg(e, 1) log.warn(e)
except requests.exceptions.SSLError as e: except requests.exceptions.SSLError as e:
self.logMsg("Invalid SSL certificate for: %s" % url, -1) log.warn("Invalid SSL certificate for: %s" % url)
self.logMsg(e, 1) log.warn(e)
except requests.exceptions.TooManyRedirects as e: except requests.exceptions.TooManyRedirects as e:
self.logMsg("Too many redirects connecting to: %s" % url, -1) log.warn("Too many redirects connecting to: %s" % url)
self.logMsg(e, 1) log.warn(e)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
self.logMsg("Unknown error connecting to: %s" % url, -1) log.warn("Unknown error connecting to: %s" % url)
self.logMsg(e, 1) log.warn(e)
except SystemExit: except SystemExit:
self.logMsg('SystemExit detected, aborting download', 0) log.info('SystemExit detected, aborting download')
self.stopSession() self.stopSession()
except: except:
self.logMsg('Unknown error while downloading. Traceback:', -1) log.warn('Unknown error while downloading. Traceback:')
import traceback import traceback
self.logMsg(traceback.format_exc(), 0) log.warn(traceback.format_exc())
# THE RESPONSE ##### # THE RESPONSE #####
else: else:
@ -264,20 +266,20 @@ 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'
self.logMsg('HTTP error 401 from PMS %s' % url, -1) log.warn('HTTP error 401 from PMS %s' % url)
self.logMsg(r.text, 1) 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):
self.logMsg('We seem to be truly unauthorized for PMS' log.warn('We seem to be truly unauthorized for PMS'
' %s ' % url, -1) ' %s ' % url)
if window('plex_serverStatus') not in ('401', 'Auth'): if window('plex_serverStatus') not in ('401', 'Auth'):
# Tell userclient token has been revoked. # Tell userclient token has been revoked.
self.logMsg('Setting PMS server status to ' log.debug('Setting PMS server status to '
'unauthorized', 0) 'unauthorized')
window('plex_serverStatus', value="401") window('plex_serverStatus', value="401")
xbmcgui.Dialog().notification( xbmcgui.Dialog().notification(
self.addonName, self.addonName,
@ -285,7 +287,7 @@ class DownloadUtils():
xbmcgui.NOTIFICATION_ERROR) xbmcgui.NOTIFICATION_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
self.logMsg('PMS might only be under strain', 0) 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):
@ -310,18 +312,17 @@ class DownloadUtils():
# update # update
pass pass
else: else:
self.logMsg("Unable to convert the response for: " log.error("Unable to convert the response for: "
"%s" % url, -1) "%s" % url)
self.logMsg("Received headers were: %s" log.info("Received headers were: %s" % r.headers)
% r.headers, -1) log.info('Received text:')
self.logMsg('Received text:', -1) log.info(r.text)
self.logMsg(r.text, -1)
return True return True
else: else:
self.logMsg('Unknown answer from PMS %s with status code %s. ' log.error('Unknown answer from PMS %s with status code %s. '
'Message:' % (url, r.status_code), -1) 'Message:' % (url, r.status_code))
r.encoding = 'utf-8' r.encoding = 'utf-8'
self.logMsg(r.text, 1) log.info(r.text)
return True return True
# And now deal with the consequences of the exceptions # And now deal with the consequences of the exceptions
@ -331,8 +332,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:
self.logMsg('Failed to connect to %s too many times. ' log.warn('Failed to connect to %s too many times. '
'Declare PMS dead' % url, -1) 'Declare PMS dead' % url)
window('plex_online', value="false") window('plex_online', value="false")
except: except:
# 'countError' not yet set # 'countError' not yet set