forked from sunziping2016/THUCourseSpider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.py
35 lines (34 loc) · 1010 Bytes
/
captcha.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
import hashlib
class CaptchaCache(object):
def __init__(self, filename):
self.filename = filename
self.cache = {}
def load(self):
try:
cache = {}
f = open(self.filename)
for line in f:
md5sum, code = line.strip().split('\t')
cache[md5sum] = code
f.close()
self.cache = cache
except FileNotFoundError as e:
pass
def save(self):
f = open(self.filename, 'w')
for item in self.cache.items():
f.write(item[0] + '\t' + item[1] + '\n')
def search(self, image):
m = hashlib.md5()
m.update(image)
key = m.hexdigest()
return self.cache.get(key)
def add(self, image, code):
m = hashlib.md5()
m.update(image)
key = m.hexdigest()
self.cache[key] = code
def merge(self, other):
self.cache.update(other.cache)
def __len__(self):
return len(self.cache)