forked from simonbromberg/googlefitbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interday.gs
422 lines (366 loc) · 15.3 KB
/
interday.gs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// interday.gs -- download daily totals of step data
// Doesn't require special permission, just follow setup and authorize
// Simon Bromberg (http://sbromberg.com)
// You are free to use, modify, copy any of the code in this script for your own purposes, as long as it's not for evil
// If you do anything cool with it, let me know!
// Note: there are minor improvements/cleanups still to be made in this file, but it should work as is if everything is setup properly
// See readme on github repo for more information
// Parts of this script is based on a script with the following information in the header.
// The main difference is this version uses Fitbit new OAuth2 and Google's OAuth2 instead of OAuthConfig which is deprecated
// Original script by loghound@gmail.com
// Original instructional video by Ernesto Ramirez at http://vimeo.com/26338767
// Modifications by Mark Leavitt (PDX Quantified Self organizer) www.markleavitt.com
// https://github.com/qslabs/FitbitDailyData/blob/master/FitbitDailyData.gs
/*
* Do not change these key names. These are just keys to access these properties once you set them up by running the Setup function from the Fitbit menu
*/
// Key of ScriptProperty for Firtbit consumer key.
var CONSUMER_KEY_PROPERTY_NAME = "fitbitConsumerKey";
// Key of ScriptProperty for Fitbit consumer secret.
var CONSUMER_SECRET_PROPERTY_NAME = "fitbitConsumerSecret";
/*
* You also need to add the callback URL to your app at dev.fitbit.com in the following format (replace {PROJECT KEY} with your project key:
* https://script.google.com/macros/d/{PROJECT KEY}/usercallback
* Go to your app at dev.fitbit.com and click "Edit Application Settings", add the URL in the Callback URL textbox, one URL per line (can do multiple in a single app)
*/
var SERVICE_IDENTIFIER = 'fitbit'; // usually do not need to change this either
// Default loggable resources (from Fitbit API docs).
var LOGGABLES = ["activities/log/steps", "activities/log/distance",
"activities/log/activeScore", "activities/log/activityCalories",
"activities/log/calories", "foods/log/caloriesIn",
"activities/log/minutesSedentary",
"activities/log/minutesLightlyActive",
"activities/log/minutesFairlyActive",
"activities/log/minutesVeryActive",
"sleep/startTime",
"sleep/timeInBed",
"sleep/minutesAsleep",
"sleep/awakeningsCount",
"sleep/minutesAwake",
"sleep/minutesToFallAsleep",
"sleep/minutesAfterWakeup",
"sleep/efficiency",
"body/weight", "body/bmi", "body/fat",];
function getFitbitService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store
if (!isConfigured()) {
setup();
return;
}
return OAuth2.createService(SERVICE_IDENTIFIER)
// Set the endpoint URLs, which are the same for all Google services.
.setAuthorizationBaseUrl('https://www.fitbit.com/oauth2/authorize')
.setTokenUrl('https://api.fitbit.com/oauth2/token')
// Set the client ID and secret, from the Google Developers Console.
.setClientId(getConsumerKey())
.setClientSecret(getConsumerSecret())
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('activity nutrition sleep weight profile settings')
// but not desirable in a production application.
//.setParam('approval_prompt', 'force')
.setTokenHeaders({
'Authorization': 'Basic ' + Utilities.base64Encode(getConsumerKey() + ':' + getConsumerSecret())
});
}
function clearService(){
OAuth2.createService(SERVICE_IDENTIFIER)
.setPropertyStore(PropertiesService.getUserProperties())
.reset();
}
function showSidebar() {
var service = getFitbitService();
if (!service.hasAccess()) {
var authorizationUrl = service.getAuthorizationUrl();
var template = HtmlService.createTemplate(
'<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
'Reopen the sidebar when the authorization is complete.');
template.authorizationUrl = authorizationUrl;
var page = template.evaluate();
SpreadsheetApp.getUi().showSidebar(page);
} else {
Logger.log("Has access!");
}
}
function authCallback(request) {
Logger.log("authcallback");
var service = getFitbitService();
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
Logger.log("success");
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
Logger.log("denied");
return HtmlService.createHtmlOutput('Denied. You can close this tab');
}
}
function getUser() {
var service = getFitbitService();
var options = {
headers: {
"Authorization": "Bearer " + service.getAccessToken(),
/*"oAuthServiceName": SERVICE_IDENTIFIER,
"oAuthUseToken": "always",*/
"method": "GET"
}};
var response = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/profile.json",options);
var o = Utilities.jsonParse(response.getContentText());
return o.user;
}
// function setup accepts and stores the Consumer Key, Consumer Secret, Project Key, firstDate, and list of Data Elements
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle("Setup Fitbit Download"); // UiApp API deprecated, may need to eventually replace with HTML service
app.setStyleAttribute("padding", "10px");
var consumerKeyLabel = app.createLabel("Fitbit OAuth 2.0 Client ID:*");
var consumerKey = app.createTextBox();
consumerKey.setName("consumerKey");
consumerKey.setWidth("100%");
consumerKey.setText(getConsumerKey());
var consumerSecretLabel = app.createLabel("Fitbit OAuth Consumer Secret:*");
var consumerSecret = app.createTextBox();
consumerSecret.setName("consumerSecret");
consumerSecret.setWidth("100%");
consumerSecret.setText(getConsumerSecret());
var projectKeyTitleLabel = app.createLabel("Project key: ");
var projectKeyLabel = app.createLabel(ScriptApp.getProjectKey());
var firstDate = app.createTextBox().setId("firstDate").setName("firstDate");
firstDate.setName("firstDate");
firstDate.setWidth("100%");
firstDate.setText(getFirstDate());
var lastDate = app.createTextBox().setId("lastDate").setName("lastDate");
lastDate.setName("lastDate");
lastDate.setWidth("100%");
lastDate.setText(getLastDate());
// add listbox to select data elements
var loggables = app.createListBox(true).setId("loggables").setName(
"loggables");
loggables.setVisibleItemCount(4);
// add all possible elements (in array LOGGABLES)
var logIndex = 0;
for (var resource in LOGGABLES) {
loggables.addItem(LOGGABLES[resource]);
// check if this resource is in the getLoggables list
if (getLoggables().indexOf(LOGGABLES[resource]) > -1) {
// if so, pre-select it
loggables.setItemSelected(logIndex, true);
}
logIndex++;
}
// create the save handler and button
var saveHandler = app.createServerClickHandler("saveSetup");
var saveButton = app.createButton("Save Setup", saveHandler);
// put the controls in a grid
var listPanel = app.createGrid(8, 2);
listPanel.setWidget(1, 0, consumerKeyLabel);
listPanel.setWidget(1, 1, consumerKey);
listPanel.setWidget(2, 0, consumerSecretLabel);
listPanel.setWidget(2, 1, consumerSecret);
listPanel.setWidget(3, 0, app.createLabel(" * (obtain these at dev.fitbit.com)"));
listPanel.setWidget(4, 0, projectKeyTitleLabel);
listPanel.setWidget(4, 1, projectKeyLabel);
listPanel.setWidget(5, 0, app.createLabel("Start date (yyyy-mm-dd)"));
listPanel.setWidget(5, 1, firstDate);
listPanel.setWidget(6, 0, app.createLabel("End date (yyyy-mm-dd)"));
listPanel.setWidget(6, 1, lastDate);
listPanel.setWidget(7, 0, app.createLabel("Data Elements to download:"));
listPanel.setWidget(7, 1, loggables);
// Ensure that all controls in the grid are handled
saveHandler.addCallbackElement(listPanel);
// Build a FlowPanel, adding the grid and the save button
var dialogPanel = app.createFlowPanel();
dialogPanel.add(listPanel);
dialogPanel.add(saveButton);
app.add(dialogPanel);
doc.show(app);
}
// function sync() is called to download all desired data from Fitbit API to the spreadsheet
function sync() {
// if the user has never performed setup, do it now
if (!isConfigured()) {
setup();
return;
}
var colIndex = 1
var dateColDone = 0;
var doc = SpreadsheetApp.getActiveSpreadsheet();
doc.setFrozenRows(4);
doc.getRange("R1C1").setValue("Sheet last synced: " + new Date());
doc.getRange("R2C1").setValue("Battery");
doc.getRange("R3C1").setValue("Last Sync");
doc.getRange("R4C1").setValue("Date");
var user = getUser();
var options = { headers:{
"Authorization": 'Bearer ' + getFitbitService().getAccessToken(),
"method": "GET"
}};
// prepare and end date, and a list of desired data elements
var dateString = getLastDate();
var activities = getLoggables();
// for each data element, fetch a list beginning from the firstDate, ending with today
Logger.log("here");
try {
var devices = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/devices.json",options);
var device = JSON.parse(devices.getContentText())[0];
} catch (exception) {
Logger.log(exception);
Browser.msgBox("error getting device info for " + user["displayName"]);
}
for (var activity in activities) {
var currentActivity = activities[activity];
try {
var result = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/"
+ currentActivity + "/date/" + getFirstDate() + "/"
+ dateString + ".json", options);
} catch (exception) {
Logger.log(exception);
Browser.msgBox("Error downloading " + currentActivity);
}
var o = JSON.parse(result.getContentText());
// set title
var workingCol = (colIndex + !dateColDone).toString()
var headerCell = doc.getRange("R1C"+workingCol);
headerCell.setValue(user["displayName"]);
headerCell = doc.getRange("R2C"+workingCol);
if (device != null) {
headerCell.setValue(device["battery"]);
}
else {
headerCell.setValue("error no device");
}
headerCell = doc.getRange("R3C"+workingCol);
if (device != null) {
var syncDate = new Date(device["lastSyncTime"]+"Z");
headerCell.setValue(Utilities.formatDate(syncDate, "GMT", "EEE, d MMM yyyy HH:mm"));
}
else {
headerCell.setValue("error no device");
}
var titleCell = doc.getRange("R4C"+colIndex.toString());
var cell = doc.getRange("R5C"+colIndex.toString());
// fill the spreadsheet with the data
var index = 0;
for (var i in o) {
// set title for this column
var title = i.substring(i.lastIndexOf('-') + 1);
titleCell.offset(0, !dateColDone + activity * 1.0).setValue(title);
var row = o[i];
for (var j = row.length - 1; j >= 0; j--) {
var val = row[j];
if (!dateColDone) {
cell.offset(index, 0).setValue(val["dateTime"]); // set the date index
}
cell.offset(index, !dateColDone + activity * 1.0).setValue(val["value"]);
index++;
}
}
}
colIndex+= 1 + !dateColDone;
dateColDone = 1;
}
function isConfigured() {
return getConsumerKey() != "" && getConsumerSecret() != "";
}
// function saveSetup saves the setup params from the UI
function saveSetup(e) {
setConsumerKey(e.parameter.consumerKey);
setConsumerSecret(e.parameter.consumerSecret);
setLoggables(e.parameter.loggables);
setFirstDate(e.parameter.firstDate);
setLastDate(e.parameter.lastDate);
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function getProperty(key) {
Logger.log("get property "+key);
return PropertiesService.getScriptProperties().getProperty(key);
}
function setProperty(key, value) {
PropertiesService.getScriptProperties().setProperty(key, value);
}
function setConsumerKey(consumerKey) {
setProperty(CONSUMER_KEY_PROPERTY_NAME, consumerKey);
}
function getConsumerKey() {
var consumer = getProperty(CONSUMER_KEY_PROPERTY_NAME);
if (consumer == null) {
consumer = "";
}
return consumer;
}
function setConsumerSecret(secret) {
setProperty(CONSUMER_SECRET_PROPERTY_NAME, secret);
}
function getConsumerSecret() {
var secret = getProperty(CONSUMER_SECRET_PROPERTY_NAME);
if (secret == null) {
secret = "";
}
return secret;
}
function setLoggables(loggables) {
setProperty("loggables", loggables);
}
function getLoggables() {
var loggable = getProperty("loggables");
if (loggable == null) {
loggable = LOGGABLES;
} else {
loggable = loggable.split(',');
}
return loggable;
}
function setFirstDate(firstDate) {
setProperty("firstDate", firstDate);
}
function getFirstDate() {
var firstDate = getProperty("firstDate");
if (firstDate == null) {
firstDate = formatToday(); // default value; feel free to change this
}
return firstDate;
}
function setLastDate(lastDate) {
setProperty("lastDate", lastDate);
}
function getLastDate() {
var lastDate = getProperty("lastDate");
if (lastDate == null) {
lastDate = formatToday(); // default value set to
}
return lastDate;
}
function formatToday() {
var todayDate = new Date;
return todayDate.getFullYear()
+ '-'
+ ("00" + (todayDate.getMonth() + 1)).slice(-2)
+ '-'
+ ("00" + todayDate.getDate()).slice(-2);
}
// function onOpen is called when the spreadsheet is opened; adds the Fitbit menu
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "Sync",
functionName: "sync"
}, {
name: "Setup",
functionName: "setup"
},
{
name: "Authorize",
functionName: "showSidebar"
},
{
name: "Reset",
functionName: "clearService"
}];
ss.addMenu("Fitbit", menuEntries);
}