forked from Nihilate/YugiohLinkBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestHandler.py
51 lines (40 loc) · 1.68 KB
/
RequestHandler.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
import re
import traceback
from CommentBuilder import buildRequestComment
from CommentBuilder import getSignature
class RequestHandler(object):
# Normal - e.g. {Blue-Eyes White Dragon}
normalRequestQuery = re.compile("(?<=(?<!\{)\{)([^{}]*)(?=\}(?!\}))")
# Expanded - e.g. {{Blue-Eyes White Dragon}}
expandedRequestQuery = re.compile("\{{2}([^}]*)\}{2}")
def __init__(self):
pass
def getNormalRequests(self, comment):
return self.normalRequestQuery.findall(comment)
def getExpandedRequests(self, comment):
return self.expandedRequestQuery.findall(comment)
def buildResponse(self, comment):
try:
reply = ''
normalRequests = self.getNormalRequests(comment)
expandedRequests = self.getExpandedRequests(comment)
#If there are 10 or more expanded requests, turn them all into normal requests
#Reddit has a 10k character limit
if (len(normalRequests) + len(expandedRequests)) >= 8:
normalRequests.extend(expandedRequests)
expandedRequests = []
for card in normalRequests:
requestComment = buildRequestComment(card, False)
if requestComment:
reply += requestComment + '\n\n---\n\n'
for card in expandedRequests:
requestComment = buildRequestComment(card, True)
if requestComment:
reply += requestComment + '\n\n---\n\n'
if reply:
reply += getSignature()
return reply
else:
return None
except:
traceback.print_exc()