Last active
January 5, 2022 12:14
-
-
Save jeffgreenca/fdcc19154c617d6da84d57c5833599df to your computer and use it in GitHub Desktop.
Quick & Dirty nodejs app to get Fitbit second by second heart rate data
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
//For info about this gist, see my blog entry https://virtualdatacave.com/2016/09/fitbit-hr-data-raw/ | |
//For API see https://dev.fitbit.com/docs/heart-rate/#get-heart-rate-intraday-time-series | |
var dateFormat = require('dateformat'); | |
// initialize the express application | |
var express = require("express"), | |
app = express(); | |
// initialize the Fitbit API client | |
var FitbitApiClient = require("fitbit-node"), | |
client = new FitbitApiClient("YOUR_SECRET", "YOUR_KEY"); | |
// redirect the user to the Fitbit authorization page | |
app.get("/hr", function (req, res) { | |
// request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes | |
res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'http://YOURSERVER:8083/callback')); | |
}); | |
// handle the callback from the Fitbit authorization flow | |
app.get("/callback", function (req, res) { | |
var menu = "<p><a href='/gethr?code=" + req.query.code + "'>Get HR for Today</a>" + | |
"<p><a href='/gethr?code=" + req.query.code + "&day=yyyy-MM-dd'>Get HR for Specific Day (Must Manually Update URL)</a>"; | |
res.send(menu); | |
}); | |
app.get("/gethr", function (req, res) { | |
var specificday = req.query.day; | |
var code = req.query.code; | |
// exchange the authorization code we just received for an access token | |
client.getAccessToken(code, 'http://YOURSERVER:8083/callback').then(function (result) { | |
// log the access token | |
// console.log("Got access token: " + result.access_token); | |
// use the access token to fetch the user's profile information | |
if(specificday) { | |
client.get("/activities/heart/date/" + specificday + "/" + specificday + "/1sec/time/00:00/23:59.json", result.access_token).then(function (results) { | |
var csv = "<pre>date,time,HR"; | |
for(var bit in results[0]['activities-heart-intraday']['dataset']) { | |
csv = csv + "\r\n" + specificday + "," + results[0]['activities-heart-intraday']['dataset'][bit]['time'] + ',' + results[0]['activities-heart-intraday']['dataset'][bit]['value']; | |
} | |
res.send(csv); | |
}); | |
} else { | |
client.get("/activities/heart/date/today/1d/1sec/time/00:00/23:59.json", result.access_token).then(function (results) { | |
//Now get the heart rate data | |
var csv = "<pre>date,time,HR"; | |
var datebit = dateFormat( (new Date()), 'yyyy-mm-dd' ); | |
for(var bit in results[0]['activities-heart-intraday']['dataset']) { | |
csv = csv + "\r\n" + datebit + "," + results[0]['activities-heart-intraday']['dataset'][bit]['time'] + ',' + results[0]['activities-heart-intraday']['dataset'][bit]['value']; | |
} | |
res.send(csv); | |
}); | |
} | |
}).catch(function (error) { | |
res.send(error); | |
}); | |
}); | |
// launch the server | |
app.listen(8083); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment