-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·90 lines (71 loc) · 2.34 KB
/
bot.py
File metadata and controls
executable file
·90 lines (71 loc) · 2.34 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
from mentions import Mentions
from sendtweet import SendTweet
from tokenizetweet import TokenizeTweet
from login import Login
from result import Result
from query import Query
from red import RedSet
import sys, os
from time import sleep
# set max to number of years to allow all years
MAXMENTIONS = 2016 - 1996
sleeptime = 65
sleeptesting = 10
service_url = "http://openciti.ca/cgi-bin/mp/get?"
# processes incoming tweets
tokenizer = TokenizeTweet()
# aquire credentials from API Keys stored in environment variables
twitterlogin = Login()
creds = twitterlogin.api
# sends outgoing tweets (responses to salary data requests)
tweeter = SendTweet(creds)
# simple persistent storage to prevent duplicate tweets
redisdb = RedSet()
# get mentions for the twitter handle associated with the API Keys
getmentions = Mentions(creds)
while True:
try:
# get the last id so searches can exclude previously answered tweets
lastid = redisdb.getlastid()
# get a list of tweets mentioning the bot account
mention_list = getmentions.mentions(lastid)
print ('processing from last known id: ' + str(lastid))
# process and respond if necessary
for m in mention_list:
# proceed only if we've not stored the id in this set
if not redisdb.inset(m.id):
try:
query = tokenizer.tokenize(m.text)
if query.help == True:
tweeter.help(m)
continue
except Exception as err:
e = str(err)
if 'name' in e:
tweeter.noname(m)
elif 'school' in e:
tweeter.noschool(m)
else:
pass
continue
if len(query.names) > 0:
results = query.get(service_url)
lenres = len(results)
if lenres == 0:
tweeter.nores(m)
elif lenres > MAXMENTIONS:
tweeter.replytoomany(m, len(results))
continue
for result in results:
print(str(result))
tweeter.tweet(result, m)
else:
tweeter.replyvague(m)
print ('sleeping....')
sleep(sleeptime)
except Exception as ex:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print ('{}: {} ({}) -- \n\n'.format(fname, exc_tb.tb_lineno, str(ex), exc_type))
sys.exit(2)