From 963d95ec0d39e922cb50151b87b69e4f2b5feb19 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 19 Mar 2008 13:58:48 +0000 Subject: [PATCH] and do not fail for new files --- scrapeit/cache.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/scrapeit/cache.py b/scrapeit/cache.py index b5553b8..f577d41 100644 --- a/scrapeit/cache.py +++ b/scrapeit/cache.py @@ -22,24 +22,23 @@ def read_url(url): cache_file = "%sindex.html" % cache_file if os.path.isdir(cache_file): cache_file = os.path.join(cache_file, "index.html") - ctime = os.stat(cache_file).st_ctime - now = time.mktime(time.localtime()) - file_age = now-ctime - print cache_timeout-file_age - if file_age < cache_timeout and os.path.exists(cache_file): - f = open(cache_file) - data = f.read() - f.close() - return data - else: - data = utils.read_url(url) - folder = os.path.dirname(cache_file) - if not os.path.exists(folder): - os.makedirs(folder) - f = open(cache_file, 'w') - f.write(data) - f.close() - return data + if os.path.exists(cache_file): + ctime = os.stat(cache_file).st_ctime + now = time.mktime(time.localtime()) + file_age = now-ctime + if file_age < cache_timeout: + f = open(cache_file) + data = f.read() + f.close() + return data + data = utils.read_url(url) + folder = os.path.dirname(cache_file) + if not os.path.exists(folder): + os.makedirs(folder) + f = open(cache_file, 'w') + f.write(data) + f.close() + return data def read_url_utf8(url): data = read_url(url)