1 import os, hashlib, urllib.request, time
5 class notfound(Exception):
9 def __init__(self, dir):
12 def mangle(self, url):
14 n.update(url.encode("ascii"))
18 req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"})
19 return urllib.request.urlopen(req)
24 except urllib.error.HTTPError as exc:
29 if s.headers.get("content-encoding") == "gzip":
31 return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read()
34 def fetch(self, url, expire=3600):
35 path = pj(self.dir, self.mangle(url))
36 if os.path.exists(path):
37 if time.time() - os.stat(path).st_mtime < expire:
38 with open(path, "rb") as f:
41 if not os.path.isdir(self.dir):
43 with open(path, "wb") as f:
47 default = cache(pj(profile.confdir, "htcache"))
49 def fetch(url, expire=3600):
50 return default.fetch(url, expire)