-
Notifications
You must be signed in to change notification settings - Fork 48
/
commentLocker.py
167 lines (133 loc) · 3.71 KB
/
commentLocker.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
#!/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 = "Comment locker (by /u/Watchful1)"
LOOP_TIME = 5 * 60
DATABASE_NAME = "database.db"
LOG_LEVEL = logging.INFO
USERNAME = ""
PASSWORD = ""
CLIENT_ID = ""
CLIENT_SECRET = ""
### Logging setup ###
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 interrupt")
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 locked (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
CommentID VARCHAR(10) NOT NULL,
UNIQUE (CommentID)
)
''')
dbConn.commit()
def addComment(commentID):
c = dbConn.cursor()
try:
c.execute('''
INSERT INTO locked
(CommentID)
VALUES (?)
''', (commentID,))
except sqlite3.IntegrityError:
return False
dbConn.commit()
return True
def removeComment(commentID):
c = dbConn.cursor()
c.execute('''
DELETE FROM locked
WHERE CommentID = ?
''', (commentID,))
dbConn.commit()
if c.rowcount == 1:
return True
else:
return False
def isCommentLocked(commentID):
c = dbConn.cursor()
result = c.execute('''
SELECT CommentID
FROM locked
WHERE CommentID = ?
''', (commentID,))
resultTuple = result.fetchone()
if not resultTuple:
return False
else:
return True
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)
log.info(f"Logged into reddit as /u/{str(r.user.me())}")
while True:
try:
sub = r.subreddit(SUBREDDIT)
for comment in sub.stream.comments(skip_existing=True):
log.debug(f"Processing comment {comment.id} with parent {comment.parent_id}")
body = comment.body.lower()
if body.startswith("!lock") and sub.moderator(redditor=comment.author):
addComment(getIDFromFullname(comment.parent_id))
log.info(f"Added comment {comment.id}")
elif body.startswith("!unlock") and sub.moderator(redditor=comment.author):
removeComment(getIDFromFullname(comment.parent_id))
log.info(f"Removed comment {comment.id}")
else:
parent = comment.parent()
while not parent.name.startswith("t3"):
log.debug(f"Checking parent {parent.id}")
if isCommentLocked(parent.id):
log.info(f"Reply to locked comment {parent.id}, removing {comment.id}")
comment.mod.remove()
break
parent = parent.parent()
if parent is None:
break
except Exception as err:
log.warning("Hit an error in main loop")
log.warning(traceback.format_exc())
time.sleep(LOOP_TIME)