From 82db6e9b54b6d419478818a9ff76a4652dcdff19 Mon Sep 17 00:00:00 2001 From: echel0n Date: Thu, 13 Mar 2014 21:44:32 -0700 Subject: [PATCH] Fixed a bug in TVRage API that was not properly handling datetime issues --- lib/tvrage_api/tvrage_api.py | 37 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/lib/tvrage_api/tvrage_api.py b/lib/tvrage_api/tvrage_api.py index c26cdf31..79d2bad8 100644 --- a/lib/tvrage_api/tvrage_api.py +++ b/lib/tvrage_api/tvrage_api.py @@ -28,8 +28,10 @@ try: except ImportError: import xml.etree.ElementTree as ElementTree -from tvrage_cache import CacheHandler +from lib import requests +from lib.dateutil.parser import parse +from tvrage_cache import CacheHandler from tvrage_ui import BaseUI from tvrage_exceptions import (tvrage_error, tvrage_userabort, tvrage_shownotfound, tvrage_seasonnotfound, tvrage_episodenotfound, tvrage_attributenotfound) @@ -409,7 +411,8 @@ class TVRage: global lastTimeout try: log().debug("Retrieving URL %s" % url) - resp = self.urlopener.open(url) + #resp = self.urlopener.open(url) + resp = requests.get(url) if 'x-local-cache' in resp.headers: log().debug("URL %s was cached in %s" % ( url, @@ -423,7 +426,7 @@ class TVRage: lastTimeout = dt.datetime.now() raise tvrage_error("Could not connect to server: %s" % (errormsg)) - return resp.read() + return resp.content def _getetsrc(self, url): """Loads a URL using caching, returns an ElementTree of the source @@ -456,18 +459,8 @@ class TVRage: elm.tag = robj.sub(lambda m: reDict[m.group(0)], elm.tag) if elm.tag in 'firstaired': - try: - fixDate = dt.datetime.strptime(elm.text,"%b/%d/%Y") - value = fixDate.strftime("%Y-%m-%d") - except: - try: - fixDate = dt.datetime.strptime(elm.text,"%b/%Y") - newDate = fixDate.replace(day=01) - value = newDate.strftime("%Y-%m-%d") - except: - fixDate = dt.datetime.strptime(elm.text,"%Y") - newDate = fixDate.replace(month=01, day=01) - value = newDate.strftime("%Y-%m-%d") + fixDate = parse(elm.text) + value = fixDate.strftime("%Y-%m-%d") elm.text = value return ElementTree.fromstring(ElementTree.tostring(xml)) @@ -480,18 +473,8 @@ class TVRage: elm.tag = robj.sub(lambda m: reDict[m.group(0)], elm.tag) if elm.tag in 'firstaired': - try: - fixDate = dt.datetime.strptime(elm.text,"%b/%d/%Y") - value = fixDate.strftime("%Y-%m-%d") - except: - try: - fixDate = dt.datetime.strptime(elm.text,"%b/%Y") - newDate = fixDate.replace(day=01) - value = newDate.strftime("%Y-%m-%d") - except: - fixDate = dt.datetime.strptime(elm.text,"%Y") - newDate = fixDate.replace(month=01, day=01) - value = newDate.strftime("%Y-%m-%d") + fixDate = parse(elm.text) + value = fixDate.strftime("%Y-%m-%d") elm.text = value return ElementTree.fromstring(ElementTree.tostring(xml))