-
Notifications
You must be signed in to change notification settings - Fork 5
/
GoogleFitness.py
244 lines (201 loc) · 6.83 KB
/
GoogleFitness.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
import requests
import json
from random import randint
import config
def getDataSources():
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources"
headers = { 'content-type': 'application/json',
'Authorization': 'Bearer %s' % ACCESS_TOKEN }
r = requests.get(url, headers=headers)
if r.status_code == 200:
return True
else:
print r.status_code
print r.content
return False
def createStepDataSource():
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources"
headers = { 'content-type': 'application/json',
'Authorization': 'Bearer %s' % config.accessToken }
data = {
"dataStreamName": "AppleStepDataSource",
"type": "derived",
"application": {
"name": "AppleHealth2GoogleFit",
"version": "1"
},
"dataType": {
"field": [
{
"name": "steps",
"format": "integer"
}
],
"name": "com.google.step_count.delta"
},
"device": {
"manufacturer": "Apple",
"model": "RESTAPI",
"type": "tablet",
"uid": str(randint(0, 1000)),
"version": "1"
}
}
r = requests.post(url, headers=headers, data=json.dumps(data))
if r.status_code == 200:
response = json.loads(r.content)
dataSourceId =response['dataStreamId']
return dataSourceId
else:
print r.status_code
print r.content
return False
def createWeightDataSource():
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources"
headers = { 'content-type': 'application/json',
'Authorization': 'Bearer %s' % config.accessToken }
data = {
"dataStreamName": "AppleWeightDataSource",
"type": "derived",
"application": {
"name": "AppleHealth2GoogleFit",
"version": "1"
},
"dataType": {
"field": [
{
"name": "weight",
"format": "floatPoint"
}
],
"name": "com.google.weight"
},
"device": {
"manufacturer": "Apple",
"model": "RESTAPI",
"type": "tablet",
"uid": str(randint(0, 1000)),
"version": "1"
}
}
r = requests.post(url, headers=headers, data=json.dumps(data))
if r.status_code == 200:
response = json.loads(r.content)
dataSourceId =response['dataStreamId']
return dataSourceId
else:
print r.status_code
print r.content
return False
def createDistanceDataSource():
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources"
headers = { 'content-type': 'application/json',
'Authorization': 'Bearer %s' % config.accessToken }
data = {
"dataStreamName": "AppleDistanceDataSource",
"type": "derived",
"application": {
"name": "AppleHealth2GoogleFit",
"version": "1"
},
"dataType": {
"field": [
{
"name": "distance",
"format": "floatPoint"
}
],
"name": "com.google.distance.delta"
},
"device": {
"manufacturer": "Apple",
"model": "RESTAPI",
"type": "tablet",
"uid": str(randint(0, 1000)),
"version": "1"
}
}
r = requests.post(url, headers=headers, data=json.dumps(data))
if r.status_code == 200:
response = json.loads(r.content)
dataSourceId =response['dataStreamId']
return dataSourceId
else:
print r.status_code
print r.content
return False
def sendPoints(dataSourceId,records):
dataPoints = []
if records[0].recordType == "HKQuantityTypeIdentifierBodyMass":
for record in records:
endTimeNanos = record.endTime*1000000
if endTimeNanos > 1546513741000000000:
endTimeNanos = 1546513741000000000
point = {
"dataTypeName": "com.google.weight",
"startTimeNanos": record.startTime*1000000,
"endTimeNanos": endTimeNanos,
"value": [
{
"fpVal": record.value
}
]
}
dataPoints.append(point);
if records[0].recordType == "HKQuantityTypeIdentifierStepCount":
for record in records:
endTimeNanos = record.endTime*1000000
if endTimeNanos > 1546513741000000000:
endTimeNanos = 1546513741000000000
point = {
"dataTypeName": "com.google.step_count.delta",
"startTimeNanos": record.startTime*1000000,
"endTimeNanos": record.endTime*1000000,
"value": [
{
"intVal": record.value
}
]
}
dataPoints.append(point);
if records[0].recordType == "HKQuantityTypeIdentifierDistanceWalkingRunning":
for record in records:
endTimeNanos = record.endTime*1000000
if endTimeNanos > 1546513741000000000:
endTimeNanos = 1546513741000000000
point = {
"dataTypeName": "com.google.distance.delta",
"startTimeNanos": record.startTime*1000000,
"endTimeNanos": record.endTime*1000000,
"value": [
{
"fpVal": record.value*1000
}
]
}
dataPoints.append(point);
minStartTime = records[0].startTime
maxEndTime = records[len(records)-1].endTime
for points in chunks(dataPoints, 10000):
addData(dataSourceId,points,minStartTime,maxEndTime)
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
def addData(dataSourceId,dataPoints,minStartTime ,maxEndTime):
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources/" + dataSourceId + "/datasets/" +str(minStartTime*1000000) + "-"+ str(maxEndTime*1000000)
headers = { 'content-type': 'application/json',
'Authorization': 'Bearer %s' % config.accessToken }
data = {
"dataSourceId": dataSourceId,
"minStartTimeNs": minStartTime*1000000,
"maxEndTimeNs": maxEndTime*1000000,
"point": dataPoints
}
r = requests.patch(url, headers=headers, data=json.dumps(data))
if r.status_code == 200:
print str(len(dataPoints)) + " dataPoints : " + dataSourceId
return True
else:
print r.status_code
print r.content
return False