-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_csv_files.py
executable file
·97 lines (75 loc) · 2.58 KB
/
generate_csv_files.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
#!/usr/bin/env python3
import argparse
import csv
import json
from os import listdir
from os.path import isfile, join
import datetime
def weightdata(path):
"""
Given the path to a full-account Fitbit dump,
load all the weight files and yield the data
records in them.
"""
weightfiles = [ join(path, f) for f in listdir(path)
if isfile(join(path, f))
if f.startswith('weight') ]
weightfiles.sort()
for file in weightfiles:
with open(file, 'r') as f:
data = json.load(f)
for d in data:
yield d
def build_row(data):
"""
Given a data record from the fitbit weight data dump,
assemble a row of data suitable for the Withings import
csv file.
"""
date = datetime.datetime.strptime('{} {}'.format(data['date'], data['time']), '%m/%d/%y %H:%M:%S')
weight = data['weight']
if 'fat' in data:
fatmass = round(data['fat'] / 100 * data['weight'], 1)
else:
fatmass = ''
return [date, weight, fatmass]
def write_to_file(file_counter, file_data):
with open('weight_data{}.csv'.format(file_counter), 'w', newline='') as csv_handle:
writer = csv.writer(csv_handle)
writer.writerows(file_data)
def init_loop_state(file_counter=0):
"""
Initialize the file row counter, the file counter,
and the list representing file data.
Janky, I know, but this needed to be done in 2 spots.
"""
file_row_counter = 0
file_counter += 1
file_data = []
file_data.append([
'Date',
'Weight (lb)',
'Fat mass (lb)'
])
return (file_row_counter, file_counter, file_data)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('path', help='path to folder containing fitbit data to convert')
args = parser.parse_args()
user_site_export_path = args.path
(file_row_counter, file_counter, file_data) = init_loop_state()
for data in weightdata(user_site_export_path):
# Skip any data that fitbit got from withings
if 'source' in data and data['source'] == 'Withings':
continue
file_row_counter += 1
row = build_row(data)
file_data.append(row)
# Withings wants files limited to 300 lines. Including the
# column header line, we'll cap at 299 data rows.
if file_row_counter >=299:
write_to_file(file_counter, file_data)
(file_row_counter, file_counter, file_data) = init_loop_state(file_counter)
write_to_file(file_counter, file_data)
if __name__ == "__main__":
main()