-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_parser.py
45 lines (37 loc) · 1.37 KB
/
my_parser.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
import mailparser
import email
import re
def get_body_string(mail):
if isinstance(mail, email.message.Message):
return mailparser.parse_from_string(mail.as_string()).body
def get_summary(mail):
body_string = get_body_string(mail)
# Because of how the subject is hard formatted,
# First instance of 'Summary' is what we always be looking for
summary_match = re.search(r"Summary[\s,\n]*", body_string)
if summary_match:
end = body_string.find("\n", summary_match.end())
if end > summary_match.end():
return body_string[summary_match.end():end]
else:
return "Summary not found correctly"
else:
return "Summary not found"
def get_cid(mail):
# \n\n\nContact Information
# We'll just search for 2 new lines before Contact Information and then whitespace followed by Customer
body_string = get_body_string(mail)
ci = re.search(r"Contact Information[\s,\n]*Customer", body_string)
if ci:
ci_string = body_string[ci.end():]
company_id = re.search(r"Cost Center[\s,\n]*", ci_string)
if company_id:
return ci_string[company_id.end():company_id.end() + 3]
else:
return "Center ID Not Found"
def parse_subject_for_slack(str):
ret_val = re.search(r" is", str)
if ret_val:
return str[0:ret_val.start()]
else:
return str