forked from hoolrory/GroupMeRedditBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_for_messages.py
executable file
·418 lines (360 loc) · 12.2 KB
/
check_for_messages.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/user/bin/python
'''
Copyright (c) 2016 Rory Hool
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import csv
import groupme
import keystore
import multiprocessing
import os
import reddit
import simplekv
import sys
import time;
import traceback
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
def find_number(words):
number = 0
for word in words:
if is_number(word):
return int(word)
return number
def find_subreddit(words):
known_subs_file = "known_subs.csv"
if os.path.isfile(known_subs_file):
with open(known_subs_file, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
for word in words:
if word.lower() in (sub.lower() for sub in row):
return word
return ''
def find_word_after(words, afterWord):
i = 0
for word in words:
i += 1
if word == afterWord:
return words[i]
def find_word_before(words, beforeWord):
i = 0
for word in words:
if word == beforeWord:
return words[i-1]
i += 1
def set_auto_interval(interval, words):
int(interval)
if interval != 0:
if "hour" in words:
interval = interval * 60 * 60
elif "hours" in words:
interval = interval * 60 * 60
elif "half" in words:
interval = interval * 30 * 60
elif "minutes" in words:
interval = interval * 60
elif "minute" in words:
interval = interval *60
elif interval == 0:
if "hours" in words:
interval = 120 * 60
elif "hour" in words:
interval = 60 * 60
elif "half" in words:
interval = 30 * 60
elif "minutes" in words:
interval = find_word_before(words, "minutes")
if is_number(interval):
int(interval)
interval = interval * 60
else:
interval = 5 * 60
elif "minute" in words:
interval = 60
return interval
def remove_auto_params(bot_id):
try:
keystore.delete('auto_sub')
keystore.delete('auto_interval')
keystore.delete('last_auto_post')
keystore.delete('auto_count')
except:
return
lastChange = time.time()
keystore.put('last_auto_change', lastChange)
keystore.put('active_auto', 0)
groupme.post_message(bot_id, "Auto posting stopped")
return
def check_subreddit(bot_id, subreddit):
subredditResult = reddit.subreddit_exists( subreddit )
if subredditResult.error is not None:
groupme.post_message( bot_id, subredditResult.error )
return False
if not subredditResult.exists:
message = "Subreddit /r/{0} does not exist.".format(subreddit)
if subredditResult.suggestion is not None:
message += " Did you mean /r/{0}?".format(subredditResult.suggestion)
groupme.post_message(bot_id, message)
return False
return True
def check_blacklist(bot_id, subreddit):
blacklisted_subs_file = "blacklisted_subs.csv"
if os.path.isfile(blacklisted_subs_file):
with open(blacklisted_subs_file, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
if subreddit.lower() in (sub.lower() for sub in row):
groupme.post_message(bot_id, "We're not gonna go there")
return True
return False
def check_active_auto():
auto = 0
try:
auto = int(keystore.get('active_auto'))
except:
return False
if auto == 1:
return True
else:
return False
def check_auto_words(bot_id, subreddit, words):
if "cancel" in words:
remove_auto_params(bot_id)
return
elif "stop" in words:
remove_auto_params(bot_id)
return
now = time.time()
interval = 0
count = 0
try:
lastChange = float(keystore.get('last_auto_change'))
except:
lastChange = 0
if "change" in words:
if not check_active_auto():
groupme.post_message(bot_id, "No active Auto post to change. Sorry.")
return
if lastChange != 0:
diff = now - lastChange
if diff < 300:
groupme.post_message(bot_id, "Why you wanna change things so much?")
return
if "time" in words:
interval = find_number(words)
interval = set_auto_interval(int(interval), words)
if interval != 0:
keystore.put('auto_interval', interval)
keystore.put('last_auto_change', now)
return
else:
return
elif "subreddit" in words:
subreddit = find_subreddit(words)
if subreddit == '':
if "to" in words:
subreddit = find_word_after(words, "to")
if not check_subreddit(bot_id, subreddit):
return
if not check_blacklist(bot_id, subreddit):
print("changing sub")
keystore.put('auto_sub', subreddit)
keystore.put('last_auto_change', now)
return
else:
return
else:
if not check_blacklist(bot_id, subreddit):
print("changing sub")
keystore.put('auto_sub', subreddit)
keystore.put('last_auto_change', now)
return
else:
return
elif "number" in words:
count = find_number(words)
if count == 0:
count = 1
if count > 3:
count = 3
keystore.put('auto_count', count)
keystore.put('last_auto_change', now)
return
elif "every" in words:
if check_active_auto():
groupme.post_message(bot_id, "Active auto post already exists.")
return
subreddit = find_word_before(words, "every")
count = find_word_before(words, str(subreddit))
interval = find_word_after(words, "every")
if not check_subreddit(bot_id, subreddit):
return
if not is_number(count):
count = 1
if is_number(interval):
interval = set_auto_interval(int(interval), words)
else:
interval = 0
interval = set_auto_interval(int(interval), words)
if count == 0:
count = 1
if int(count) > 3:
count = 3
if not check_blacklist(bot_id, subreddit):
keystore.put('auto_sub', subreddit)
keystore.put('auto_interval', interval)
keystore.put('auto_count', count)
keystore.put('last_auto_change', now)
keystore.put('last_auto_post', now)
keystore.put('active_auto', 1)
return
def check_words(bot_id, words):
count = find_number(words)
subreddit = find_subreddit(words)
if "every" in words or "change" in words or "stop" in words or "cancel" in words:
check_auto_words(bot_id, subreddit, words)
return
if count == 0:
if "some" in words:
print "Found word some"
wordAfterSome = find_word_after(words, "some")
if subreddit == '':
subreddit = wordAfterSome
count = 2
elif subreddit == wordAfterSome:
count = 2
elif "show" in words:
print "Found word show"
wordAfterShow = find_word_after(words, "show")
if subreddit == '':
subreddit = wordAfterShow
count = 1
elif subreddit == wordAfterShow:
count = 1
elif subreddit == '' :
subreddit = find_word_after(words, str(count))
if count == 0:
count = 1
if count > 3:
count = 3
print 'Found number {0}'.format(count)
if subreddit == '':
subreddit = find_word_after(words, str(count))
if subreddit is None or subreddit == '':
return True
print 'Found subreddit {0}'.format(subreddit)
process_rbot_message(bot_id, subreddit, count)
def post_message(bot_id, subreddit):
message = reddit.get_url_from_subreddit(bot_id, subreddit)
if message is None:
message = "Failed to get url"
groupme.post_message(bot_id, message)
def process_rbot_message(bot_id, subreddit, count):
if check_blacklist(bot_id, subreddit):
print("blacklist")
return True
if not check_subreddit(bot_id, subreddit):
return True
for i in range( 0, count ):
post_message(bot_id, subreddit)
return True
def process_auto_message(bot_id):
print("auto message")
subreddit = ''
last_post = 0
interval = 0
count = 0
try:
subreddit = str(keystore.get('auto_sub'))
interval = int(keystore.get('auto_interval'))
count = int(keystore.get('auto_count'))
last_post = float(keystore.get('last_auto_post'))
except:
print("auto message error")
return
now = 0.0
if last_post != 0:
now = time.time()
diff = now - last_post
if diff < interval:
print("not enough time passed")
return
print(count)
print(subreddit)
for i in range(0, count):
post_message(bot_id, subreddit)
keystore.put('last_auto_post', now)
return
def process_message(bot_id, message):
text = ''
messageText = message['text']
if messageText is not None:
text = messageText.encode('utf-8')
words = text.split(' ')
for word in words:
if word.lower() == 'rbot':
print 'Found rbot in message', message['id'].encode('utf-8')
check_words(bot_id, words)
return
def process_command(auth_token, group_id, bot_id):
timeout_until()
lastMessageId = ''
lastMessageIdKey = 'lastMessageId-' + str(bot_id)
if keystore.contains(lastMessageIdKey):
lastMessageId = keystore.get(lastMessageIdKey)
print "LastMessageId was", lastMessageId
else:
print "No LastMessageId with key {0}".format(lastMessageIdKey)
messages = groupme.get_messages(auth_token, group_id, lastMessageId)
print 'Got {0} new messages'.format(len(messages))
if len(messages) > 0:
message = messages[0]
print message
print message['id'].encode('utf-8')
keystore.put(lastMessageIdKey, message['id'].encode('utf-8'))
print "x"
jobs = []
for message in messages:
p = multiprocessing.Process(target=process_message, args=(bot_id, message))
jobs.append(p)
p.start()
return
def timeout_until():
if keystore.contains('timeout_until'):
timeoutTime = float(keystore.get('timeout_until'))
now = time.time()
if timeoutTime > now:
timeDiff = timeoutTime - now
print 'Timeout - Waiting {0} minute(s) longer'.format(int(timeDiff * 0.0166667))
sys.exit()
else:
keystore.delete('timeout_until')
try:
auth_token = str(sys.argv[1])
group_id = str(sys.argv[2])
bot_id = str(sys.argv[3])
process_command(auth_token, group_id, bot_id)
process_auto_message(bot_id)
except:
logs_directory = "logs"
if not os.path.exists(logs_directory):
os.makedirs(logs_directory)
with open("{0}/{1}_error_log.txt".format(logs_directory, group_id), "a+") as logFile:
exc_type, exc_value, exc_traceback = sys.exc_info()
logFile.write("Rbot crashed in with group_id: " + group_id + " and bot_id " + bot_id + "\r\n")
traceback.print_tb(exc_traceback, limit=1, file=logFile)
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=100, file=logFile)
logFile.write("\r\n\r\n")