X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fhtcache.py;h=45ede5d8f594da98ff068b63d79bc9b5e3e26ff9;hb=3cc7937cd91ec6d3cfb7eebcd4c1afd85c5a615a;hp=68546200abfc47a368aff2f8f34dae56bc264264;hpb=f3ad0817587482b5a726db4c2f82072e191355e1;p=automanga.git diff --git a/manga/htcache.py b/manga/htcache.py index 6854620..45ede5d 100644 --- a/manga/htcache.py +++ b/manga/htcache.py @@ -1,4 +1,4 @@ -import os, md5, urllib, time +import os, hashlib, urllib.request, time pj = os.path.join class cache(object): @@ -6,24 +6,27 @@ class cache(object): self.dir = dir def mangle(self, url): - n = md5.new() - n.update(url) + n = hashlib.md5() + n.update(url.encode("ascii")) return n.hexdigest() - def fetch(self, url, expire = 3600): + def miss(self, url): + with urllib.request.urlopen(url) as s: + if s.headers.get("content-encoding") == "gzip": + import gzip, io + return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read() + return s.read() + + def fetch(self, url, expire=3600): path = pj(self.dir, self.mangle(url)) if os.path.exists(path): if time.time() - os.stat(path).st_mtime < expire: - with open(path) as f: + with open(path, "rb") as f: return f.read() - s = urllib.urlopen(url) - try: - data = s.read() - finally: - s.close() + data = self.miss(url) if not os.path.isdir(self.dir): os.makedirs(self.dir) - with open(path, "w") as f: + with open(path, "wb") as f: f.write(data) return data @@ -32,5 +35,5 @@ if home is None or not os.path.isdir(home): raise Exception("Could not find home directory for HTTP caching") default = cache(pj(home, ".manga", "htcache")) -def fetch(url, expire = 3600): +def fetch(url, expire=3600): return default.fetch(url, expire)