This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbox.py
53 lines (42 loc) · 1.73 KB
/
mbox.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
from email.header import Header, decode_header
def process_header(header):
val = []
if type(header) == Header:
for code, encoding in decode_header(header): # it may be many values
if encoding in (None, "unknown-8bit"):
val.append(code)
else:
val.append(code.decode(encoding))
else:
val = header
return val
def is_chat(message):
"""Check if Gmail message is chat message, not email.
Google/Hangout chats is shown in emails with label 'Chat'. Let's filter such messages."""
labels = message.get('X-Gmail-Labels', "").split(',')
return 'Chat' in labels
def walk_payload(message):
div = "\n"
if message.is_multipart():
parts = []
for part in message.walk():
content_type = part.get_content_type()
content_disposition = part.get('Content-Disposition')
maintype, _ = content_type.split('/')
if maintype == 'text' and content_disposition is None: # skip data with non 'text/*' context type
payload_str = try_decode(part)
parts.append(payload_str)
return div.join(parts)
else:
return try_decode(message)
def try_decode(msg):
charset = msg.get_content_charset('ascii') # use ascii as failobj by default.
part_payload = msg.get_payload(decode=True)
try:
payload_str = part_payload.decode(charset)
except UnicodeDecodeError: # Guess: try to decode using 'utf-8' if charset does not work
# try to decode with 'replace' error handling scheme. Most probably loose some data
payload_str = part_payload.decode('utf-8', 'ignore')
except LookupError:
payload_str = ""
return payload_str