forked from simonbromberg/googlefitbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intraday.gs
353 lines (307 loc) · 11.9 KB
/
intraday.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
// 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
//Script based on post here http://quantifiedself.com/2014/09/download-minute-fitbit-data/ by Ernesto Ramirez
/*
* 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";
// Key of project (inside File > Project Properties)
var PROJECT_KEY_PROPERTY_NAME = "projectKey";
var SERVICE_IDENTIFIER = 'fitbit';
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{
name: "Setup",
functionName: "setup"
},
{
name: "Authorize",
functionName: "showSidebar"
},
{
name: "Reset",
functionName: "clearService"
},
{
name: "Download data",
functionName: "refreshTimeSeries"
}];
ss.addMenu("Fitbit", menuEntries);
}
function isConfigured() {
return getConsumerKey() != "" && getConsumerSecret() != "" && getProjectKey() != "";
}
function setConsumerKey(key) {
ScriptProperties.setProperty(CONSUMER_KEY_PROPERTY_NAME, key);
}
function getConsumerKey() {
var key = ScriptProperties.getProperty(CONSUMER_KEY_PROPERTY_NAME);
if (key == null) {
key = "";
}
return key;
}
function setLoggables(loggable) {
ScriptProperties.setProperty("loggables", loggable);
}
function getLoggables() {
var loggable = ScriptProperties.getProperty("loggables");
if (loggable == null) {
loggable = LOGGABLES;
} else {
loggable = loggable.split(',');
}
return loggable;
}
function setProjectKey(key) {
ScriptProperties.setProperty(PROJECT_KEY_PROPERTY_NAME, key);
}
function getProjectKey() {
var key = ScriptProperties.getProperty(PROJECT_KEY_PROPERTY_NAME);
if (key == null) {
key = "";
}
return key;
}
function setConsumerSecret(secret) {
ScriptProperties.setProperty(CONSUMER_SECRET_PROPERTY_NAME, secret);
}
function getConsumerSecret() {
var secret = ScriptProperties.getProperty(CONSUMER_SECRET_PROPERTY_NAME);
if (secret == null) {
secret = "";
}
return secret;
}
// function saveSetup saves the setup params from the UI
function saveSetup(e) {
setConsumerKey(e.parameter.consumerKey);
setConsumerSecret(e.parameter.consumerSecret);
setProjectKey(e.parameter.projectKey);
setLoggables(e.parameter.loggables);
setFirstDate(e.parameter.firstDate);
setLastDate(e.parameter.lastDate);
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function setFirstDate(firstDate) {
ScriptProperties.setProperty("firstDate", firstDate);
}
function getFirstDate() {
var firstDate = ScriptProperties.getProperty("firstDate");
if (firstDate == null) {
firstDate = "2012-01-01";
}
return firstDate;
}
function setLastDate(lastDate) {
ScriptProperties.setProperty("lastDate", lastDate);
}
function getLastDate() {
var lastDate = ScriptProperties.getProperty("lastDate");
if (lastDate == null) {
var today = new Date();
lastDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(),"yyyy-mm-dd");
}
return lastDate;
}
// 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");
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 projectKeyLabel = app.createLabel("Project key:*");
var projectKey = app.createTextBox();
projectKey.setName("projectKey");
projectKey.setWidth("100%");
projectKey.setText(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());
// 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, 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, app.createLabel(" * (obtain these at dev.fitbit.com, use OAuth2.0)"));
listPanel.setWidget(4, 0, projectKeyLabel);
listPanel.setWidget(4, 1, projectKey);
listPanel.setWidget(5, 0, app.createLabel("Start Date for download (yyyy-mm-dd)"));
listPanel.setWidget(5, 1, firstDate);
listPanel.setWidget(6, 0, app.createLabel("End date for download (yyyy-mm-dd)"));
listPanel.setWidget(6, 1, lastDate);
listPanel.setWidget(7, 0, app.createLabel("Very long intervals will not work; exceed Fitbit rate limit and/or function will timeout"));
// 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 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
Logger.log(PropertiesService.getUserProperties());
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 profile')
.setParam('redirect_uri','https://script.google.com/macros/d/'+getProjectKey()+'/usercallback')
// Forces the approval prompt every time. This is useful for testing,
// 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(),
"method": "GET"
}};
var response = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/profile.json",options);
var o = JSON.parse(response.getContentText());
return o.user;
}
function refreshTimeSeries() {
if (!isConfigured()) {
setup();
return;
}
var user = getUser();
var doc = SpreadsheetApp.getActiveSpreadsheet();
doc.setFrozenRows(2);
// two header rows
doc.getRange("a1").setValue(user.fullName);
doc.getRange("a1").setComment("DOB:" + user.dateOfBirth)
doc.getRange("b1").setValue(user.country + "/" + user.state + "/" + user.city);
var options =
{headers:{
"Authorization": 'Bearer ' + getFitbitService().getAccessToken(),
"method": "GET"
}};
var activities = ["activities/log/steps"];
var intradays = ["activities-log-steps-intraday"];
var lastIndex = 0;
for (var activity in activities) {
var index = 0;
var dateString = getFirstDate();
date = parseDate(dateString);
var table = new Array();
while (1) {
var currentActivity = activities[activity];
try {
var result = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/" + currentActivity + "/date/" + dateString+ "/" + dateString + ".json", options);
} catch(exception) {
Logger.log(exception);
}
var o = JSON.parse(result.getContentText());
var cell = doc.getRange('a3');
var titleCell = doc.getRange("a2");
titleCell.setValue("Date");
var title = currentActivity.split("/");
title = title[title.length - 1];
titleCell.offset(0, 1 + activity * 1.0).setValue(title);
var row = o[intradays[activity]]["dataset"];
for (var j in row) {
var val = row[j];
var arr = new Array(2);
arr[0] = dateString + ' ' + val["time"];
arr[1] = val["value"];
table.push(arr);
// set the value index index
index++;
}
date.setDate(date.getDate()+1);
dateString = Utilities.formatDate(date, "GMT", "yyyy-MM-dd");
if (dateString > getLastDate()) {
break;
}
}
// Batch set values of table, much faster than doing each time per loop run, this wouldn't work as is if there were multiple activities being listed
doc.getRange("A3:B"+(table.length+2)).setValues(table);
}
}
// parse a date in yyyy-mm-dd format
function parseDate(input) {
var parts = input.match(/(\d+)/g);
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}
// parse a date in 2011-10-25T23:57:00.000 format
function parseDate2(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4]);
}