Commit | Line | Data |
---|---|---|
f3ad0817 FT |
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 | ||
d6c0e189 FT |
13 | def miss(self, url): |
14 | s = urllib.urlopen(url) | |
15 | try: | |
16 | return s.read() | |
17 | finally: | |
18 | s.close() | |
19 | ||
f3ad0817 FT |
20 | def fetch(self, url, expire = 3600): |
21 | path = pj(self.dir, self.mangle(url)) | |
22 | if os.path.exists(path): | |
23 | if time.time() - os.stat(path).st_mtime < expire: | |
24 | with open(path) as f: | |
25 | return f.read() | |
d6c0e189 | 26 | data = self.miss(url) |
f3ad0817 FT |
27 | if not os.path.isdir(self.dir): |
28 | os.makedirs(self.dir) | |
29 | with open(path, "w") as f: | |
30 | f.write(data) | |
31 | return data | |
32 | ||
33 | home = os.getenv("HOME") | |
34 | if home is None or not os.path.isdir(home): | |
35 | raise Exception("Could not find home directory for HTTP caching") | |
36 | default = cache(pj(home, ".manga", "htcache")) | |
37 | ||
38 | def fetch(url, expire = 3600): | |
39 | return default.fetch(url, expire) |