X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fhtcache.py;h=a53aa45a595e9cbfe13696ae8f5f21e4232c3396;hb=90b3abc10f9013fc043a82b8eb8e17397da0bb61;hp=45ede5d8f594da98ff068b63d79bc9b5e3e26ff9;hpb=3cc7937cd91ec6d3cfb7eebcd4c1afd85c5a615a;p=automanga.git diff --git a/manga/htcache.py b/manga/htcache.py index 45ede5d..a53aa45 100644 --- a/manga/htcache.py +++ b/manga/htcache.py @@ -1,6 +1,10 @@ import os, hashlib, urllib.request, time +from . import profile pj = os.path.join +class notfound(Exception): + pass + class cache(object): def __init__(self, dir): self.dir = dir @@ -10,8 +14,18 @@ class cache(object): n.update(url.encode("ascii")) return n.hexdigest() + def open(self, url): + req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"}) + return urllib.request.urlopen(req) + def miss(self, url): - with urllib.request.urlopen(url) as s: + try: + s = self.open(url) + except urllib.error.HTTPError as exc: + if exc.code == 404: + raise notfound(url) + raise + with s: if s.headers.get("content-encoding") == "gzip": import gzip, io return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read() @@ -30,10 +44,7 @@ class cache(object): f.write(data) return data -home = os.getenv("HOME") -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")) +default = cache(pj(profile.confdir, "htcache")) def fetch(url, expire=3600): return default.fetch(url, expire)