1 import os, hashlib, urllib.request, time
6 def __init__(self, dir):
11 n.update(url.encode("ascii"))
15 req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"})
16 with urllib.request.urlopen(req) as s:
17 if s.headers.get("content-encoding") == "gzip":
19 return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read()
22 def fetch(self, url, expire=3600):
23 path = pj(self.dir, self.mangle(url))
24 if os.path.exists(path):
25 if time.time() - os.stat(path).st_mtime < expire:
26 with open(path, "rb") as f:
29 if not os.path.isdir(self.dir):
31 with open(path, "wb") as f:
35 default = cache(pj(profile.confdir, "htcache"))
37 def fetch(url, expire=3600):
38 return default.fetch(url, expire)