-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_gmail.py
More file actions
executable file
·66 lines (52 loc) · 1.81 KB
/
count_gmail.py
File metadata and controls
executable file
·66 lines (52 loc) · 1.81 KB
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
61
62
63
64
65
66
#!/usr/bin/env python
import mailbox
import rfc822
import email
import time
import sys
import os.path
import logging
md_name = os.path.expanduser(sys.argv[1])
inbox = mailbox.Maildir(md_name, factory=None)
def cls_mail(mail_str):
if mail_str == None:
mail_str = "NA"
else:
mail_str = email.utils.parseaddr(mail_str)[1]
return mail_str
for intermsg in inbox.items():
#flags = msg.get_flags()
#date = msg.get_date()
msg = intermsg[1]
date = msg.get("Date")
if date == None:
logging.warning('No Date! Key: %s', intermsg[0]) # will print a message to the console
continue
else:
date = email.utils.parsedate(date)
if date == None:
logging.warning('No Date, after utils parsing! Key: %s', intermsg[0]) # will print a message to the console
continue
else:
date = time.mktime(date)
#delivered = cls_mail(msg.get("Delivered-To"))
#sender = cls_mail(msg.get("Sender"))
sender= cls_mail(msg.get("From"))
to = cls_mail(msg.get("To"))
try:
sender_domain = '.'.join(sender.split("@")[1].split(".")[-2:])
except:
sender_domain = "NA"
logging.warning('Split for Sender doomain failed. Key: %s', intermsg[0]) # will print a message to the console
try:
to_domain = '.'.join(to.split("@")[1].split(".")[-2:])
except:
to_domain = "NA"
logging.warning('Split for To doomain failed. Key: %s', intermsg[0]) # will print a message to the console
try:
msg_size = intermsg[0].split(",")[1].split("=")[1]
except:
msg_size = "NA"
logging.warning('Split for message size failed. Key: %s', intermsg[0]) # will print a message to the console
# put it together and output
print("\t".join([str(date), to, sender, to_domain, sender_domain, msg_size]))