Handle mangafox download errors slightly nicer.
[automanga.git] / manga / htcache.py
CommitLineData
f3ad0817
FT
1import os, md5, urllib, time
2pj = os.path.join
3
4class 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 fetch(self, url, expire = 3600):
14 path = pj(self.dir, self.mangle(url))
15 if os.path.exists(path):
16 if time.time() - os.stat(path).st_mtime < expire:
17 with open(path) as f:
18 return f.read()
19 s = urllib.urlopen(url)
20 try:
21 data = s.read()
22 finally:
23 s.close()
24 if not os.path.isdir(self.dir):
25 os.makedirs(self.dir)
26 with open(path, "w") as f:
27 f.write(data)
28 return data
29
30home = os.getenv("HOME")
31if home is None or not os.path.isdir(home):
32 raise Exception("Could not find home directory for HTTP caching")
33default = cache(pj(home, ".manga", "htcache"))
34
35def fetch(url, expire = 3600):
36 return default.fetch(url, expire)