From 45a8800cf1f212980d6d24846addf4f6e40c9221 Mon Sep 17 00:00:00 2001 From: martind2000 Date: Tue, 18 Apr 2017 10:17:56 +0100 Subject: [PATCH] fitbit updates --- lib/today.js | 6 +++--- lib/today/fitbit.js | 19 +++++++++---------- web-server.js | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/today.js b/lib/today.js index a6c2e3d..a9fd5ff 100644 --- a/lib/today.js +++ b/lib/today.js @@ -17,7 +17,7 @@ const weather = require('./today/weather'); const trains = require('./today/trains'); const history = require('./today/history'); const mdMailer = require('./today/mailer'); -// const mdFitbit = require('./today/fitbit'); +const mdFitbit = require('./today/fitbit'); const todayFTSE = require('./today/todayftse'); const quotes = require('./today/quotes'); //var db_name = 'silvrgit'; @@ -389,14 +389,14 @@ module.exports = { console.error(e); }); - /*mdFitbit.getYesterdayFitbit() + mdFitbit.getYesterdayFitbit() .then((d) => { todayCache.data.fitbit = d; }) .catch((e) => { 'use strict'; console.error(e); - });*/ + }); todayCache.date = breakDay(); }, broadcast: function () { diff --git a/lib/today/fitbit.js b/lib/today/fitbit.js index 1e4effc..129090f 100644 --- a/lib/today/fitbit.js +++ b/lib/today/fitbit.js @@ -1,21 +1,20 @@ -var jsonfile = require('jsonfile'); -var config = require('../../config/config.json'); -var Fitbit = require('fitbit-oauth2'); -var dateFormat = require('dateformat'); +const jsonfile = require('jsonfile'); +const config = require('../../config/config.json'); +const Fitbit = require('fitbit-oauth2'); -var logger = require('log4js').getLogger(); +const logger = require('log4js').getLogger(); require('sugar-date'); -var fitbit = new Fitbit(config.fitbit); -var tokenFile = 'fb-token.json'; +const fitbit = new Fitbit(config.fitbit); +const tokenFile = 'fb-token.json'; module.exports = { getYesterdayFitbit: function() { return new Promise(function(resolve, reject) { - var yesterday = Date.create('yesterday').format('{yyyy}-{MM}-{dd}'); - var url = 'https://api.fitbit.com/1/user/-/activities/date/' + yesterday +'.json'; + const yesterday = Date.create('yesterday').format('{yyyy}-{MM}-{dd}'); + const url = 'https://api.fitbit.com/1/user/-/activities/date/' + yesterday + '.json'; logger.info('Getting fitbit for: ', yesterday); logger.debug(url); @@ -26,7 +25,7 @@ logger.info('Getting fitbit for: ', yesterday); if ( err ) { return reject(err); } - var profile = JSON.parse( body ); + const profile = JSON.parse(body); // if token is not null, a refesh has happened and we need to persist the new token if ( token ) jsonfile.writeFile(tokenFile, token, function( err ) { diff --git a/web-server.js b/web-server.js index 18d9ab2..621b1b6 100644 --- a/web-server.js +++ b/web-server.js @@ -156,6 +156,7 @@ app.get('/fitbit', function(req, res) { res.redirect(fitbit.authorizeURL()); }); + app.get( '/fb-profile', function( req, res, next ) { fitbit.request({ uri: "https://api.fitbit.com/1/user/-/profile.json", @@ -174,6 +175,27 @@ app.get( '/fb-profile', function( req, res, next ) { }); }); +app.get( '/fb-today', function( req, res, next ) { + const today = Date.create('today').format('{yyyy}-{MM}-{dd}'); + const url = 'https://api.fitbit.com/1/user/-/activities/date/' + today + '.json'; + + fitbit.request({ + uri: url, + method: 'GET', + }, function( err, body, token ) { + if ( err ) return next( err ); + var profile = JSON.parse( body ); + // if token is not null, a refesh has happened and we need to persist the new token + if ( token ) + persist.write( tfile, token, function( err ) { + if ( err ) return next( err ); + res.send( '
' + JSON.stringify( profile, null, 2 ) + '
' ); + }); + else + res.send( '
' + JSON.stringify( profile, null, 2 ) + '
' ); + }); +}); + // Callback service parsing the authorization token and asking for the access token. This // endpoint is refered to in config.fitbit.authorization_uri.redirect_uri. See example // config below.