-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathvoteBot.py
441 lines (358 loc) · 9.96 KB
/
voteBot.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/python3
import praw
import os
import logging.handlers
import sys
import configparser
import signal
import time
import traceback
import sqlite3
import re
from shutil import copyfile
from datetime import datetime
from datetime import timedelta
### Config ###
LOG_FOLDER_NAME = "logs"
SUBREDDIT = "subtestbot1"
USER_AGENT = "vote counter (by /u/Watchful1)"
LOOP_TIME = 5 * 60
REDDIT_OWNER = "Watchful1"
SUBREDDIT_LINK = "https://www.reddit.com/r/{}/comments/".format(SUBREDDIT)
DATABASE_NAME = "database.db"
BACKLOG_HOURS = 0
START_TIME = datetime.utcnow()
USERNAME = ""
PASSWORD = ""
CLIENT_ID = ""
CLIENT_SECRET = ""
### Logging setup ###
LOG_LEVEL = logging.DEBUG
if not os.path.exists(LOG_FOLDER_NAME):
os.makedirs(LOG_FOLDER_NAME)
LOG_FILENAME = LOG_FOLDER_NAME+"/"+"bot.log"
LOG_FILE_BACKUPCOUNT = 5
LOG_FILE_MAXSIZE = 1024 * 256 * 64
log = logging.getLogger("bot")
log.setLevel(LOG_LEVEL)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s')
log_stderrHandler = logging.StreamHandler()
log_stderrHandler.setFormatter(log_formatter)
log.addHandler(log_stderrHandler)
if LOG_FILENAME is not None:
log_fileHandler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=LOG_FILE_MAXSIZE, backupCount=LOG_FILE_BACKUPCOUNT)
log_formatter_file = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
log_fileHandler.setFormatter(log_formatter_file)
log.addHandler(log_fileHandler)
def signal_handler(signal, frame):
log.info("Handling interupt")
dbConn.commit()
dbConn.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
dbConn = sqlite3.connect(DATABASE_NAME)
c = dbConn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS votes (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
CommentID VARCHAR(10) NOT NULL,
Voter VARCHAR(80) NOT NULL,
VotedFor VARCHAR(80) NOT NULL,
UNIQUE (CommentID, Voter)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS keystore (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
KeyName VARCHAR(80) NOT NULL,
KeyValue VARCHAR(80) NOT NULL,
UNIQUE (KeyName)
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS optOut (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Username VARCHAR(80) NOT NULL,
UNIQUE (Username)
)
''')
dbConn.commit()
def addVote(commentID, voter, votedFor):
c = dbConn.cursor()
try:
c.execute('''
INSERT INTO votes
(CommentID, Voter, VotedFor)
VALUES (?, ?, ?)
''', (commentID, voter.lower(), votedFor.lower()))
except sqlite3.IntegrityError:
return False
dbConn.commit()
return True
def getVotes():
c = dbConn.cursor()
results = []
for row in c.execute('''
SELECT VotedFor, count(*) as VoteNum
FROM votes
GROUP BY VotedFor
ORDER BY VoteNum desc
'''):
results.append({"user": row[0], "votes": row[1]})
return results
def clearVotesForUser(username):
c = dbConn.cursor()
c.execute('''
DELETE FROM votes
WHERE VotedFor = ?
''', (username.lower(),))
dbConn.commit()
if c.rowcount == 1:
return True
else:
return False
def clearVotes():
c = dbConn.cursor()
c.execute('''
DELETE FROM votes
''')
dbConn.commit()
if c.rowcount > 0:
return True
else:
return False
def isCommentProcessed(commentID):
c = dbConn.cursor()
result = c.execute('''
SELECT CommentID
FROM votes
WHERE CommentID = ?
''', (commentID,))
resultTuple = result.fetchone()
if not resultTuple:
return False
else:
return True
def hasAuthorVoted(voter):
c = dbConn.cursor()
result = c.execute('''
SELECT CommentID
FROM votes
WHERE Voter = ?
''', (voter.lower(),))
resultTuple = result.fetchone()
if not resultTuple:
return False
else:
return True
def addKey(key, value):
c = dbConn.cursor()
try:
c.execute('''
INSERT INTO keystore
(KeyName, KeyValue)
VALUES (?, ?)
''', (key, value))
except sqlite3.IntegrityError:
return False
dbConn.commit()
return True
def getValue(key):
c = dbConn.cursor()
result = c.execute('''
SELECT KeyValue
FROM keystore
WHERE KeyName = ?
''', (key,))
resultTuple = result.fetchone()
if not resultTuple:
return None
else:
return resultTuple[0]
def deleteKey(key):
c = dbConn.cursor()
c.execute('''
DELETE FROM keystore
WHERE KeyName = ?
''', (key,))
dbConn.commit()
if c.rowcount == 1:
return True
else:
return False
def optOut(username):
c = dbConn.cursor()
try:
c.execute('''
INSERT INTO optOut
(Username)
VALUES (?)
''', (username.lower(),))
except sqlite3.IntegrityError:
return False
dbConn.commit()
return True
def optedOut(username):
c = dbConn.cursor()
result = c.execute('''
SELECT Username
FROM optOut
WHERE Username = ?
''', (username.lower(),))
resultTuple = result.fetchone()
if not resultTuple:
return False
else:
return True
def clearOptOuts():
c = dbConn.cursor()
c.execute('''
DELETE FROM optOut
''')
dbConn.commit()
if c.rowcount > 0:
return True
else:
return False
threadMonth = getValue("threadMonth")
if threadMonth is None:
threadMonth = 13
else:
threadMonth = int(threadMonth)
voteThread = getValue("thread")
def endVoting():
global dbConn
global threadMonth
global voteThread
log.info("Closing thread: {}".format(voteThread))
votes = getVotes()
bldr = ["Voting finished!\n\n"]
for vote in votes:
bldr.append(vote["user"])
bldr.append(" has ")
bldr.append(str(vote["votes"]))
bldr.append(" vote(s) \n")
bldr.append("\n\n")
winner = votes[0]["user"]
bldr.append(winner)
bldr.append(" wins!")
voteThreadSubmission = r.submission(id=voteThread)
voteThreadComment = voteThreadSubmission.reply(''.join(bldr))
voteThreadComment.mod.distinguish(how='yes', sticky=True)
voteThreadSubmission.mod.lock()
sub.flair.set(winner, "Monthly Winner")
r.redditor(winner).message("Monthly Winner", "You won!", from_subreddit=SUBREDDIT)
dbConn.commit()
dbConn.close()
copyfile(DATABASE_NAME,
voteThread + "-backup.db")
dbConn = sqlite3.connect("database.db")
clearVotes()
clearOptOuts()
deleteKey("thread")
deleteKey("threadMonth")
voteThread = None
threadMonth = 13
def compareMonths(firstMonth, secondMonth):
return firstMonth < secondMonth or firstMonth == 12 and secondMonth == 1
def getIDFromFullname(fullname):
return re.findall('^(?:t\d_)?(.{4,8})', fullname)[0]
log.debug("Connecting to reddit")
r = praw.Reddit(
username=USERNAME
,password=PASSWORD
,client_id=CLIENT_ID
,client_secret=CLIENT_SECRET
,user_agent=USER_AGENT)
# r = praw.Reddit(
# "Watchful1BotTest"
# ,user_agent=USER_AGENT)
def redditorExists(username):
try:
id = r.redditor(username).id
return True
except Exception as err:
return False
log.info("Logged into reddit as /u/{}".format(str(r.user.me())))
while True:
try:
sub = r.subreddit(SUBREDDIT)
for comment in sub.stream.comments():
if compareMonths(threadMonth, datetime.utcnow().month):
endVoting()
if comment is None or datetime.utcfromtimestamp(comment.created_utc) < START_TIME - timedelta(hours=BACKLOG_HOURS) \
or comment.author == r.user.me():
continue
log.info("Processing comment {} from /u/{}".format(comment.id, comment.author.name))
body = comment.body.lower()
if body.startswith("summonvotebot") and comment.author.name.lower() == REDDIT_OWNER.lower():
log.info("Owner summoning vote bot")
if voteThread is None:
log.info("Successfully summoned for thread: {}".format(getIDFromFullname(comment.link_id)))
voteThread = getIDFromFullname(comment.link_id)
addKey("thread", voteThread)
threadMonth = datetime.utcnow().month
addKey("threadMonth", str(threadMonth))
comment.reply("Thread activated")
else:
log.info("Bot already active in another thread")
comment.reply("Bot is already activated in [this thread]({}{}). You need to close it there first."
.format(SUBREDDIT_LINK, voteThread))
elif body.startswith("endvotebot") and comment.author.name.lower() == REDDIT_OWNER.lower():
log.info("Owner ending vote bot")
if voteThread is not None:
endVoting()
else:
log.info("Bot not active in this thread")
comment.reply("Bot is activated in [this thread]({}{})."
.format(SUBREDDIT_LINK, voteThread))
elif voteThread is not None:
if voteThread == getIDFromFullname(comment.link_id):
log.info("Comment is in the active thread")
if body.startswith("optout"):
log.info("Opting out comment author: /u/{}".format(comment.author.name))
optOut(comment.author.name)
clearVotesForUser(comment.author.name)
comment.reply("You have opted out of voting")
elif body.startswith("vote"):
log.info("Vote comment")
if isCommentProcessed(comment.id):
log.info("Comment already processed")
elif hasAuthorVoted(comment.author.name):
log.info("Comment author has already voted")
comment.reply("You have already voted.")
else:
votedForArray = re.findall('(?:vote )(?:/u/)?(\w+)', body)
if not len(votedForArray):
log.info("Could not find a vote in comment")
else:
votedFor = votedForArray[0].lower()
log.info("Found a vote: {}".format(votedFor))
if not redditorExists(votedFor):
log.info("Redditor doesn't exist")
else:
if optedOut(votedFor):
log.info("/u/{} has opted out of voting".format(votedFor))
comment.reply("This user has opted out of voting")
else:
result = addVote(comment.id, comment.author.name, votedFor)
if not result:
log.info("Something went wrong")
else:
log.info("Successfully voted, posting comment")
votes = getVotes()
bldr = ["Vote tabulated!\n\n"]
for vote in votes:
bldr.append(vote["user"])
bldr.append(" has ")
bldr.append(str(vote["votes"]))
bldr.append(" vote(s) \n")
comment.reply(''.join(bldr))
else:
log.info("Not a vote comment")
else:
log.info("Not in vote thread")
except Exception as err:
log.warning("Hit an error in main loop")
log.warning(traceback.format_exc())
time.sleep(LOOP_TIME)