X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fhtcache.py;fp=manga%2Fhtcache.py;h=68546200abfc47a368aff2f8f34dae56bc264264;hb=f3ad0817587482b5a726db4c2f82072e191355e1;hp=0000000000000000000000000000000000000000;hpb=7939bf819a2ff5bec28937c7e37c2693a132a7a0;p=automanga.git diff --git a/manga/htcache.py b/manga/htcache.py new file mode 100644 index 0000000..6854620 --- /dev/null +++ b/manga/htcache.py @@ -0,0 +1,36 @@ +import os, md5, urllib, time +pj = os.path.join + +class cache(object): + def __init__(self, dir): + self.dir = dir + + def mangle(self, url): + n = md5.new() + n.update(url) + return n.hexdigest() + + 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() + if not os.path.isdir(self.dir): + os.makedirs(self.dir) + with open(path, "w") as f: + f.write(data) + return data + +home = os.getenv("HOME") +if home is None or not os.path.isdir(home): + raise Exception("Could not find home directory for HTTP caching") +default = cache(pj(home, ".manga", "htcache")) + +def fetch(url, expire = 3600): + return default.fetch(url, expire)