forked from motherapp/weight-csv-to-gfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_weight_csv.py
48 lines (42 loc) · 1.44 KB
/
read_weight_csv.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
import csv
import datetime
import dateutil.tz
import dateutil.parser
from dateutil import zoneinfo
DAWN_TIME = datetime.datetime(1970, 1, 1, tzinfo=dateutil.tz.tzutc())
TIME_ZONE = dateutil.tz.tzlocal
def nano(val):
"""Converts a number to nano (str)."""
return '%d' % (int(val) * 1e9)
def epoch_of_time_str(dateTimeStr, tzinfo):
log_time = dateutil.parser.parse(dateTimeStr).replace(tzinfo=tzinfo)
return (log_time - DAWN_TIME).total_seconds()
def read_weights_csv():
print "Reading Weights"
is_header = True
weights = []
with open('weights.csv', 'rb') as csvfile:
weights_reader = csv.reader(csvfile, delimiter=';')
for row in weights_reader:
if is_header:
is_header=False
continue
weights.append(dict(
seconds_from_dawn=epoch_of_time_str(row[0], TIME_ZONE),
weight=float(row[1])
))
return weights
def read_weights_csv_with_gfit_format():
weights = read_weights_csv()
gfit_weights = []
for weight in weights:
gfit_weights.append(dict(
dataTypeName='com.google.weight',
endTimeNanos=nano(weight["seconds_from_dawn"]),
startTimeNanos=nano(weight["seconds_from_dawn"]),
value=[dict(fpVal=weight["weight"])],
))
return gfit_weights
if __name__=="__main__":
gfit_weights = read_weights_csv_with_gfit_format()
print gfit_weights[0]