-
Notifications
You must be signed in to change notification settings - Fork 4
/
YugiohLinkBot.py
85 lines (66 loc) · 3.36 KB
/
YugiohLinkBot.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
import praw
import re
import time
import pprint
import traceback
import Config
import DatabaseHandler
from RequestHandler import RequestHandler
from SubmissionProcessor import SubmissionProcessor
class YugiohLinkBot(object):
def __init__(self, subredditList):
self.subredditList = subredditList
self.requestHandler = RequestHandler()
#Set up reddit
self.reddit = praw.Reddit(Config.useragent)
self.reddit.set_oauth_app_info(client_id=Config.appid, client_secret=Config.appsecret, redirect_uri=Config.redirecturi)
self.reddit.refresh_access_information(Config.refreshtoken)
print("Connected to Reddit")
self.submissionProcessor = SubmissionProcessor(self.reddit, subredditList, self.requestHandler)
self.submissionsLastProcessed = 0
self.updateTime = time.time()
def run(self):
try:
print("Starting stream")
commentStream = praw.helpers.comment_stream(self.reddit, self.subredditList, limit=1000, verbosity=0)
for comment in commentStream:
if ((time.time() - self.updateTime) > Config.tcgUpdateInterval * 60 * 60):
DatabaseHandler.updateTCGCardlist()
self.updateTime = time.time()
if ((time.time() - self.submissionsLastProcessed) > Config.submissionProcessingInterval * 60 * 60):
self.submissionProcessor.processSubmissions(100)
self.submissionsLastProcessed = time.time()
#print("Found comment")
#If we've already seen this comment, ignore it
if DatabaseHandler.commentExists(comment.id):
continue
#If the post has been deleted, getting the author will return an error
try:
author = comment.author.name
except Exception as e:
continue
#If this is one of our own comments, ignore it
if (author == 'YugiohLinkBot'):
continue
reply = self.requestHandler.buildResponse(comment.body)
try:
if reply:
cards = re.findall('\[\*\*(.+?)\*\*\]\(', reply)
for card in cards:
DatabaseHandler.addRequest(card, author, comment.subreddit)
if("VENT THREAD" in comment.link_title):
reply = self.submissionProcessor.convertCase(True, reply)
elif("happiness thread" in comment.link_title):
reply = self.submissionProcessor.convertCase(False, reply)
DatabaseHandler.addComment(comment.id, author, comment.subreddit, True)
comment.reply(reply)
print("Comment made.\n")
else:
if ('{' in comment.body and '}' in comment.body):
print('')
DatabaseHandler.addComment(comment.id, author, comment.subreddit, False)
except Exception as e:
print("Reddit probably broke when replying:" + str(e) + '\n')
except Exception as e:
print(e)
pass