Only declare unauthorized after 3 fails in a row

This commit is contained in:
tomkat83 2016-04-13 12:14:16 +02:00
parent 47938cf5c2
commit dc96dc533a

View file

@ -32,6 +32,10 @@ class DownloadUtils():
# Requests session # Requests session
timeout = 30 timeout = 30
# How many failed attempts before declaring PMS dead?
connectionAttempts = 3
# How many 401 returns before declaring unauthorized?
unauthorizedAttempts = 3
def __init__(self): def __init__(self):
self.__dict__ = self._shared_state self.__dict__ = self._shared_state
@ -108,6 +112,10 @@ class DownloadUtils():
self.setUserId(window('currUserId')) self.setUserId(window('currUserId'))
self.setUsername(window('plex_username')) self.setUsername(window('plex_username'))
# Counters to declare PMS dead or unauthorized
self.countUnauthorized = 0
self.countError = 0
# Retry connections to the server # Retry connections to the server
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))
@ -118,7 +126,7 @@ class DownloadUtils():
try: try:
self.s.close() self.s.close()
except: except:
self.logMsg("Requests session could not be terminated.", 0) self.logMsg("Requests session already closed", 0)
try: try:
del self.s del self.s
except: except:
@ -161,7 +169,7 @@ class DownloadUtils():
JSON json() object, if applicable JSON json() object, if applicable
""" """
kwargs = {} kwargs = {}
if authenticate: if authenticate is True:
# Get requests session # Get requests session
try: try:
s = self.s s = self.s
@ -199,61 +207,46 @@ class DownloadUtils():
# THE EXCEPTIONS # THE EXCEPTIONS
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
# Connection error # Connection error
if authenticate is False:
self.logMsg("Server unreachable at: %s" % url, -1) self.logMsg("Server unreachable at: %s" % url, -1)
self.logMsg(e, 2) self.logMsg(e, 1)
# Make the addon aware of status
window('emby_online', value="false")
return False
except requests.exceptions.ConnectTimeout as e: except requests.exceptions.ConnectTimeout as e:
self.logMsg("Server timeout at: %s" % url, -1) self.logMsg("Server timeout at: %s" % url, -1)
self.logMsg(e, 2) self.logMsg(e, 1)
return False
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
r = r.status_code self.logMsg('HTTP Error at %s' % url, -1)
if r == 401: self.logMsg(e, 1)
# Unauthorized
self.logMsg('Error 401 contacting %s' % url, -1)
elif r in (301, 302):
# Redirects
self.logMsg('HTTP redirect error %s at %s' % (r, url), -1)
elif r == 400:
# Bad requests
self.logMsg('Bad request at %s' % url, -1)
else:
self.logMsg('HTTP Error %s at %s' % (r, url), -1)
self.logMsg(e, 2)
return r
except requests.exceptions.SSLError as e: except requests.exceptions.SSLError as e:
self.logMsg("Invalid SSL certificate for: %s" % url, -1) self.logMsg("Invalid SSL certificate for: %s" % url, -1)
self.logMsg(e, 2) self.logMsg(e, 1)
return False
except requests.exceptions.TooManyRedirects as e: except requests.exceptions.TooManyRedirects as e:
self.logMsg("Too many redirects connecting to: %s" % url, -1) self.logMsg("Too many redirects connecting to: %s" % url, -1)
self.logMsg(e, 2) self.logMsg(e, 1)
return False
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
self.logMsg("Unknown error connecting to: %s" % url, -1) self.logMsg("Unknown error connecting to: %s" % url, -1)
self.logMsg("Error message: %s" % e, 2) self.logMsg(e, 1)
return False
except SystemExit: except SystemExit:
self.logMsg('SystemExit detected, aborting download', 0) self.logMsg('SystemExit detected, aborting download', 0)
self.stopSession() self.stopSession()
return False
except: except:
self.logMsg('Unknown requests error', -1) self.logMsg('Unknown error while downloading. Traceback:', -1)
import traceback import traceback
self.logMsg(traceback.format_exc(), 0) self.logMsg(traceback.format_exc(), 0)
return False
# THE RESPONSE ##### # THE RESPONSE #####
else:
# We COULD contact the PMS, hence it ain't dead
if authenticate is True:
self.countError = 0
if r.status_code != 401:
self.countUnauthorized = 0
if r.status_code == 204: if r.status_code == 204:
# No body in the response # No body in the response
return True return True
@ -263,22 +256,25 @@ 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. Message received:', -1) self.logMsg('HTTP error 401 from PMS', -1)
self.logMsg(r.text, -1) self.logMsg(r.text, 1)
if '401 Unauthorized' in r.text: if '401 Unauthorized' in r.text:
# Truly unauthorized # Truly unauthorized
self.logMsg('We seem to be truely unauthorized', 0) self.countUnauthorized += 1
if self.countUnauthorized >= self.unauthorizedAttempts:
self.logMsg('We seem to be truly unauthorized for PMS'
% url, -1)
if window('emby_serverStatus') not in ('401', 'Auth'): if window('emby_serverStatus') not in ('401', 'Auth'):
# Tell userclient token has been revoked. # Tell userclient token has been revoked.
self.logMsg('Setting emby_serverStatus to 401', 0) self.logMsg('Setting PMS server status to '
'unauthorized', 0)
window('emby_serverStatus', value="401") window('emby_serverStatus', value="401")
xbmcgui.Dialog().notification( xbmcgui.Dialog().notification(
self.addonName, self.addonName,
"Error connecting: Unauthorized.", "Unauthorized for PMS",
xbmcgui.NOTIFICATION_ERROR) xbmcgui.NOTIFICATION_ERROR)
return 401
else: else:
# there might be other 401 where e.g. PMS is under strain (tbv) # there might be other 401 where e.g. PMS under strain
self.logMsg('PMS might only be under strain', 0) self.logMsg('PMS might only be under strain', 0)
return 401 return 401
@ -292,20 +288,20 @@ class DownloadUtils():
except: except:
r.encoding = 'utf-8' r.encoding = 'utf-8'
if r.text == '': if r.text == '':
# Answer does not contain a body (even though it should) # Answer does not contain a body
return True return True
try: try:
# UNICODE - JSON object # UNICODE - JSON object
r = r.json() r = r.json()
return r return r
except: except:
r.encoding = 'utf-8'
if '200 OK' in r.text: if '200 OK' in r.text:
# Received fucked up OK from PMS on playstate update # Received fucked up OK from PMS on playstate
# update
pass pass
else: else:
self.logMsg("Unable to convert the response for: %s" self.logMsg("Unable to convert the response for: "
% url, -1) "%s" % url, -1)
self.logMsg("Received headers were: %s" self.logMsg("Received headers were: %s"
% r.headers, -1) % r.headers, -1)
self.logMsg('Received text:', -1) self.logMsg('Received text:', -1)
@ -315,5 +311,15 @@ class DownloadUtils():
self.logMsg('Unknown answer from PMS %s with status code %s. ' self.logMsg('Unknown answer from PMS %s with status code %s. '
'Message:' % (url, r.status_code), -1) 'Message:' % (url, r.status_code), -1)
r.encoding = 'utf-8' r.encoding = 'utf-8'
self.logMsg(r.text, -1) self.logMsg(r.text, 1)
return True return True
# And now deal with the consequences of the exceptions
if authenticate is True:
# Make the addon aware of status
self.countError += 1
if self.countError >= self.connectionAttempts:
self.logMsg('Failed to connect to %s too many times. Declare '
'PMS dead' % url, -1)
window('emby_online', value="false")
return False