Added initial version of mangafox library.
[automanga.git] / manga / htcache.py
diff --git a/manga/htcache.py b/manga/htcache.py
new file mode 100644 (file)
index 0000000..6854620
--- /dev/null
@@ -0,0 +1,36 @@
+import os, md5, urllib, time
+pj = os.path.join
+
+class cache(object):
+    def __init__(self, dir):
+        self.dir = dir
+
+    def mangle(self, url):
+        n = md5.new()
+        n.update(url)
+        return n.hexdigest()
+
+    def fetch(self, url, expire = 3600):
+        path = pj(self.dir, self.mangle(url))
+        if os.path.exists(path):
+            if time.time() - os.stat(path).st_mtime < expire:
+                with open(path) as f:
+                    return f.read()
+        s = urllib.urlopen(url)
+        try:
+            data = s.read()
+        finally:
+            s.close()
+        if not os.path.isdir(self.dir):
+            os.makedirs(self.dir)
+        with open(path, "w") as f:
+            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"))
+
+def fetch(url, expire = 3600):
+    return default.fetch(url, expire)