forked from loghound/Fitbit-for-Google-App-Script
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfitbit.js
371 lines (331 loc) · 11.1 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
// 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 [email protected]
//
/**
* Key of ScriptProperty for Firtbit consumer key.
*
* @type {String}
* @const
*/
var CONSUMER_KEY_PROPERTY_NAME = "fitbitConsumerKey";
/**
* Key of ScriptProperty for Fitbit consumer secret.
*
* @type {String}
* @const
*/
var CONSUMER_SECRET_PROPERTY_NAME = "fitbitConsumerSecret";
/**
* 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" ];
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();
var doc = SpreadsheetApp.getActiveSpreadsheet();
doc.setFrozenRows(2);
// header rows
doc.getRange("a1").setValue(user.displayName);
doc.getRange("a1").setComment("DOB:" + user.dateOfBirth);
doc.getRange("b1").setValue(
user.country);
// add the loggables for the last update
doc.getRange("c1").setValue("Loggables:");
doc.getRange("c1").setComment(getLoggables());
// period for the last update
doc.getRange("d1").setValue("Period: " + getPeriod());
doc.getRange("e1").setValue(user.avatar);
var options = {
"oAuthServiceName" : "fitbit",
"oAuthUseToken" : "always",
"method" : "GET",
"headers": {
"Accept-Language": user.foodsLocale
}
};
// 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 result = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/"
+ currentActivity + "/date/" + dateString + "/"
+ getPeriod() + ".json", options);
} catch (exception) {
Logger.log(exception);
}
var o = Utilities.jsonParse(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(CONSUMER_KEY_PROPERTY_NAME);
if (key == null) {
key = "";
}
return key;
}
/**
* @param String
* OAuth consumer key to use when tweeting.
*/
function setConsumerKey(key) {
ScriptProperties.setProperty(CONSUMER_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.consumerKey);
setConsumerSecret(e.parameter.consumerSecret);
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");
var helpLabel = app
.createLabel("From here you will configure access to fitbit -- Just supply your own"
+ "consumer key and secret \n\n"
+ "Important: To authroize this app you need to load the script in the script editor"
+ " (tools->Script Manager) and then run the 'authorize' script.");
helpLabel.setStyleAttribute("text-align", "justify");
helpLabel.setWidth("95%");
var consumerKeyLabel = app.createLabel("Fitbit OAuth Consumer Key:");
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 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);
// 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(3, 0, app.createLabel("Resources:"));
listPanel.setWidget(3, 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(4, 0, app.createLabel("Period:"));
listPanel.setWidget(4, 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 authorize() {
var oAuthConfig = UrlFetchApp.addOAuthService("fitbit");
oAuthConfig.setAccessTokenUrl("https://api.fitbit.com/oauth/access_token");
oAuthConfig.setRequestTokenUrl("https://api.fitbit.com/oauth/request_token");
oAuthConfig.setAuthorizationUrl("https://api.fitbit.com/oauth/authorize");
oAuthConfig.setConsumerKey(getConsumerKey());
oAuthConfig.setConsumerSecret(getConsumerSecret());
var options = {
"oAuthServiceName" : "fitbit",
"oAuthUseToken" : "always"
};
// get The profile but don't do anything with it -- just to force
// authentication
var result = UrlFetchApp.fetch(
"https://api.fitbit.com/1/user/-/profile.json", options);
var o = Utilities.jsonParse(result.getContentText());
return o.user;
// options are dateOfBirth, nickname, state, city, fullName, etc. see
// http://wiki.fitbit.com/display/API/API-Get-User-Info
}
/** 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"
} ];
ss.addMenu("Fitbit", menuEntries);
}
function onInstall() {
onOpen();
// put the menu when script is installed
}
function dump(arr, level) {
var dumped_text = "";
if (!level)
level = 0;
// The padding given at the beginning of the line.
var level_padding = "";
for ( var j = 0; j < level + 1; j++)
level_padding += " ";
if (typeof (arr) == 'object') { // Array/Hashes/Objects
for ( var item in arr) {
var value = arr[item];
if (typeof (value) == 'object') { // If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value, level + 1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value
+ "\"\n";
}
}
} else { // Stings/Chars/Numbers etc.
dumped_text = "===>" + arr + "<===(" + typeof (arr) + ")";
}
return dumped_text;
}
// 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());
}