forked from Nihilate/YugiohLinkBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmissionProcessor.py
71 lines (56 loc) · 2.61 KB
/
SubmissionProcessor.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
import DatabaseHandler
import re
import traceback
class SubmissionProcessor(object):
def __init__(self, reddit, subredditList, requestHandler):
self.reddit = reddit
self.subredditList = subredditList
self.requestHandler = requestHandler
def processSubmissions(self, num):
subreddits = self.reddit.get_subreddit(self.subredditList)
for submission in subreddits.get_new(limit=num):
#If we've already seen this submission, ignore it
if DatabaseHandler.commentExists(submission.id):
continue
#If the post has been deleted, getting the author will return an error
try:
author = submission.author.name
except Exception as e:
continue
#If this is one of our own submissions, ignore it
if (author == 'YugiohLinkBot'):
continue
reply = self.requestHandler.buildResponse(submission.selftext)
try:
if reply:
cards = re.findall('\[\*\*(.+?)\*\*\]\(', reply)
for card in cards:
DatabaseHandler.addRequest(card, author, submission.subreddit)
if("VENT THREAD" in submission.title):
reply = self.convertCase(True, reply)
elif("happiness thread" in submission.title):
reply = self.convertCase(False, reply)
DatabaseHandler.addComment(submission.id, author, submission.subreddit, True)
submission.add_comment(reply)
print("Comment made.\n")
else:
if ('{' in submission.selftext and '}' in submission.selftext):
print('')
DatabaseHandler.addComment(submission.id, author, submission.subreddit, False)
except Exception as e:
traceback.print_exc()
print("Reddit probably broke when replying:" + str(e) + '\n')
def convertCase(self, capitalise, reply):
links = []
#find and store all links using regex
for match in re.finditer("\((http:[^)]*)\)", reply, re.S):
links.append(match.group(1))
#change the case
if (capitalise == True):
reply = reply.upper()
else:
reply = reply.lower()
#replace the links with the original ones we stored
for idx, match in enumerate(re.finditer("\((http:[^)]*)\)", reply, re.I)):
reply = reply.replace(match.group(1), links[idx])
return reply