-
Notifications
You must be signed in to change notification settings - Fork 16
/
health_to_fitbit.py
executable file
·175 lines (133 loc) · 5.02 KB
/
health_to_fitbit.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# Script to convert Apple Health data to Fitbit CSV data
import os.path
import xml.etree.ElementTree as ET
from datetime import datetime
if not os.path.exists('export_cda.xml'):
print("Error: export_cda.xml not found.")
exit(1)
if not os.path.exists('export.xml'):
print("Error: export.xml not found.")
exit(1)
height_cm_input = input("What is your height in cm? ")
try:
height_cm = int(height_cm_input)
height_m = float(height_cm) / 100.0
except:
print("Unable to parse input.")
exit(1)
print("OK. Parsing files...")
try:
export_cda = ET.parse('export_cda.xml')
except ET.ParseError as error:
print("Unable to parse 'export_cda.xml', it might be invalid XML.")
print("Try to fix 'export_cda.xml' by running the included 'fix_invalid_export_cda_xml' script.")
print("Exiting")
exit(1)
try:
export = ET.parse('export.xml')
except ET.ParseError as error:
print("Failed to parse 'export.xml'.")
print("Exiting")
exit(1)
export_cda_root = export_cda.getroot()
export_root = export.getroot()
# Go through export_cda.xml to get all weight values
weight_dict = {}
for entry in export_cda_root.findall('{urn:hl7-org:v3}entry'):
for organizer in entry.findall('{urn:hl7-org:v3}organizer'):
for component in organizer.findall('{urn:hl7-org:v3}component'):
observation = component.find('{urn:hl7-org:v3}observation')
code = observation.find('{urn:hl7-org:v3}code').get('code')
if(code == "3141-9"):
value = observation.find('{urn:hl7-org:v3}value').get('value')
effective_time = observation.find('{urn:hl7-org:v3}effectiveTime').find('{urn:hl7-org:v3}low')
time_value = datetime.strptime(effective_time.get('value'), '%Y%m%d%H%M%S%z')
date_string = time_value.strftime('%d-%m-%Y')
weight_dict[date_string] = value
# Go through export.xml to get all activities, steps, distance, floors climbed
steps_dict = {}
distance_dict = {}
floors_dict = {}
# Helper function to parse int
def parse_to_int(s):
try:
return int(s)
except ValueError:
return int(float(s))
for record in export_root.findall('Record'):
start_date = datetime.strptime(record.get('startDate'), '%Y-%m-%d %H:%M:%S %z')
date_string = start_date.strftime('%d-%m-%Y')
value = record.get('value')
# Aggregate the data by calculating the sum for each date
if(record.get('type') == "HKQuantityTypeIdentifierStepCount"):
if date_string in steps_dict:
steps_dict[date_string] = steps_dict[date_string] + parse_to_int(value)
else:
steps_dict[date_string] = parse_to_int(value)
if(record.get('type') == "HKQuantityTypeIdentifierDistanceWalkingRunning"):
if date_string in distance_dict:
distance_dict[date_string] = float(distance_dict[date_string]) + float(value)
else:
distance_dict[date_string] = float(value)
if(record.get('type') == "HKQuantityTypeIdentifierFlightsClimbed"):
if date_string in floors_dict:
floors_dict[date_string] = floors_dict[date_string] + parse_to_int(value)
else:
floors_dict[date_string] = parse_to_int(value)
# Find out which years we need to print
# All dict keys are formated with "time_value.strftime('%d-%m-%Y')"
years = []
for date_key in weight_dict:
tmp_year = datetime.strptime(date_key, '%d-%m-%Y').strftime('%Y')
if tmp_year not in years:
print("Found weight data for " + tmp_year)
years.append(tmp_year)
for date_key in steps_dict:
tmp_year = datetime.strptime(date_key, '%d-%m-%Y').strftime('%Y')
if tmp_year not in years:
print("Found step data for " + tmp_year)
years.append(tmp_year)
print("Now generating FitBit CSV files for the following years: " + ', '.join(years))
for file_year in years:
filename = "fitbit_" + file_year + ".csv"
print("Writing " + filename + "...")
with open(filename, 'w') as output_file:
# Print weight
# Header
output_file.write("Body\n")
output_file.write("Date,Weight,BMI,Fat\n")
# Data
for date_key in weight_dict:
dict_year = datetime.strptime(date_key, '%d-%m-%Y').strftime('%Y')
if(dict_year == file_year):
value = weight_dict[date_key]
bmi = round(float(value) / (height_m * height_m),2)
output_file.write("\"%s\",\"%s\",\"%s\",\"0\"\n" % (date_key, value, bmi))
# Print activities
# Header
output_file.write("\n")
output_file.write("Activities\n")
output_file.write("Date,Calories Burned,Steps,Distance,Floors,Minutes Sedentary,Minutes Lightly Active,Minutes Fairly Active,Minutes Very Active,Activity Calories\n")
# Data
for date_key in steps_dict:
dict_year = datetime.strptime(date_key, '%d-%m-%Y').strftime('%Y')
if(dict_year == file_year):
output = "\""
output += date_key
output += "\",\"0\",\""
if date_key in steps_dict:
output += str("{:,}".format(steps_dict[date_key]))
output += "\",\""
if date_key in distance_dict:
output += str(round(distance_dict[date_key],2))
else:
output += "0"
output += "\",\""
if date_key in floors_dict:
output += str(floors_dict[date_key])
else:
output += "0"
output += "\",\"0\",\"0\",\"0\",\"0\",\"0\"\n"
output_file.write(output)
print("Done.")