This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache.py
59 lines (50 loc) · 1.55 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import hashlib, os, codecs
'''
Super basic file-based cache (utf-8 friendly). Helpful if you're developing a
webpage scraper and want to be a bit more polite to the server you're scraping
while developing. The idea is that it caches content in files, each named by an
md5 of the title you pass in.
'''
DEFAULT_DIR = "cache"
cache_dir = DEFAULT_DIR
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
def md5_key(string):
'''
Use this to generate filenae keys
'''
m = hashlib.md5()
m.update(string)
return m.hexdigest()
def set_dir(new_dir = DEFAULT_DIR):
'''
Don't need to call this, unless you want to override the default location
'''
global cache_dir
cache_dir = new_dir
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
def contains(title):
'''
Returns true if a file named by key is in the cache dir
'''
key = md5_key(title)
return os.path.isfile(os.path.join(cache_dir,key))
def get(title):
'''
Returns the contents of the file named by key from the cache dir.
Returns None if file doesn't exist
'''
key = md5_key(title)
if os.path.isfile(os.path.join(cache_dir,key)):
with open(os.path.join(cache_dir,key), "r") as myfile:
return myfile.read()
return None
def put(title,content):
'''
Creates a file in the cache dir named by key, with the content in it
'''
key = md5_key(title)
text_file = codecs.open(os.path.join(cache_dir,key), encoding='utf-8', mode="w")
text_file.write(content)
text_file.close()