-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_conn.py
59 lines (42 loc) · 1.26 KB
/
redis_conn.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
import redis
import configparser
class redis_conn:
def __init__(self):
conf = configparser.ConfigParser()
conf.read("config.ini")
host = conf["redis"]["host"]
port = conf["redis"]["port"]
pwd = conf["redis"]["password"]
self.imagekey = conf["redis"]["imagekey"]
self.wordkey = conf["redis"]["wordskey"]
del conf
self.r = redis.Redis(
host=host,
port=port,
password=pwd)
# only two sets are used:
# for storing unclassified image urls
# for storing unsearched keywords
# 0 used to identify a failed addition or pop.
def write_image(self, img):
try:
self.r.sadd(self.imagekey, img)
return 1
except:
return 0
def write_word(self, word):
try:
self.r.sadd(self.wordkey, word)
return 1
except:
return 0
def get_image(self):
try:
return self.r.spop(self.imagekey).decode("utf-8")
except:
return 0
def get_word(self):
try:
return self.r.spop(self.wordkey).decode("utf-8")
except:
return 0