Added batoto byname implementation.
[automanga.git] / manga / htcache.py
index 6854620..4212db3 100644 (file)
@@ -10,17 +10,23 @@ class cache(object):
         n.update(url)
         return n.hexdigest()
 
+    def miss(self, url):
+        s = urllib.urlopen(url)
+        try:
+            if s.headers.get("content-encoding") == "gzip":
+                import gzip, StringIO
+                return gzip.GzipFile(fileobj=StringIO.StringIO(s.read()), mode="r").read()
+            return s.read()
+        finally:
+            s.close()
+
     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()
+        data = self.miss(url)
         if not os.path.isdir(self.dir):
             os.makedirs(self.dir)
         with open(path, "w") as f: