1 import os, md5, urllib, time
5 def __init__(self, dir):
14 s = urllib.urlopen(url)
16 if s.headers.get("content-encoding") == "gzip":
18 return gzip.GzipFile(fileobj=StringIO.StringIO(s.read()), mode="r").read()
23 def fetch(self, url, expire = 3600):
24 path = pj(self.dir, self.mangle(url))
25 if os.path.exists(path):
26 if time.time() - os.stat(path).st_mtime < expire:
30 if not os.path.isdir(self.dir):
32 with open(path, "w") as f:
36 home = os.getenv("HOME")
37 if home is None or not os.path.isdir(home):
38 raise Exception("Could not find home directory for HTTP caching")
39 default = cache(pj(home, ".manga", "htcache"))
41 def fetch(url, expire = 3600):
42 return default.fetch(url, expire)