This repository was archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (140 loc) · 4.96 KB
/
index.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
var strava = require('./lib/strava');
var oauth = require('./lib/oauth');
var _ = require('lodash');
var moment = require('moment');
function getLastStravaDays(token, startMoment) {
return strava.listActivities({
after: startMoment.unix(),
access_token: token
}).then(function groupByActivity (activities) {
return _.groupBy(activities, 'type')
});
}
function metersToKms (number) {
return (Math.round(number /100)) /10
}
function fillDates(days, startMoment, endMoment) {
var dates = [];
var interval = endMoment ? endMoment.diff(startMoment, 'days') : 28;
var current = startMoment, i, value;
for (i = 0; i < interval; i++) {
value = days[current.format('YYYY-MM-DD')] || 0;
dates.push(metersToKms(value));
current = current.add(1, 'days');
}
return dates;
}
function calcStats (startMoment) {
return function (activitiesByType) {
return _.transform(activitiesByType, function (result, activities, type) {
var _days = _.chain(activities)
.groupBy(function getActivityStartDate (activity) {
return activity.start_date.substring(0,10);
})
.mapValues(function sumAllDistancesForDay(dayActivities) {
return _.reduce(dayActivities, function addDistance(sum, act){
return sum + act.distance
}, 0);
});
result[type] = {
longest: metersToKms(_days
.values()
.max()
.value()),
total: metersToKms(_days
.reduce(function (sum, activity) { return sum + activity;})
.value()),
climb: Math.round(_.reduce(activities, function (sum, activity) { return sum + activity.total_elevation_gain }, 0)),
days: fillDates(_days.value(), startMoment)
}
});
};
};
function getToken(req) {
return req.cookies[COOKIE_NAME] || req.query.token;
}
var express = require('express');
var app = express();
app.set('view engine', 'jade');
app.use(require('cookie-parser')());
app.use(require('express-session')({
resave: false,
saveUninitialized: false,
secret: process.env.SESSION_SECRET || 'asdf'
}));
app.use(require('connect-flash')());
app.use(require('helmet')());
app.use(express.static('public', {}));
var COOKIE_NAME = 'strvtkn';
var OAUTH_INIT_PATH = '/oauth/init';
var PORT = process.env.PORT || 3000;
var APP_HOST = process.env.APP_HOST || 'http://localhost:' + PORT;
// MIDDLEWARE
//Check user status
app.use(function checkKnownUser(req, res, next) {
console.log('req', req.cookies);
if (getToken(req)) {
//ok
next();
} else if (!req.query.code) {
var authUrl = 'https://www.strava.com/oauth/authorize?client_id=' + process.env.STRAVA_CLIENT_ID +
'&response_type=code' +
'&redirect_uri=' + APP_HOST + OAUTH_INIT_PATH;
console.log(authUrl);
res.render('index', {
authUrl: authUrl
});
} else if (req.query.code) {
console.log('New session!');
next();
} else {
console.warn('Unexpected', req.url);
}
});
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something broke!');
});
//ROUTES
app.get(OAUTH_INIT_PATH, function initUser(req, res) {
console.log('Init oauth', req.query.code);
oauth.getToken(req.query.code)
.then(function (tokenResponse) {
var body = JSON.parse(tokenResponse.body);
if (tokenResponse.statusCode === 200) {
res.cookie(COOKIE_NAME, body.access_token, { maxAge: 60 * 60 * 24 * 365 * 1000 }); //Express uses millis for maxAge.
req.flash('token', body.access_token);
res.redirect('/');
} else {
throw {
code: tokenResponse.statusCode, response: tokenResponse.body
};
}
})
.catch(function (e) {
console.error('Error exchanging code for token', e);
res.status(500).send('Error exchanging code for token');
})
});
app.get('/', function displayData(req, res) {
var twentyEightDaysAgo = moment()
.startOf('day')
.subtract(27, 'days');
getLastStravaDays(getToken(req), twentyEightDaysAgo)
.then(calcStats(twentyEightDaysAgo))
.then(function (data) {
var token = req.flash('token');
console.log('Got data', data, token);
res.render('stats', {data: data, token: token});
})
.catch(function (e) {
console.error('ERROR', e);
res.status(500).send('Error rendering stats');
});
});
//STARTUP
var server = app.listen(PORT, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});