-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
62 lines (54 loc) · 1.71 KB
/
parse.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
#!/usr/bin/env python
# encoding: utf-8
import re
def split_content(content):
chunks = {
'addresses': [],
'subject': "",
'body': "<html> <head> </head> <body>"
}
# Parser can be equal to 'mailto', 'subject' or 'body'
parser = ""
for line in content.splitlines():
# print("line: " + line)
if "mailto:" in line:
parser = "mailto"
elif "subject:" in line:
parser = "subject"
elif "body:" in line:
parser = "body"
elif not line:
continue
if parser == "mailto":
# print("mailto")
# Split the content to take the email addresses but the last which contains the message
addresses = re.split("mailto:", line.strip())
# Build the list and discard the first empty chunk
if len(addresses) == 2:
addresses = re.split("; ", addresses[1])
else:
addresses = re.split("; ", addresses[0])
for i in addresses:
# print("email: " + i)
# print(chunks['addresses'])
chunks['addresses'].append(i.strip())
elif parser == "subject":
# print("Subject")
# Parse the subject
subject = re.split("subject:", line.strip())
if len(subject) == 2:
chunks['subject'] = chunks['subject'] + subject[1] + " "
else:
chunks['subject'] = chunks['subject'] + subject[0] + " "
elif parser == "body":
# print("body")
# Parse the body
body = re.split("body:", line.strip())
if len(body) == 2:
chunks['body'] = chunks['body'] + body[1] + "<br>"
else:
chunks['body'] = chunks['body'] + body[0] + "<br>"
chunks['body'] = chunks['body'] + "</body> </html>"
# print("Result: ")
# print(chunks)
return chunks