From 256d98c6f3c3f128015498222d9d0f77e935d013 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Thu, 4 Feb 2016 19:09:47 -0600 Subject: [PATCH] Fix datetime error Known kodi/python issue where datetime.strptime throws a Nonetype error for no reason, after being used once. --- resources/lib/librarysync.py | 8 ++++---- resources/lib/utils.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index b0fc454b..2a7697cd 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -105,17 +105,17 @@ class LibrarySync(threading.Thread): lastSync = "2010-01-01T00:00:00Z" self.logMsg("Last sync run: %s" % lastSync, 1) - lastSyncTime = datetime.strptime(lastSync, "%Y-%m-%dT%H:%M:%SZ") + lastSyncTime = utils.convertdate(lastSync) self.logMsg("LastIncrementalSync : %s" % lastSyncTime, 1) # get server RetentionDateTime url = "{server}/Emby.Kodi.SyncQueue/GetServerDateTime?format=json" result = self.doUtils.downloadUrl(url) retention_time = "2010-01-01T00:00:00Z" - if result and result.get("RetentionDateTime"): + if result and result.get('RetentionDateTime'): self.logMsg("RetentionDateTime Found", 1) retention_time = result['RetentionDateTime'] - retention_time = datetime.strptime(retention_time, "%Y-%m-%dT%H:%M:%SZ") + retention_time = utils.convertdate(retention_time) self.logMsg("RetentionDateTime : %s" % retention_time, 1) # if last sync before retention time do a full sync @@ -155,7 +155,7 @@ class LibrarySync(threading.Thread): result = self.doUtils.downloadUrl(url) try: # datetime fails when used more than once, TypeError server_time = result['ServerDateTime'] - server_time = datetime.strptime(server_time, "%Y-%m-%dT%H:%M:%SZ") + server_time = utils.convertdate(server_time) except Exception as e: # If the server plugin is not installed or an error happened. diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 8da3c9d9..a83fb48b 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -6,6 +6,7 @@ import cProfile import inspect import pstats import sqlite3 +from datetime import datetime, time import time import unicodedata import xml.etree.ElementTree as etree @@ -238,6 +239,16 @@ def stopProfiling(pr, profileName): "{0}".format(cumulative_time), func_name, filename)) f.close() +def convertdate(date): + try: + date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ") + except 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])) + + return date + def normalize_nodes(text): # For video nodes text = text.replace(":", "")