-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflairbot_all_comments.py
249 lines (197 loc) · 7.01 KB
/
flairbot_all_comments.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
from prawcore.exceptions import RequestException, ResponseException, InvalidToken
import threading
import config
import praw
import time
now_time = time.time()
class bot:
# defined variables
def __init__(self):
self.sub = config.sub
self.reddit = praw.Reddit(client_id = config.id,
client_secret = config.secret,
password = config.password,
user_agent = config.user_agent,
username = config.username)
self.time_removal = config.time_removal
self.time_flair = config.time_flair
self.flair_message = config.flair_message
self.removal_message = config.removal_message
self.removal_title = config.removal_title
self.removal_type = ('public')
self.loggingdict = {}
# takes submission ID to reply to in the form of a comment
# returns the comment ID
def comment(self, submission):
sm = submission.reply(self.flair_message)
sm.mod.distinguish(sticky=True)
return sm.id
# takes comment ID to remove the comment
def comment_remove(self, comment):
self.reddit.comment(comment).delete()
# takes submission ID to remove the submission
def post_remove(self, submission):
self.reddit.submission(submission).mod.remove()
self.reddit.submission(submission).mod.send_removal_message(message = self.removal_message, title = self.removal_title, type = self.removal_type)
# takes submission ID to update loggingdict if submission is not yet present
def add_to_dict(self, submission):
if submission in self.loggingdict.keys():
pass
else:
self.loggingdict.update({submission: [self.comment(submission), time.time()]})
# checks submissions in new once/minute to see if flair has yet to be set
def new_flair_check(self):
print('thread 1 running')
try:
start_time = time.time()
while True:
try:
for submission in self.reddit.subreddit(self.sub).new(limit=100):
if submission.link_flair_text == None and (time.time() - submission.created_utc) > self.time_flair and (time.time() - submission.created_utc) <= 600:
self.add_to_dict(submission)
else:
continue
time.sleep(60.0 - ((time.time() - start_time) % 60.0))
except InvalidToken:
print("Encountered Invalid Token error, resetting PRAW")
time.sleep(10)
self.reddit = None
self.reddit = praw.Reddit(username = config.username,
password = config.password,
client_id = config.id,
client_secret = config.secret,
user_agent = config.user_agent)
self.subreddit = self.reddit.subreddit(str(config.sub))
except RequestException as e:
print(e)
print("Request problem, will retry\n")
time.sleep(10)
except ResponseException as e:
print(e)
print("Server problem, will retry\n")
time.sleep(60)
except Exception as e:
print(e)
except KeyboardInterrupt:
pass
# checks submissions in loggingdict once/minute to see if flair is put
# also handles exeptions
def responses_check(self):
print('thread 2 running')
try:
start_time = time.time()
while True:
try:
for key, value in self.loggingdict.copy().items():
submission = self.reddit.submission(id=key)
currentpostflair = submission.link_flair_text
comment = value[0] #praw.models.Comment(reddit=self.reddit, id=value[0])
post_time = value[1]
if (((time.time() - post_time) >= self.time_removal) and ((time.time() - post_time) < self.time_removal + 300)):
if currentpostflair == None:
try:
self.comment_remove(comment)
except:
print('comment already removed')
try:
self.post_remove(submission)
except:
print('post already removed')
self.loggingdict.pop(submission)
else:
try:
self.comment_remove(comment)
except:
print('comment already removed')
self.loggingdict.pop(submission)
elif (time.time() - post_time) < self.time_removal:
if not(currentpostflair == None):
try:
self.comment_remove(comment)
except:
print('comment already removed')
self.loggingdict.pop(submission)
else:
continue
for key, value in self.loggingdict.items():
if (time.time() - value[1]) > 7200:
self.loggingdict.pop(key)
time.sleep(60.0 - ((time.time() - start_time) % 60.0))
except InvalidToken:
print("Encountered Invalid Token error, resetting PRAW")
time.sleep(10)
self.reddit = None
self.reddit = praw.Reddit(username = config.username,
password = config.password,
client_id = config.id,
client_secret = config.secret,
user_agent = config.user_agent)
self.subreddit = self.reddit.subreddit(str(config.sub))
except RequestException as e:
print(e)
print("Request problem, will retry\n")
time.sleep(10)
except ResponseException as e:
print(e)
print("Server problem, will retry\n")
time.sleep(60)
except Exception as e:
print(e)
except KeyboardInterrupt:
pass
# checks new for nsfw flairs, marks nsfw accordingly
def nsfwflair_check(self):
print('thread 3 running')
try:
start_time = time.time()
while True:
try:
for submission in self.reddit.subreddit(self.sub).new(limit=100):
if submission.link_flair_text != None:
if submission.over_18 == False:
flair = submission.link_flair_text
if 'NSFW' in flair:
submission.mod.nsfw()
else:
continue
else:
continue
else:
continue
time.sleep(60.0 - ((time.time() - start_time) % 60.0))
except InvalidToken:
print("Encountered Invalid Token error, resetting PRAW")
time.sleep(10)
self.reddit = None
self.reddit = praw.Reddit(username = config.username,
password = config.password,
client_id = config.id,
client_secret = config.secret,
user_agent = config.user_agent)
self.subreddit = self.reddit.subreddit(str(config.sub))
except RequestException as e:
print(e)
print("Request problem, will retry\n")
time.sleep(10)
except ResponseException as e:
print(e)
print("Server problem, will retry\n")
time.sleep(60)
except Exception as e:
print(e)
except KeyboardInterrupt:
pass
# threading to execute functions in parallel
def threading(self):
a = threading.Thread(target=self.new_flair_check, name='Thread-a', daemon=True)
b = threading.Thread(target=self.responses_check, name='Thread-b', daemon=True)
c = threading.Thread(target=self.nsfwflair_check, name='Thread-c', daemon=True)
a.start()
b.start()
c.start()
a.join()
b.join()
c.join()
if __name__ == '__main__':
bot().threading()
print('Processing time:', time.time() - now_time)