From da7685b03f7ab1064032f405f5a03e1ffd3936d3 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sat, 16 Jul 2016 10:02:50 -0500 Subject: [PATCH] Try to fix import strptime bug Clean up of downloadutils and utils. --- resources/lib/downloadutils.py | 39 +++++++++++++++------------------ resources/lib/utils.py | 40 +++++++++++++++------------------- 2 files changed, 34 insertions(+), 45 deletions(-) diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py index 1e104294..34596c2f 100644 --- a/resources/lib/downloadutils.py +++ b/resources/lib/downloadutils.py @@ -11,7 +11,6 @@ import xbmcgui import clientinfo from utils import Logging, window, settings -import utils ################################################################################################## @@ -29,11 +28,10 @@ class DownloadUtils(): # Borg - multiple instances, shared state _shared_state = {} clientInfo = clientinfo.ClientInfo() - addonName = clientInfo.getAddonName() # Requests session s = None - timeout = 30 + default_timeout = 30 def __init__(self): @@ -182,7 +180,19 @@ class DownloadUtils(): deviceId = self.clientInfo.getDeviceId() version = self.clientInfo.getVersion() - if not authenticate: + if authenticate: + auth = ( + 'MediaBrowser UserId="%s", Client="Kodi", Device="%s", DeviceId="%s", Version="%s"' + % (self.userId, deviceName, deviceId, version)) + header = { + + 'Content-type': 'application/json', + 'Accept-encoding': 'gzip', + 'Accept-Charset': 'UTF-8,*', + 'Authorization': auth, + 'X-MediaBrowser-Token': self.token + } + else: # If user is not authenticated auth = ( 'MediaBrowser Client="Kodi", Device="%s", DeviceId="%s", Version="%s"' @@ -194,21 +204,6 @@ class DownloadUtils(): 'Accept-Charset': 'UTF-8,*', 'Authorization': auth } - else: - userId = self.userId - token = self.token - # Attached to the requests session - auth = ( - 'MediaBrowser UserId="%s", Client="Kodi", Device="%s", DeviceId="%s", Version="%s"' - % (userId, deviceName, deviceId, version)) - header = { - - 'Content-type': 'application/json', - 'Accept-encoding': 'gzip', - 'Accept-Charset': 'UTF-8,*', - 'Authorization': auth, - 'X-MediaBrowser-Token': token - } return header @@ -266,14 +261,14 @@ class DownloadUtils(): ##### PREPARE REQUEST ##### kwargs.update({ 'url': url, - 'timeout': self.timeout, + 'timeout': self.default_timeout, 'json': postBody, 'params': parameters }) ##### THE RESPONSE ##### log(kwargs, 2) - r = self.__requests(action_type, session, **kwargs) + r = self._requests(action_type, session, **kwargs) if r.status_code == 204: # No body in the response @@ -358,7 +353,7 @@ class DownloadUtils(): return default_link - def __requests(self, action, session=requests, **kwargs): + def _requests(self, action, session=requests, **kwargs): if action == "GET": r = session.get(**kwargs) diff --git a/resources/lib/utils.py b/resources/lib/utils.py index cda4c5e2..fe281ef5 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -22,39 +22,33 @@ import xbmcvfs ################################################################################################# # Main methods -class Logging(): - - LOGGINGCLASS = None +class Logging(object): - def __init__(self, classname=""): + def __init__(self, title=""): - self.LOGGINGCLASS = classname + self.title = title - def log(self, msg, level=1): + def _getLogLevel(self, level): - self.logMsg("EMBY %s" % self.LOGGINGCLASS, msg, level) - - def logMsg(self, title, msg, level=1): - - # Get the logLevel set in UserClient try: logLevel = int(window('emby_logLevel')) except ValueError: logLevel = 0 - if logLevel >= level: + return logLevel >= level - if logLevel == 2: # inspect.stack() is expensive - try: - xbmc.log("%s -> %s : %s" % (title, inspect.stack()[1][3], msg)) - except UnicodeEncodeError: - xbmc.log("%s -> %s : %s" % (title, inspect.stack()[1][3], msg.encode('utf-8'))) - else: - try: - xbmc.log("%s -> %s" % (title, msg)) - except UnicodeEncodeError: - xbmc.log("%s -> %s" % (title, msg.encode('utf-8'))) + def _printMsg(self, title, msg): + + try: + xbmc.log("%s -> %s" % (title, msg)) + except UnicodeEncodeError: + xbmc.log("%s -> %s" % (title, msg.encode('utf-8'))) + + def log(self, msg, level=1): + + if self._getLogLevel(level): + self._printMsg("EMBY %s" % self.title, msg) # Initiate class for utils.py document logging log = Logging('Utils').log @@ -223,7 +217,7 @@ def setScreensaver(value): def convertDate(date): try: date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ") - except TypeError: + except (ImportError, TypeError): # TypeError: attribute of type 'NoneType' is not callable # Known Kodi/python error date = datetime(*(time.strptime(date, "%Y-%m-%dT%H:%M:%SZ")[0:6]))