Skip to content

Instantly share code, notes, and snippets.

@cassus
Created November 1, 2016 14:54
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassus/7ba7829ff6f6882508e6d4bbf2d06534 to your computer and use it in GitHub Desktop.
Save cassus/7ba7829ff6f6882508e6d4bbf2d06534 to your computer and use it in GitHub Desktop.
Zapier app to read Activity and Sleep data from FitBit
## Triggers (Bringing data into Zapier)
### Sleep - polling
https://api.fitbit.com/1/user/-/sleep/date/today.json
### Activity - polling
https://api.fitbit.com/1/user/-/activities/list.json?beforeDate=today&sort=desc&limit=10&offset=0
## Scripting API
see script.js
function formatDuration(minutes) {
var m = moment.duration(minutes,'minutes');
return (m.hours() === 0 ? "" : m.hours() + "h ") + m.minutes() + "m";
}
function add_auth_headers_to_bundle(bundle) {
var extra_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization':'Basic <YOUR KEY>'
};
return {
url: bundle.request.url,
method: bundle.request.method,
headers: $.extend({},bundle.request.headers,extra_headers),
params: bundle.request.params,
data: bundle.request.data
};
}
var Zap = {
pre_oauthv2_refresh: function(bundle) {
return add_auth_headers_to_bundle(bundle);
},
activity_pre_poll: function(bundle) {
/*
Argument:
bundle.request.url: <string>
bundle.request.method: <string> # 'GET'
bundle.request.auth: <array> # [username, password]
bundle.request.headers: <object>
bundle.request.params: <object> # this will be mapped into the querystring
bundle.request.data: <string> # str or null
bundle.url_raw: <string>
bundle.auth_fields: <object>
bundle.trigger_fields: <object> # the fields provided by the user during setup
bundle.zap: <object> # info about the zap
bundle.meta: <object> # extra runtime information you can use
The response should be an object of:
url: <string>
method: <string> # 'GET', 'POST', 'PATCH', 'PUT', 'DELETE'
auth: <array> # [username, password]
headers: <object>
params: <object> # this will be mapped into the query string
data: <string> or null # request body: optional if POST, not needed if GET
*/
var url = bundle.request.url.replace('today', moment().add(2,'day').format('YYYY-MM-DD'));
return {
url: url,
method: bundle.request.method,
auth: bundle.request.auth,
headers: bundle.request.headers,
params: bundle.request.params,
data: bundle.request.data
}; // or return bundle.request;
},
activity_post_poll: function(bundle) {
var response = JSON.parse(bundle.response.content);
return _.map(response.activities, function (activity) {
activity.endTime = moment(activity.startTime).add(activity.duration,'ms').format();
activity.startTime = moment(activity.startTime).format();
activity.activeDurationMinutes = Math.round(moment.duration({millisecond: activity.activeDuration}).asMinutes());
return activity;
});
},
sleep_post_poll: function(bundle) {
/*
Argument:
bundle.response.status_code: <integer>
bundle.response.headers: <object>
bundle.response.content: <str>
bundle.request: <original object from TRIGGERKEY_pre_poll bundle>
bundle.auth_fields: <object>
bundle.trigger_fields: <object> # the fields provided by the user during setup
bundle.zap: <object> # info about the zap
bundle.meta: <object> # extra runtime information you can use
The response should be JSON serializable:
[
<object>, # with unique 'id' key
<object> # with unique 'id' key
]
*/
var response = JSON.parse(bundle.response.content);
function enhanceSleepResponse(sleep) {
delete sleep.minuteData;
//sleep.startTime = sleep.startTime
sleep.endTime = moment(sleep.startTime).add(sleep.duration,'ms').format('YYYY-MM-DDTHH:mm');
sleep.formattedAsleep = formatDuration(sleep.minutesAsleep);
sleep.formattedAwake = formatDuration(sleep.minutesAwake);
return sleep;
}
return _.map(response.sleep, enhanceSleepResponse);
},
pre_oauthv2_token: function(bundle) {
/*
Argument:
bundle.request.url: <string>
bundle.request.method: <string> # 'GET'
bundle.request.headers: <object>
bundle.request.params: <object> # this will be mapped into the querystring
bundle.request.data: <object>
bundle.oauth_data: <object> # obj that contains your client_id, client_secret, etc...
bundle.load: <object> # the params set to be sent as form/query string...
bundle.zap: <object> # info about the zap
The response should be an object of:
url: <string>
method: <string> # 'GET', 'POST', 'PATCH', 'PUT', 'DELETE'
headers: <object>
params: <object> # this will be mapped into the query string
data: <string> or null # request body: optional if POST, not needed if GET
*/
return add_auth_headers_to_bundle(bundle);
}
};
@cassus
Copy link
Author

cassus commented Nov 1, 2016

I use this to transfer activity and sleep data from FitBit to my google calendar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment