Skip to content

Commit

Permalink
fix keeping cache of broken zip
Browse files Browse the repository at this point in the history
  • Loading branch information
EchelonFour committed Aug 8, 2012
1 parent 09b942e commit 000c4d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tvdb_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ def _loadUrl(self, url, recache = False, language=None):
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()
Expand Down
21 changes: 21 additions & 0 deletions tvdb_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ def store_in_cache(cache_location, url, response):
return True
else:
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):
"""Stores responses in a persistant on-disk cache.
Expand Down Expand Up @@ -197,6 +211,13 @@ def recache(self):
)
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__":
def main():
Expand Down

0 comments on commit 000c4d2

Please sign in to comment.