Created
November 1, 2016 14:54
-
-
Save cassus/7ba7829ff6f6882508e6d4bbf2d06534 to your computer and use it in GitHub Desktop.
Zapier app to read Activity and Sleep data from FitBit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this to transfer activity and sleep data from FitBit to my google calendar