-
Notifications
You must be signed in to change notification settings - Fork 7
/
mention_manager.py
53 lines (45 loc) · 1.51 KB
/
mention_manager.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
import re
from markdownrenderer import escape
class mentionManager:
def isStudentNumber(self, msg):
search = self.student_no_matcher.search(msg)
return search if search is None else search.group()
def __init__(self, db, cow_bot):
self.student_no_matcher = re.compile("e?\d{6,7}")
self.db = db
self.cow_bot = cow_bot
self.mention_text = "Your alias *{}* has been mentioned in " + \
"newsgroup: *{}* with header: *{}* at line: {}\."
def sendMention(self, cid, alias, newsgroup, header, line_no):
return self.cow_bot.sendMsg(
cid,
self.mention_text.format(
escape(alias), escape(newsgroup), escape(header), line_no),
escaped=True)
def getMinimalStudentNo(self, student_no):
base = student_no
if base[0] == "e":
base = base[1:]
base = base[:6]
return base
def parseMentions(self, content, newsgroup):
current_header = ""
line_no = 0
for raw_line in content.split("\n"):
line = raw_line.strip()
if line.startswith("> "):
continue
student_no = self.isStudentNumber(line)
if student_no is not None:
cids = self.db.checkForAlias(self.getMinimalStudentNo(student_no))
if cids is None:
continue
header = current_header
if len(line) > len(student_no):
header = line
for cid in cids:
self.sendMention(cid, student_no, newsgroup, header, line_no)
else:
current_header = line
line_no = 0
line_no += 1