From 90b3abc10f9013fc043a82b8eb8e17397da0bb61 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 31 Aug 2016 22:19:55 +0200 Subject: [PATCH] Make the cache interface slightly more extensible. --- manga/htcache.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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() -- 2.11.0