4212db34e3df39e30ddb1604e8e7ef10442645db
[automanga.git] / manga / htcache.py
1 import os, md5, urllib, time
2 pj = os.path.join
3
4 class cache(object):
5     def __init__(self, dir):
6         self.dir = dir
7
8     def mangle(self, url):
9         n = md5.new()
10         n.update(url)
11         return n.hexdigest()
12
13     def miss(self, url):
14         s = urllib.urlopen(url)
15         try:
16             if s.headers.get("content-encoding") == "gzip":
17                 import gzip, StringIO
18                 return gzip.GzipFile(fileobj=StringIO.StringIO(s.read()), mode="r").read()
19             return s.read()
20         finally:
21             s.close()
22
23     def fetch(self, url, expire = 3600):
24         path = pj(self.dir, self.mangle(url))
25         if os.path.exists(path):
26             if time.time() - os.stat(path).st_mtime < expire:
27                 with open(path) as f:
28                     return f.read()
29         data = self.miss(url)
30         if not os.path.isdir(self.dir):
31             os.makedirs(self.dir)
32         with open(path, "w") as f:
33             f.write(data)
34         return data
35
36 home = os.getenv("HOME")
37 if home is None or not os.path.isdir(home):
38     raise Exception("Could not find home directory for HTTP caching")
39 default = cache(pj(home, ".manga", "htcache"))
40
41 def fetch(url, expire = 3600):
42     return default.fetch(url, expire)