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: | |
75efe5be FT |
16 | if s.headers.get("content-encoding") == "gzip": |
17 | import gzip, StringIO | |
18 | return gzip.GzipFile(fileobj=StringIO.StringIO(s.read()), mode="r").read() | |
d6c0e189 FT |
19 | return s.read() |
20 | finally: | |
21 | s.close() | |
22 | ||
f3ad0817 FT |
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() | |
d6c0e189 | 29 | data = self.miss(url) |
f3ad0817 FT |
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) |