-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
179 lines (146 loc) · 4.77 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var flatiron = require('flatiron'),
path = require('path'),
plates = require('plates'),
app = flatiron.app,
request = require('request'),
qs = require('querystring'),
_ = require('underscore'),
mongo = require('mongodb'),
moment = require('moment');
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/statusboard';
var CONSUMER_KEY = process.env.FITBIT_KEY;
var CONSUMER_SECRET = process.env.FITBIT_SECRET;
//app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
app.use(flatiron.plugins.http);
app.router.get('/', function () {
this.res.json({ 'hello': 'world' });
});
var oauth = {
callback: process.env.AUTH_CALLBACK,
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET
};
app.router.get('/auth', function() {
var res = this.res;
var url = 'https://api.fitbit.com/oauth/request_token';
request.post({url:url, oauth:oauth}, function (e, r, body) {
var access_token = qs.parse(body);
oauth = {
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
token: access_token.oauth_token
};
var url = 'https://www.fitbit.com/oauth/authorize?';
url += 'oauth_token=' + access_token.oauth_token;
var html = '<a href="/">Connect to Fitbit</a>';
var data = { "authUrl": url};
var map = plates.Map();
map.where('href').is('/').insert('authUrl');
var output = plates.bind(html, data, map);
res.end(output);
});
});
app.router.get('/auth/fitbit', function() {
var url = 'https://api.fitbit.com/oauth/access_token';
var res = this.res;
var access_token = this.req.query;
oauth = {
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
token: access_token.oauth_token,
verifier: access_token.oauth_verifier
};
request.post({url:url, oauth:oauth}, function (e, r, body) {
var perm_token = qs.parse(body);
oauth = {
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
token: perm_token.oauth_token,
token_secret: perm_token.oauth_token_secret
};
url = 'http://api.fitbit.com/1/user/-/profile.json';
request.get({url:url, oauth:oauth, json:true}, function (e, r, response) {
var doc = {
type: 'fitbit',
token: perm_token.oauth_token,
secret: perm_token.oauth_token_secret
};
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('tokens', function(er, collection) {
collection.insert(doc, {safe: true}, function(er,rs) {
res.end(JSON.stringify(response));
});
});
});
});
});
});
app.router.get('/fitbit', function() {
var res = this.res;
var url = 'http://api.fitbit.com/1/user/-/activities/steps/date/today/7d.json';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('tokens', function(er, collection) {
collection.findOne({type: 'fitbit'}, function(er,rs) {
oauth = {
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
token: rs.token,
token_secret: rs.secret
};
request.get({url:url, oauth:oauth, json:true}, function (e, r, data) {
var steps = _.map(data['activities-steps'], function(ob, key) {
return {title: moment(ob.dateTime).format('dddd'), value: ob.value};
});
var response = {
graph: {
title: "Fitbit",
datasequences: [
{
title: "Steps",
datapoints: steps
}
]
}
};
res.end(JSON.stringify(response));
});
});
});
});
});
app.router.get('/sleep', function() {
var res = this.res;
var url = 'http://api.fitbit.com/1/user/-/sleep/minutesAsleep/date/today/7d.json';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('tokens', function(er, collection) {
collection.findOne({type: 'fitbit'}, function(er,rs) {
oauth = {
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
token: rs.token,
token_secret: rs.secret
};
request.get({url:url, oauth:oauth, json:true}, function (e, r, data) {
var sleep = _.map(data['sleep-minutesAsleep'], function(ob, key) {
return {title: moment(ob.dateTime).format('dddd'), value: Math.round((ob.value / 60) * 100) / 100 };
});
var response = {
graph: {
title: "Fitbit",
datasequences: [
{
title: "Sleep",
datapoints: sleep
}
]
}
};
res.end(JSON.stringify(response));
});
});
});
});
});
app.start(process.env.PORT);