-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
350 lines (265 loc) · 12.1 KB
/
main.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
import logging
import operator
import os
import sqlite3 as db
from sqlite3 import OperationalError
from socket import timeout
import sys
from time import sleep
from praw.errors import *
from requests.exceptions import HTTPError
from crawler import SubredditAnalysis
from exceptions import *
def login(username, password):
for i in range(0, 3):
try:
myBot.login(username, password)
break
except (InvalidUser, InvalidUserPass, RateLimitExceeded, APIException) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
sys.exit(1)
except (ConnectionResetError, HTTPError, timeout) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
if i == 2:
print("Failed to login.")
sys.exit(1)
else:
# wait a minute and try again
sleep(60)
continue
def check_subreddits(subredditList):
"""
Checks on the listed subreddits to make sure that they are
valid subreddits and that there's no typos and whatnot. This
function removes the bad subreddits from the list so the bot
can carry on with its task. Feed it the list of subreddits.
"""
for i in range(0, 3):
try:
for subreddit in subredditList:
if(subreddit in ["quit", ".quit", 'q']):
continue
print("Verifying /r/{0}...".format(subreddit))
try:
# make sure the subreddit is valid
testSubmission = myBot.client.get_subreddit(subreddit).get_new(limit=1)
for submission in testSubmission:
"".join(submission.title)
except (InvalidSubreddit, RedirectException) as e:
myBot.add_msg(e)
logging.error("Invalid subreddit. Removing from list." + str(e) + "\n\n")
subredditList.remove(subreddit)
raise SkipThis("Skipping invalid subreddit...")
except (ConnectionResetError, HTTPError, timeout) as e:
myBot.add_msg(e)
logging.error(str(subreddit) + ' ' + str(e) + "\n\n")
# private subreddits return a 403 error
if "403" in str(e):
myBot.add_msg("/r/{0} is private. Removing from list...".format(subreddit))
subredditList.remove(subreddit)
continue
# banned subreddits return a 404 error
if "404" in str(e):
myBot.add_msg("/r/{0} banned. Removing from list...".format(subreddit))
subredditList.remove(subreddit)
continue
myBot.add_msg("Waiting a minute to try again...")
sleep(60)
raise SkipThis("Trying again...")
except (APIException, ClientException, Exception) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Something went wrong. Skipping...")
break
except SkipThis:
if i == 2:
sys.exit(1)
else:
continue
try:
# keeps this message from being displayed when
# the only item is a quit command
if subredditList[0] not in ["quit", ".quit", 'q']:
print("Subreddit verification completed.")
except IndexError:
print("Subreddit List empty.")
def main():
# login credentials
username = myBot.config.login.username
password = myBot.config.login.password
print("Welcome to Reddit Analysis Bot.")
print("Type \"quit\", \".quit\", or \'q\' to exit the program.")
login(username, password)
while True:
try:
# list of subreddits you want to analyze
drilldownList = raw_input("Enter the subreddits you wish to target.~/> ").split()
except NameError:
# python 3 support
drilldownList = input("Enter the subreddits you wish to target.~/> ").split()
# check to make sure each subreddit is valid
check_subreddits(drilldownList)
# iterate through the drilldownList to get data
for subreddit in drilldownList:
# check to see if a drilldown for this subreddit
# was already done
dbFile = "{0}.db".format(subreddit)
if(subreddit in ["quit", ".quit", 'q']):
print("Quitting...")
sys.exit(0)
elif(os.path.isfile("subreddits/{0}".format(dbFile))):
con = db.connect("subreddits/{0}".format(dbFile))
cur = con.cursor()
sub = (subreddit,)
cur.execute("SELECT users FROM drilldown WHERE overlaps=?", sub)
for element in cur:
userList = operator.getitem(element, 0)
try:
# format the data for Reddit
text = myBot.format_post(subreddit, userList)
except Exception as e:
myBot.add_msg(e)
logging.error("Failed to format post. " + str(e) + "\n\n")
continue
try:
while True:
try:
# submit the post for Reddit
post = myBot.submit_post(subreddit, text)
break
except (ConnectionResetError, HTTPError, timeout) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
myBot.add_msg("Waiting to try again...")
sleep(60)
continue
except (APIException, ClientException, Exception) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Something went wrong. Skipping...")
except SkipThis:
logging.error(str(e) + "\n\n")
myBot.log_post(subreddit, text)
continue
if(post != None):
try:
try:
myBot.give_flair(post, subreddit)
except (APIException, ClientException, Exception) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Couldn't flair post. Skipping...")
except SkipThis:
continue
con.close()
continue
else:
try:
while True:
# get the list of users
try:
userList = myBot.get_users(subreddit)
myBot.userList = []
break
except (InvalidSubreddit, RedirectException) as e:
myBot.add_msg(e)
logging.error("Invalid subreddit. Removing from list." + str(e) + "\n\n")
drilldownList.remove(subreddit)
raise SkipThis("Skipping invalid subreddit...")
except (APIException, ClientException, Exception) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Couldn't get users. Skipping...")
except SkipThis:
continue
for user in userList:
myBot.log_info(user + ',')
myBot.log_info("\n\n")
try:
while True:
try:
# get the list of subreddits
subredditList = myBot.get_subs(userList)
myBot.subredditList = []
break
except (APIException, ClientException, OperationalError) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Couldn't get overlapping subreddits. Skipping...")
except SkipThis:
continue
for sub in subredditList:
myBot.log_info(sub + ',')
myBot.log_info("\n\n")
try:
# get the list of tuples
subredditTuple = myBot.create_tuples(subreddit, subredditList)
for item in subredditTuple:
myBot.log_info(item)
myBot.log_info(',')
myBot.log_info("\n\n")
except Exception as e:
myBot.add_msg(e)
logging.error("Failed to create tuples. " + str(e) + "\n\n")
continue
try:
myBot.add_db(subreddit, subredditTuple, len(userList))
except Exception as e:
myBot.add_msg(e)
logging.error("Failed to add to database. " + str(e) + "\n\n")
continue
try:
# format the data for Reddit
text = myBot.format_post(subreddit, userList)
except Exception as e:
myBot.add_msg(e)
logging.error("Failed to format post. " + str(e) + "\n\n")
continue
try:
while True:
try:
# submit the post for Reddit
post = myBot.submit_post(subreddit, text)
break
except (ConnectionResetError, HTTPError, timeout) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
myBot.add_msg("Waiting to try again...")
sleep(60)
continue
except (APIException, ClientException, Exception) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Couldn't submit post. Skipping...")
except SkipThis:
logging.error(str(e) + "\n\n")
myBot.log_post(subreddit, text)
continue
if(post != None):
try:
try:
print("Setting post's flair...")
myBot.give_flair(post, subreddit)
except ModeratorRequired as e:
myBot.add_msg(e)
logging.error("Failed to set flair. " + str(e) + '\n' + str(post.permalink) + "\n\n")
raise SkipThis("Need moderator privileges to set flair. Skipping...")
except (APIException, ClientException, Exception) as e:
myBot.add_msg(e)
logging.error(str(e) + "\n\n")
raise SkipThis("Couldn't set flair. Skipping...")
except SkipThis:
continue
if __name__ == "__main__":
myBot = SubredditAnalysis()
if(myBot.errorLogging):
logging.basicConfig(
filename="SubredditAnalysis_logerr.log",
filemode='a', format="%(asctime)s\nIn "
"%(filename)s (%(funcName)s:%(lineno)s): "
"%(message)s", datefmt="%Y-%m-%d %H:%M:%S",
level=logging.ERROR
)
main()