Skip to content

Commit

Permalink
updated tvdb_api. catch badZipfile
Browse files Browse the repository at this point in the history
  • Loading branch information
lad1337 committed Aug 10, 2012
1 parent 04f7c0b commit 7a39041
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
17 changes: 11 additions & 6 deletions lib/tvdb_api/tvdb_api.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -528,12 +528,17 @@ def _loadUrl(self, url, recache = False, language=None):
raise tvdb_error("Received gzip data from thetvdb.com, but could not correctly handle it") raise tvdb_error("Received gzip data from thetvdb.com, but could not correctly handle it")


if 'application/zip' in resp.headers.get("Content-Type", ''): if 'application/zip' in resp.headers.get("Content-Type", ''):
# TODO: The zip contains actors.xml and banners.xml, which are currently ignored [GH-20] try:
log().debug("We recived a zip file unpacking now ...") # TODO: The zip contains actors.xml and banners.xml, which are currently ignored [GH-20]
zipdata = StringIO.StringIO() log().debug("We recived a zip file unpacking now ...")
zipdata.write(resp.read()) zipdata = StringIO.StringIO()
myzipfile = zipfile.ZipFile(zipdata) zipdata.write(resp.read())
return myzipfile.read('%s.xml' % language) myzipfile = zipfile.ZipFile(zipdata)
return myzipfile.read('%s.xml' % language)
except zipfile.BadZipfile:
if 'x-local-cache' in resp.headers:
resp.delete_cache()
raise tvdb_error("Bad zip file received from thetvdb.com, could not read it")


return resp.read() return resp.read()


Expand Down
29 changes: 25 additions & 4 deletions lib/tvdb_api/tvdb_cache.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,18 +75,32 @@ def store_in_cache(cache_location, url, response):
"""Tries to store response in cache.""" """Tries to store response in cache."""
hpath, bpath = calculate_cache_path(cache_location, url) hpath, bpath = calculate_cache_path(cache_location, url)
try: try:
outf = open(hpath, "w") outf = open(hpath, "wb")
headers = str(response.info()) headers = str(response.info())
outf.write(headers) outf.write(headers)
outf.close() outf.close()


outf = open(bpath, "w") outf = open(bpath, "wb")
outf.write(response.read()) outf.write(response.read())
outf.close() outf.close()
except IOError: except IOError:
return True return True
else: else:
return False return False

@locked_function
def delete_from_cache(cache_location, url):
"""Deletes a response in cache."""
hpath, bpath = calculate_cache_path(cache_location, url)
try:
if os.path.exists(hpath):
os.remove(hpath)
if os.path.exists(bpath):
os.remove(bpath)
except IOError:
return True
else:
return False


class CacheHandler(urllib2.BaseHandler): class CacheHandler(urllib2.BaseHandler):
"""Stores responses in a persistant on-disk cache. """Stores responses in a persistant on-disk cache.
Expand Down Expand Up @@ -167,12 +181,12 @@ def __init__(self, cache_location, url, set_cache_header=True):
self.cache_location = cache_location self.cache_location = cache_location
hpath, bpath = calculate_cache_path(cache_location, url) hpath, bpath = calculate_cache_path(cache_location, url)


StringIO.StringIO.__init__(self, file(bpath).read()) StringIO.StringIO.__init__(self, file(bpath, "rb").read())


self.url = url self.url = url
self.code = 200 self.code = 200
self.msg = "OK" self.msg = "OK"
headerbuf = file(hpath).read() headerbuf = file(hpath, "rb").read()
if set_cache_header: if set_cache_header:
headerbuf += "x-local-cache: %s\r\n" % (bpath) headerbuf += "x-local-cache: %s\r\n" % (bpath)
self.headers = httplib.HTTPMessage(StringIO.StringIO(headerbuf)) self.headers = httplib.HTTPMessage(StringIO.StringIO(headerbuf))
Expand All @@ -197,6 +211,13 @@ def recache(self):
) )
CachedResponse.__init__(self, self.cache_location, self.url, True) CachedResponse.__init__(self, self.cache_location, self.url, True)


@locked_function
def delete_cache(self):
delete_from_cache(
self.cache_location,
self.url
)



if __name__ == "__main__": if __name__ == "__main__":
def main(): def main():
Expand Down

0 comments on commit 7a39041

Please sign in to comment.