-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
107 lines (89 loc) · 3.45 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# some parts taken from: https://gist.github.com/baali/2633554
import email
import imaplib
import cStringIO
import csv
import pprint
import argparse
import json
import os
def parse_attachement(messageParts):
message_body = messageParts[0][1]
mail = email.message_from_string(message_body)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
# print part.as_string()
continue
if part.get('Content-Disposition') is None:
# print part.as_string()
continue
return part.get_payload(decode=True)
def connect_imap(imap_host, imap_user, imap_pass):
s = imaplib.IMAP4_SSL(imap_host)
s.login(imap_user, imap_pass)
return s
def get_messages(imap_server, search_string):
imap_server.select('Inbox')
typ, data = imap_server.search(None, search_string)
return typ, data
def seperate_datatypes(csvdata):
def reconstruct_string(l):
_l = ''
for line in l:
_l = _l + line
return _l
header = []
daily_summary = []
detailed_usage = []
a = header
for line in cStringIO.StringIO(csvdata):
if "Exported Data for WeMo Insight" in line:
a = header
elif "Daily Usage Summary" in line:
a = daily_summary
elif "Energy Data" in line:
a = detailed_usage
else:
a.append(line)
header = reconstruct_string(header)
daily_summary = reconstruct_string(daily_summary)
detailed_usage = reconstruct_string(detailed_usage)
return header, daily_summary, detailed_usage
def parse_csv(csvbody):
_l = []
data = csv.DictReader(cStringIO.StringIO(csvbody))
for row in data:
_l.append(row)
return _l
def update_db(dbname, data, dirpath):
filename = os.path.join(dirpath,'db_{}.json'.format(dbname))
if not os.path.isfile(filename):
with open (filename, 'w') as f:
f.write('[]')
with open(filename, 'r') as f:
_data = json.load(f)
_data.append(data)
with open(filename, 'w') as f:
json.dump(_data, f)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Tool to fetch wemo data export emails and parse them')
parser.add_argument("--imap_host", help="IMAP host to connect to", required=True)
parser.add_argument("--imap_user", help="IMAP username", required=True)
parser.add_argument("--imap_password", help="IMAP password", required=True)
parser.add_argument("--db_dir", help="Directory to write database files to", required=False,
default=os.path.expanduser("~"))
parser.add_argument("--print_output", help="Print output to stdout?", action='store_true', required=False,
default=False)
parser.add_argument("--email_search", help="search string",
required=False, default='(From [email protected])')
args = parser.parse_args()
imapSession = connect_imap(args.imap_host, args.imap_user, args.imap_password)
typ, data = get_messages(imapSession, args.email_search)
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
data = parse_attachement(messageParts)
if args.print_output:
print data
header, daily_summary, detailed_usage = seperate_datatypes(data)
update_db("daily_summary", parse_csv(daily_summary), args.db_dir)
update_db("detailed_usage", parse_csv(detailed_usage), args.db_dir)