X-Git-Url: http://dolda2000.com/gitweb/?p=automanga.git;a=blobdiff_plain;f=manga%2Fhtcache.py;h=a53aa45a595e9cbfe13696ae8f5f21e4232c3396;hp=2aa594ef710750066c380ef85b5f37c61afb62a0;hb=90b3abc10f9013fc043a82b8eb8e17397da0bb61;hpb=ebc277d35244dd94829cd19c3aadd09cc340384b diff --git a/manga/htcache.py b/manga/htcache.py index 2aa594e..a53aa45 100644 --- a/manga/htcache.py +++ b/manga/htcache.py @@ -2,6 +2,9 @@ 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 @@ -11,9 +14,18 @@ class cache(object): n.update(url.encode("ascii")) return n.hexdigest() - def miss(self, url): + def open(self, url): req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"}) - with urllib.request.urlopen(req) as s: + return urllib.request.urlopen(req) + + def miss(self, url): + 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()