local: Treat dots/periods as digits in destructuring directories.
[automanga.git] / manga / htcache.py
index 5f28c00..a53aa45 100644 (file)
@@ -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,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()
@@ -31,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)