-
Notifications
You must be signed in to change notification settings - Fork 0
/
red.py
68 lines (49 loc) · 1.55 KB
/
red.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
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import redis, os
from redis import ConnectionError
class RedSet:
def __init__(self):
# get redis connection details from environment
r_db = 0
r_port = 6379
r_host = 'localhost'
self.red = None
self.red = redis.StrictRedis(host=r_host, port=r_port, db=r_db)
if self.red is None:
sys.stderr.write('start redis-server manually')
sys.exit(2)
# store only the last id here for use with since_id
self.lastreplyid = 'lrid'
# store all replies here so no duplicate tweets are sent out
self.allreplyids = 'rids'
# ignore these users
self.ignorelist = 'bad'
# unhandled errors. so we don't keep trying to answer but may get back to eventually
self.errors = 'err'
def adderr(self, id):
self.red.sadd(self.errors, id)
def addbad(self, id):
self.red.sadd(self.ignorelist, id)
def isbad(self, id):
val = self.red.sismember(self.ignorelist, id)
return True if val == 1 else False
def setlastid(self, id):
self.red.set(self.lastreplyid, id)
def getlastid(self):
id = self.red.get(self.lastreplyid)
if id is None:
id = 0
return int(id)
def addid(self, id):
self.red.sadd(self.allreplyids, id)
def getallreplies(self):
return self.red.smembers(self.allreplyids)
def inset(self, id):
val = self.red.sismember(self.allreplyids, id)
return True if val == 1 else False
if __name__ == '__main__':
r = RedSet()
print('lastid: {}\n'.format(str(r.getlastid())))
replies = r.getallreplies()
for id in replies:
print(id)