forked from tdda/applehealthdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
applehealthdata.py
246 lines (207 loc) · 7.33 KB
/
applehealthdata.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: utf-8 -*-
"""
applehealthdata.py: Extract data from Apple Health App's export.xml.
Copyright (c) 2016 Nicholas J. Radcliffe
Licence: MIT
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import re
import sys
from xml.etree import ElementTree
from collections import Counter, OrderedDict
__version__ = '1.3'
RECORD_FIELDS = OrderedDict((
('sourceName', 's'),
('sourceVersion', 's'),
('device', 's'),
('type', 's'),
('unit', 's'),
('creationDate', 'd'),
('startDate', 'd'),
('endDate', 'd'),
('value', 'n'),
))
ACTIVITY_SUMMARY_FIELDS = OrderedDict((
('dateComponents', 'd'),
('activeEnergyBurned', 'n'),
('activeEnergyBurnedGoal', 'n'),
('activeEnergyBurnedUnit', 's'),
('appleExerciseTime', 's'),
('appleExerciseTimeGoal', 's'),
('appleStandHours', 'n'),
('appleStandHoursGoal', 'n'),
))
WORKOUT_FIELDS = OrderedDict((
('sourceName', 's'),
('sourceVersion', 's'),
('device', 's'),
('creationDate', 'd'),
('startDate', 'd'),
('endDate', 'd'),
('workoutActivityType', 's'),
('duration', 'n'),
('durationUnit', 's'),
('totalDistance', 'n'),
('totalDistanceUnit', 's'),
('totalEnergyBurned', 'n'),
('totalEnergyBurnedUnit', 's'),
))
FIELDS = {
'Record': RECORD_FIELDS,
'ActivitySummary': ACTIVITY_SUMMARY_FIELDS,
'Workout': WORKOUT_FIELDS,
}
PREFIX_RE = re.compile('^HK.*TypeIdentifier(.+)$')
ABBREVIATE = True
VERBOSE = True
def format_freqs(counter):
"""
Format a counter object for display.
"""
return '\n'.join('%s: %d' % (tag, counter[tag])
for tag in sorted(counter.keys()))
def format_value(value, datatype):
"""
Format a value for a CSV file, escaping double quotes and backslashes.
None maps to empty.
datatype should be
's' for string (escaped)
'n' for number
'd' for datetime
"""
if value is None:
return ''
elif datatype == 's': # string
return '"%s"' % value.replace('\\', '\\\\').replace('"', '\\"')
elif datatype in ('n', 'd'): # number or date
return value
else:
raise KeyError('Unexpected format value: %s' % datatype)
def abbreviate(s, enabled=ABBREVIATE):
"""
Abbreviate particularly verbose strings based on a regular expression
"""
m = re.match(PREFIX_RE, s)
return m.group(1) if enabled and m else s
def encode(s):
"""
Encode string for writing to file.
In Python 2, this encodes as UTF-8, whereas in Python 3,
it does nothing
"""
return s.encode('UTF-8') if sys.version_info.major < 3 else s
class HealthDataExtractor(object):
"""
Extract health data from Apple Health App's XML export, export.xml.
Inputs:
path: Relative or absolute path to export.xml
verbose: Set to False for less verbose output
Outputs:
Writes a CSV file for each record type found, in the same
directory as the input export.xml. Reports each file written
unless verbose has been set to False.
"""
def __init__(self, path, verbose=VERBOSE):
self.in_path = path
self.verbose = verbose
self.directory = os.path.abspath(os.path.split(path)[0])
with open(path) as f:
self.report('Reading data from %s . . . ' % path, end='')
self.data = ElementTree.parse(f)
self.report('done')
self.root = self.data._root
self.nodes = list(self.root)
self.n_nodes = len(self.nodes)
self.abbreviate_types()
self.collect_stats()
def report(self, msg, end='\n'):
if self.verbose:
print(msg, end=end)
sys.stdout.flush()
def count_tags_and_fields(self):
self.tags = Counter()
self.fields = Counter()
for record in self.nodes:
self.tags[record.tag] += 1
for k in record.keys():
self.fields[k] += 1
def count_record_types(self):
"""
Counts occurrences of each type of (conceptual) "record" in the data.
In the case of nodes of type 'Record', this counts the number of
occurrences of each 'type' or record in self.record_types.
In the case of nodes of type 'ActivitySummary' and 'Workout',
it just counts those in self.other_types.
The slightly different handling reflects the fact that 'Record'
nodes come in a variety of different subtypes that we want to write
to different data files, whereas (for now) we are going to write
all Workout entries to a single file, and all ActivitySummary
entries to another single file.
"""
self.record_types = Counter()
self.other_types = Counter()
for record in self.nodes:
if record.tag == 'Record':
self.record_types[record.attrib['type']] += 1
elif record.tag in ('ActivitySummary', 'Workout'):
self.other_types[record.tag] += 1
elif record.tag in ('Export', 'Me'):
pass
else:
self.report('Unexpected node of type %s.' % record.tag)
def collect_stats(self):
self.count_record_types()
self.count_tags_and_fields()
def open_for_writing(self):
self.handles = {}
self.paths = []
for kind in (list(self.record_types) + list(self.other_types)):
path = os.path.join(self.directory, '%s.csv' % abbreviate(kind))
f = open(path, 'w')
headerType = (kind if kind in ('Workout', 'ActivitySummary')
else 'Record')
f.write(','.join(FIELDS[headerType].keys()) + '\n')
self.handles[kind] = f
self.report('Opening %s for writing' % path)
def abbreviate_types(self):
"""
Shorten types by removing common boilerplate text.
"""
for node in self.nodes:
if node.tag == 'Record':
if 'type' in node.attrib:
node.attrib['type'] = abbreviate(node.attrib['type'])
def write_records(self):
kinds = FIELDS.keys()
for node in self.nodes:
if node.tag in kinds:
attributes = node.attrib
kind = attributes['type'] if node.tag == 'Record' else node.tag
values = [format_value(attributes.get(field), datatype)
for (field, datatype) in FIELDS[node.tag].items()]
line = encode(','.join(values) + '\n')
self.handles[kind].write(line)
def close_files(self):
for (kind, f) in self.handles.items():
f.close()
self.report('Written %s data.' % abbreviate(kind))
def extract(self):
self.open_for_writing()
self.write_records()
self.close_files()
def report_stats(self):
print('\nTags:\n%s\n' % format_freqs(self.tags))
print('Fields:\n%s\n' % format_freqs(self.fields))
print('Record types:\n%s\n' % format_freqs(self.record_types))
if __name__ == '__main__':
if len(sys.argv) != 2:
print('USAGE: python applehealthdata.py /path/to/export.xml',
file=sys.stderr)
sys.exit(1)
data = HealthDataExtractor(sys.argv[1])
data.report_stats()
data.extract()