1 import os, hashlib, urllib.request, time
5 def __init__(self, dir):
10 n.update(url.encode("ascii"))
14 req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"})
15 with urllib.request.urlopen(req) as s:
16 if s.headers.get("content-encoding") == "gzip":
18 return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read()
21 def fetch(self, url, expire=3600):
22 path = pj(self.dir, self.mangle(url))
23 if os.path.exists(path):
24 if time.time() - os.stat(path).st_mtime < expire:
25 with open(path, "rb") as f:
28 if not os.path.isdir(self.dir):
30 with open(path, "wb") as f:
34 home = os.getenv("HOME")
35 if home is None or not os.path.isdir(home):
36 raise Exception("Could not find home directory for HTTP caching")
37 default = cache(pj(home, ".manga", "htcache"))
39 def fetch(url, expire=3600):
40 return default.fetch(url, expire)