forked from loghound/Fitbit-for-Google-App-Script
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fitbit.js
443 lines (394 loc) · 13.6 KB
/
fitbit.js
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// This script will pull down your fitbit data
// and push it into a spreadsheet
// Units are metric (kg, km) unless otherwise noted
// Suggestions/comments/improvements? Let me know loghound@gmail.com
//
//
/**** Length of time to look at.
* From fitbit documentation values are
* 1d, 7d, 30d, 1w, 1m, 3m, 6m, 1y, max.
*/
var period = "1y";
/**
* Key of ScriptProperty for Fitbit consumer key.
* @type {String}
* @const
*/
var CLIENT_ID_PROPERTY_NAME = "fitbitClientID";
/**
* Key of ScriptProperty for Fitbit consumer secret.
* @type {String}
* @const
*/
var CONSUMER_SECRET_PROPERTY_NAME = "fitbitConsumerSecret";
/**
* Key of Project.
* @type {String}
* @const
*/
var PROJECT_KEY_PROPERTY_NAME = "projectKey";
/**
* Default loggable resources.
*
* @type String[]
* @const
*/
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/timeInBed",
"sleep/minutesAsleep", "sleep/minutesAwake", "sleep/awakeningsCount",
"body/weight", "body/bmi", "body/fat" ];
/**
* Default fetchable periods.
*
* @type String[]
* @const
*/
var PERIODS = [ "1d", "7d", "30d", "1w", "1m", "3m", "6m", "1y", "max" ];
/**
* Instance of PropertiesService for access to ScriptProperties
*
* @type {Object} scriptProperties
*/
var scriptProperties = PropertiesService.getScriptProperties();
function refreshTimeSeries() {
// if the user has never configured ask him to do it here
if (!isConfigured()) {
renderFitbitConfigurationDialog();
return;
}
Logger.log('Refreshing timeseries data...');
var user = authorize().user;
Logger.log(user)
var doc = SpreadsheetApp.getActiveSpreadsheet()
doc.setFrozenRows(2);
// header rows
doc.getRange("a1").setValue(user.displayName);
doc.getRange("a1").setNote("DOB:" + user.dateOfBirth);
doc.getRange("b1").setValue(
user.locale);
// add the loggables for the last update
doc.getRange("c1").setValue("Loggables:");
doc.getRange("c1").setNote(getLoggables());
// period for the last update
doc.getRange("d1").setValue("Period: " + getPeriod());
doc.getRange("e1").setValue("=image(\"" + user.avatar + "\";1)");
// get inspired here http://wiki.fitbit.com/display/API/API-Get-Time-Series
var activities = getLoggables();
for ( var activity in activities) {
Logger.log('Refreshing ' + activity)
var dateString = "today";
var currentActivity = activities[activity];
try {
var service = getService();
var options = {
"method" : "GET",
"headers": {
"Authorization": "Bearer " + service.getAccessToken()
}
};
if (service.hasAccess()) {
var url = "https://api.fitbit.com/1/user/-/"
+ currentActivity + "/date/" + dateString + "/"
+ getPeriod() + ".json";
Logger.log(options)
var result = UrlFetchApp.fetch(url, options);
}
} catch (exception) {
Logger.log(exception);
}
Logger.log(result);
var o = JSON.parse(result.getContentText());
// set title
var titleCell = doc.getRange("a2");
titleCell.setValue("Date");
var cell = doc.getRange('a3');
// fill data
for ( var i in o) {
// set title for this column
var title = i.substring(i.lastIndexOf('-') + 1);
titleCell.offset(0, 1 + activity * 1.0).setValue(title);
var row = o[i];
var row_index = 0;
for ( var j in row) {
var val = row[j];
// Convert the date from the API to a real GS date needed for finding the right row.
var dateParts = val["dateTime"].split("-");
var date = new Date(dateParts[0], (dateParts[1]-1), dateParts[2], 0, 0, 0, 0);
// Have we found a row yet? or do we need to look for it?
if ( row_index != 0 ) {
row_index++;
} else {
row_index = findRow(date);
}
// Insert Date into first column
doc.getActiveSheet().getRange(row_index, 1).setValue(val["dateTime"]);
// Insert value
doc.getActiveSheet().getRange(row_index, 2 + activity * 1.0).setValue(Number(val["value"]));
}
}
}
}
function isConfigured() {
return getConsumerKey() != "" && getConsumerSecret() != "";
}
/**
* @return String OAuth consumer key to use when tweeting.
*/
function getConsumerKey() {
var key = scriptProperties.getProperty(CLIENT_ID_PROPERTY_NAME);
if (key == null) {
key = "";
}
return key;
}
/**
* @param String OAuth consumer key to use when tweeting.
*/
function setConsumerKey(key) {
scriptProperties.setProperty(CLIENT_ID_PROPERTY_NAME, key);
}
/**
* @return String Project key
*/
function getProjectKey() {
var key = scriptProperties.getProperty(PROJECT_KEY_PROPERTY_NAME);
if (key == null) {
key = "";
}
return key;
}
/**
* @param String Project key
*/
function setProjectKey(key) {
scriptProperties.setProperty(PROJECT_KEY_PROPERTY_NAME, key);
}
/**
* @param Array
* of String for loggable resources, i.e. "foods/log/caloriesIn"
*/
function setLoggables(loggable) {
scriptProperties.setProperty('loggables', loggable);
}
/**
* Returns the loggable resources as String[]
*
* @return String[] loggable resources
*/
function getLoggables() {
var loggable = scriptProperties.getProperty('loggables');
if (loggable == null) {
loggable = LOGGABLES;
} else {
loggable = loggable.split(',');
}
return loggable;
}
function setPeriod(period) {
scriptProperties.setProperty('period', period);
}
function getPeriod() {
var period = scriptProperties.getProperty('period');
if (period == null) {
period = "30d";
}
return period;
}
/**
* @return String OAuth consumer secret to use when tweeting.
*/
function getConsumerSecret() {
var secret = scriptProperties.getProperty(CONSUMER_SECRET_PROPERTY_NAME);
if (secret == null) {
secret = "";
}
return secret;
}
/**
* @param String OAuth consumer secret to use when tweeting.
*/
function setConsumerSecret(secret) {
scriptProperties.setProperty(CONSUMER_SECRET_PROPERTY_NAME, secret);
}
/** Retrieve config params from the UI and store them. */
function saveConfiguration(e) {
setConsumerKey(e.parameter.clientID);
setConsumerSecret(e.parameter.consumerSecret);
setProjectKey(e.parameter.projectKey);
setLoggables(e.parameter.loggables);
setPeriod(e.parameter.period);
var app = UiApp.getActiveApplication();
app.close();
return app;
}
/**
* Configure all UI components and display a dialog to allow the user to
* configure approvers.
*/
function renderFitbitConfigurationDialog() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle("Configure Fitbit");
app.setStyleAttribute("padding", "10px");
app.setHeight('380');
var helpLabel = app
.createLabel("From here you will configure access to fitbit -- Just supply your own"
+ "client id and secret from dev.fitbit.com. "
+ " You can find the project key by loading the script in the script editor"
+ " (tools->Script Editor..) and opening the project properties (file->Project properties). \n\n"
+ " While in the script editor, you also need to add the OAuth2 library following these instructions: https://github.com/googlesamples/apps-script-oauth2/tree/0e7bcd464962321a75ccb97256d5373b27c4c2e1#setup. \n\n"
+ " You also need to setup your Redirect URI at fitbit, substituting in your project key you just found and "
+ "using the instructions here: https://github.com/googlesamples/apps-script-oauth2/tree/0e7bcd464962321a75ccb97256d5373b27c4c2e1#redirect-uri \n\n"
+ "Important: To authorize this app you need to run 'Authorize' from the fitbit menu.");
helpLabel.setStyleAttribute("text-align", "justify");
helpLabel.setWidth("95%");
var consumerKeyLabel = app.createLabel("Fitbit OAuth 2.0 Client ID:");
var consumerKey = app.createTextBox();
consumerKey.setName("clientID");
consumerKey.setWidth("100%");
consumerKey.setText(getConsumerKey());
var consumerSecretLabel = app.createLabel("Fitbit OAuth Client (Consumer) Secret:");
var consumerSecret = app.createTextBox();
consumerSecret.setName("consumerSecret");
consumerSecret.setWidth("100%");
consumerSecret.setText(getConsumerSecret());
var projectKeyLabel = app.createLabel("Project Key:");
var projectKey = app.createTextBox();
projectKey.setName("projectKey");
projectKey.setWidth("100%");
projectKey.setText(getProjectKey());
var saveHandler = app.createServerClickHandler("saveConfiguration");
var saveButton = app.createButton("Save Configuration", saveHandler);
var listPanel = app.createGrid(6, 3);
listPanel.setWidget(1, 0, consumerKeyLabel);
listPanel.setWidget(1, 1, consumerKey);
listPanel.setWidget(2, 0, consumerSecretLabel);
listPanel.setWidget(2, 1, consumerSecret);
listPanel.setWidget(3, 0, projectKeyLabel);
listPanel.setWidget(3, 1, projectKey);
// add checkboxes to select loggables
var loggables = app.createListBox(true).setId("loggables").setName("loggables");
loggables.setVisibleItemCount(3);
var current_loggables = getLoggables();
for ( var resource in LOGGABLES) {
loggables.addItem(LOGGABLES[resource]);
if (current_loggables.indexOf(LOGGABLES[resource]) > -1) {
loggables.setItemSelected(parseInt(resource), true);
}
}
listPanel.setWidget(4, 0, app.createLabel("Resources:"));
listPanel.setWidget(4, 1, loggables);
var period = app.createListBox(false).setId("period").setName("period");
period.setVisibleItemCount(1);
// add valid timeperiods
for ( var resource in PERIODS) {
period.addItem(PERIODS[resource]);
}
period.setSelectedIndex(PERIODS.indexOf(getPeriod()));
listPanel.setWidget(5, 0, app.createLabel("Period:"));
listPanel.setWidget(5, 1, period);
// Ensure that all form fields get sent along to the handler
saveHandler.addCallbackElement(listPanel);
var dialogPanel = app.createFlowPanel();
dialogPanel.add(helpLabel);
dialogPanel.add(listPanel);
dialogPanel.add(saveButton);
app.add(dialogPanel);
doc.show(app);
}
function getService() {
//Implement updated OAuth 2 support
//When using new OAuth1 library, callback URL length is too long, therefore doesn't work:
// https://github.com/googlesamples/apps-script-oauth1/issues/8
//
//Fitbit API:
//https://wiki.fitbit.com/display/API/OAuth+2.0
//Google App Script OAuth2 instructions:
//https://github.com/googlesamples/apps-script-oauth2
//modified from : https://github.com/googlesamples/apps-script-oauth1/issues/8#issuecomment-100309694
return OAuth2.createService('fitbit')
.setAuthorizationBaseUrl('https://www.fitbit.com/oauth2/authorize')
.setTokenUrl('https://api.fitbit.com/oauth2/token')
.setClientId(getConsumerKey())
.setClientSecret(getConsumerSecret())
.setProjectKey(getProjectKey())
.setCallbackFunction('fitbitAuthCallback')
.setPropertyStore(PropertiesService.getScriptProperties())
.setScope('activity')
.setTokenHeaders({
'Authorization': 'Basic ' + Utilities.base64Encode(getConsumerKey() + ':' + getConsumerSecret())
});
}
function authorize() {
var service = getService()
if (service.hasAccess()) {
var url = 'https://api.fitbit.com/1/user/-/profile.json';
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + service.getAccessToken()
}
});
Logger.log(JSON.stringify(JSON.parse(response.getContentText()), null, 2));
return JSON.parse(response.getContentText())
} else {
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);
}
}
// modified from: https://github.com/googlesamples/apps-script-oauth1/tree/9d074adc735e35c8966bcfa30114c205d69ab44e#3-handle-the-callback
function fitbitAuthCallback(request) {
var service = getService();
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this page.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this page');
}
}
/** When the spreadsheet is opened, add a Fitbit menu. */
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "Refresh fitbit Time Data",
functionName: "refreshTimeSeries"
},
{
name: "Configure",
functionName: "renderFitbitConfigurationDialog"
},
{
name: "Authorize",
functionName: "authorize"
}];
ss.addMenu("Fitbit", menuEntries);
}
function onInstall() {
onOpen();
// put the menu when script is installed
}
// Find the right row for a date.
function findRow(date) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange("A3");
// Find the first cell in first column which is either empty,
// or has an equal or bigger date than the one we are looking for.
while ((cell.getValue() != "") && (cell.getValue() < date)) {
cell = cell.offset(1,0);
}
// If the cell we found has a newer date than ours, we need to
// insert a new row right before that.
if (cell.getValue() > date) {
doc.insertRowBefore(cell.getRow())
}
// return only the number of the row.
return (cell.getRow());
}