Browse Source

fitbit api aggregation

master
jrtechs 3 years ago
parent
commit
2f772230ae
3 changed files with 197 additions and 3 deletions
  1. +0
    -0
      python/parseTCX.py
  2. +182
    -3
      server.js
  3. +15
    -0
      utils.js

+ 0
- 0
python/parseTCX.py View File


+ 182
- 3
server.js View File

@ -15,8 +15,6 @@ const utils = require("./utils.js");
const config = utils.getFileAsJSON("config.json"); const config = utils.getFileAsJSON("config.json");
app.use(passport.initialize()); app.use(passport.initialize());
app.use(passport.session({ app.use(passport.session({
resave: false, resave: false,
@ -114,9 +112,107 @@ app.get('/steps', (request, result)=>
}); });
}); });
//2021-06-01T00:00:00
app.get('/list-activities/:startTime', (request, result)=>
{
var startTime = request.params.startTime;
fetchActivities(result, startTime).then((data)=>
{
result.writeHead(200, {'Content-Type': 'text/json'});
result.write(JSON.stringify(data));
result.end();
}).catch((error)=>
{
console.log(error);
result.writeHead(500, {'Content-Type': 'text/json'});
result.end();
});
});
function fetchActivities(result, startTime)
{
return new Promise((resolve, reject)=>
{
queryAPI(result, 'https://api.fitbit.com/1/user/-/activities/list.json?beforeDate=' + startTime + '&sort=desc&offset=0&limit=100').then((data)=>
{
if(data != false)
{
resolve(data.activities);
}
reject("Error with API, are you authenticated")
});
});
}
//2021-06-01T00:00:00
//2020-04-06T17:16:19.000-04:00
function fetchAllRuns(result, startTime)
{
var runs = [];
console.log(startTime);
return new Promise((resolve, reject)=>
{
fetchActivities(result, startTime).then((events)=>
{
if(events.length < 10)
{
resolve(runs);
}
else
{
for(var i = 0; i < events.length; i++)
{
if(events[i].logType === "mobile_run")
{
console.log(events[i]);
runs.push(events[i]);
}
}
var newStart = events[events.length -1].startTime.slice(0, -10);
fetchAllRuns(result, newStart).then((run_rec)=>
{
resolve(runs.concat(run_rec));
}).catch((err)=>
{
reject(err);
});
}
}).catch((error)=>
{
reject(error);
});
});
}
//2021-06-01T00:00:00
app.get('/all-logged-runs', (request, result)=>
{
var startTime = '2030-06-01T00:00:00';
fetchAllRuns(result, startTime).then((data)=>
{
result.writeHead(200, {'Content-Type': 'text/json'});
result.write(JSON.stringify(data));
result.end();
}).catch((error)=>
{
console.log(error);
result.writeHead(500, {'Content-Type': 'text/json'});
result.end();
});
});
app.get('/activities', (request, result)=> app.get('/activities', (request, result)=>
{ {
queryAPI(result, 'https://api.fitbit.com/1/user/-/activities/date/1M.json').then((data)=>
queryAPI(result, 'https://api.fitbit.com/1/user/-/activities/recent.json').then((data)=>
{ {
if(data != false) if(data != false)
{ {
@ -132,6 +228,89 @@ app.get('/activities', (request, result)=>
}); });
function fetchTCX(result, tcxID)
{
console.log("Fetching tcx event: " + tcxID);
return new Promise((resolve, reject)=>
{
queryAPI(result, 'https://api.fitbit.com/1/user/-/activities/' + tcxID + ".tcx").then((data)=>
{
if(data != false)
{
resolve(data);
}
reject("Error with API, are you authenticated")
});
});
}
function saveTCX(result, tcxID)
{
return new Promise((resolve, reject)=>
{
fetchTCX(result, tcxID).then((tcx)=>
{
utils.saveFile(tcxID + ".tcx", tcx);
resolve();
}).catch((err)=>
{
reject(err);
})
});
}
app.get('/save-all-tcx', (request, result)=>
{
var tcxID = request.params.id;
var startTime = '2030-06-01T00:00:00';
fetchAllRuns(result, startTime).then((data)=>
{
var promises = [];
for(var i =0; i < data.length; i++)
{
promises.push(saveTCX(result, data[i].logId));
}
Promise.all(promises).then(function(content)
{
result.write("All events saved");
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
}).catch((error)=>
{
console.log(error);
result.writeHead(500, {'Content-Type': 'text/json'});
result.end();
});
});
app.get('/tcx/:id', (request, result)=>
{
var tcxID = request.params.id;
fetchTCX(result, tcxID).then((data)=>
{
result.writeHead(200, {'Content-Type': 'text/xml'});
result.write(data);
result.end();
}).catch((error)=>
{
console.log(error);
result.writeHead(500, {'Content-Type': 'text/json'});
result.end();
});
});
app.get('/', (request, result) => app.get('/', (request, result) =>
{ {
result.write(utils.getFile("index.html")); result.write(utils.getFile("index.html"));

+ 15
- 0
utils.js View File

@ -16,5 +16,20 @@ module.exports=
getFile: function(filename) getFile: function(filename)
{ {
return fs.readFileSync(filename, 'utf8'); return fs.readFileSync(filename, 'utf8');
},
saveFile: function(filename, contents)
{
fs.writeFile(filename, contents, function(err)
{
if(err)
{
console.log(err);
}
else
{
console.log(filename + " saved");
}
});
} }
}; };

Loading…
Cancel
Save