-
Notifications
You must be signed in to change notification settings - Fork 1
/
flairbot_all_messages.py
183 lines (140 loc) · 5.95 KB
/
flairbot_all_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
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.removal_message = config.removal_message
self.removal_title = config.removal_title
self.removal_type = ('public')
self.loggingdict = {}
# takes user id, sends reminder message
def flair_message(self, author, submission):
user = author
sm = self.reddit.submission(id=submission)
self.reddit.redditor(user).message(subject='No flair', message='[Your post](' + 'https://www.reddit.com/r/' + self.sub + '/comments/' + str(sm.id) + ') does not have a flair and will soon be removed. Please add a flair within ' + str(int(self.time_removal/60)) + ' minutes or your post will be removed. \n\n *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/' + self.sub + ') if you have any questions or concerns.*', from_subreddit=self.reddit.subreddit(self.sub))
# takes user id, sends removal message
def remove_message(self, author, submission):
user = author
sm = self.reddit.submission(id=submission)
self.reddit.redditor(user).message(subject='Post removal', message='[Your post](' + 'https://www.reddit.com/r/' + self.sub + '/comments/' + str(sm.id) + ') was removed because a flair was not set in time. \n\n *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/' + self.sub + ') if you have any questions or concerns.*', from_subreddit=self.reddit.subreddit(self.sub))
# takes submission ID to remove the submission
def post_remove(self, submission):
self.reddit.submission(submission).mod.remove()
self.remove_message(submission.author.name, submission)
# 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.flair_message(submission.author.name, submission)
self.loggingdict.update({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
post_time = value
if (((time.time() - post_time) >= self.time_removal) and ((time.time() - post_time) < self.time_removal + 300)):
if currentpostflair == None:
try:
self.post_remove(submission)
except:
print('post already removed')
self.loggingdict.pop(submission)
else:
self.loggingdict.pop(submission)
elif (time.time() - post_time) < self.time_removal:
if not(currentpostflair == None):
self.loggingdict.pop(submission)
else:
continue
for key, value in self.loggingdict.items():
if (time.time() - value) > 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
# 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)
a.start()
b.start()
a.join()
b.join()
if __name__ == '__main__':
bot().threading()
print('Processing time:', time.time() - now_time)