-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (231 loc) · 9.02 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const strava = require('strava-v3');
const express = require('express');
const fs = require('fs');
const path = require('path');
const opn = require('opn');
const app = express();
var request = require('request-promise');
var request = request.defaults({jar: true, simple: false});
process.env.STRAVA_ACCESS_TOKEN = "empty";
process.env.STRAVA_CLIENT_ID = "24949";
process.env.STRAVA_CLIENT_SECRET = "efa786ccc73e8d9d61f8a180f4cba2fe1a430b83";
process.env.STRAVA_REDIRECT_URI = "http://localhost:3000/exchange_token";
jsonFormater = (json) => {
return JSON.stringify(creds).replace(/,/g, ',\n ')
.replace(/\{/g, '{\n ')
.replace(/\}/g, '\n}')
.replace(/\:/g, ': ');
}
// read creds from file
var jsonPath = path.join(__dirname, '/credentials.json');
try {
var creds = fs.readFileSync(jsonPath).toString();
} catch (err) {
console.error('ERROR: Could not find credentials.json');
process.exit(1);
}
// parse file content
try {
creds = JSON.parse(creds);
} catch (err) {
console.error('ERROR: Could not parse credentials.json');
process.exit(1);
}
noGui = () => {
request.post({ url: 'https://we-row.mynohrd.com/login', form: {email: creds.WEROW_EMAIL, password: creds.WEROW_PASSWORD} })
.then(function (body) {
request.get('https://we-row.mynohrd.com/history/races')
.then(function (body) {
try {
races = JSON.parse(body);
} catch (err) {
console.error('ERROR: we-row credentials are most likely wrong');
process.exit(1);
}
handleRaceIndex(races);
})
.catch(function (err) {
console.error('ERROR: Problem getting we-row races');
process.exit(1);
});
})
.catch(function (err) {
console.error('ERROR: Problem logging into we-row');
process.exit(1);
});
}
if (creds.STRAVA_ACCESS_TOKEN == "") {
app.listen(3000)
// no strava access token, start oauth flow
opn('http://localhost:3000/');
console.log('INFO: If your browser didnt open, please visit http://localhost:3000')
} else if (creds.WEROW_EMAIL == "" || creds.WEROW_PASSWORD == "") {
app.listen(3000)
// start we-row auth process
opn('http://localhost:3000/exchange_token');
console.log('INFO: If your browser didnt open, please visit http://localhost:3000/exchange_token')
} else {
noGui();
}
app.get('/', function (req, res) {
if (creds.STRAVA_ACCESS_TOKEN == "") {
// oauth flow
const oauthUrl = strava.oauth.getRequestAccessURL({scope:"view_private,write"})
res.redirect(307, oauthUrl);
} else {
// start we-row auth process
res.redirect(307, '/exchange_token');
}
})
app.get('/exchange_token', function (req, res) {
if (typeof req.query.code !== 'undefined') {
// we came here from strava
strava.oauth.getToken(req.query.code,function(err,payload,limits) {
if(!err) {
creds.STRAVA_ACCESS_TOKEN = payload.access_token;
process.env.STRAVA_ACCESS_TOKEN = payload.access_token;
fs.writeFileSync(jsonPath, jsonFormater(creds), function(err) {
if(err) {
console.error('ERROR: Could not write credentials.json');
process.exit(1);
}
});
} else {
console.error('ERROR: Problem with OAuth flow (see messagein browser)');
res.send(err);
}
});
}
if (creds.WEROW_EMAIL == "" || creds.WEROW_PASSWORD == "") {
// we-row auth process
res.send(`
<html><body>
<p>All good. Now please enter your We-Row account details so we can fetch the data.</p>
<form action="/werow" method="get">
<input placeholder="Your e-mail address" value="" name="email" type="email" id="email" autocomplete="on">
<input placeholder="Your password" name="password" type="password" value="" id="password" autocomplete="off">
<br/><button type="submit">Submit</button>
</form>
</html></body>
`);
} else {
res.redirect(307, '/werow?email=' + creds.WEROW_EMAIL + '&password=' + creds.WEROW_PASSWORD);
}
})
app.get('/werow', function (req, res) {
let email = req.query.email;
let password = req.query.password;
request.post({ url: 'https://we-row.mynohrd.com/login', form: {email: email, password: password} })
.then(function (body) {
request.get('https://we-row.mynohrd.com/history/races')
.then(function (body) {
try {
races = JSON.parse(body);
} catch (err) {
console.error('ERROR: we-row credentials are most likely wrong');
process.exit(1);
}
creds.WEROW_EMAIL = email;
creds.WEROW_PASSWORD = password;
fs.writeFileSync(jsonPath, jsonFormater(creds), function(err) {
if(err) {
console.error('ERROR: Could not write credentials.json');
process.exit(1);
}
});
handleRaceIndex(races);
res.send(`
<html><body>
<p>Working ...</p>
<p>You can close this window now and should move your attention to the command line.</p>
</html></body>
`);
})
.catch(function (err) {
console.error('ERROR: Problem getting we-row races');
process.exit(1);
});
})
.catch(function (err) {
console.error('ERROR: Problem logging into we-row');
process.exit(1);
});
})
handleRaceIndex = async (races) => {
var raceArray = await Promise.all(races.map(async (race) => {
if (race.state == "finished") {
return await request.get("https://we-row.mynohrd.com/history/races/data/" + race.id)
}
}));
opn('https://www.strava.com/athlete/training_activities?activity_type=Rowing');
console.log('INFO: If your browser didnt open, please visit https://www.strava.com/athlete/training_activities?activity_type=Rowing')
console.log(raceArray.filter(race => race).length + ' Activities found on we-row');
// for (const [index, el] of raceArray.entries()) {
raceArray.forEach(function(el, index) {
try {
var race = JSON.parse(el);
} catch {
return;
}
var date = new Date(race.started_at);
var offset = date.getTimezoneOffset()/60;
var start_date_local = date.setHours(date.getHours()-offset);
var start_date_local = date.toISOString();
var elapsed_time = Math.floor(race.time/1000);
var dataPoints = 0;
var speed = 0;
var strokeRate = 0;
for (var j in race.data.raceData) {
dataPoints++;
speed = speed + race.data.raceData[j].speed;
strokeRate = strokeRate + race.data.raceData[j].stroke_rate;
}
var speedAvg = Number(Math.round(speed / dataPoints+'e2')+'e-2');
var strokeRateAvg = Number(Math.round(strokeRate / dataPoints+'e2')+'e-2');
var desc = 'Average Speed: ' + speedAvg + " m/s\nAverage Stroke Rate: " + strokeRateAvg;
var args = {
'access_token': creds.STRAVA_ACCESS_TOKEN,
'before': parseInt(String(race.started_at).substring(0,10))+1,
'after': parseInt(String(race.started_at).substring(0,10))-1,
};
// Does acticity already exist?
strava.athlete.listActivities(args, function(err,payload,limits) {
if(!err) {
// is new activity
if (payload.length == 0) {
console.log('session not found on strava, uploading...');
createArgs = {
'access_token': creds.STRAVA_ACCESS_TOKEN,
'name': 'Rowing Session',
'type': 'Rowing',
'start_date_local': start_date_local,
'elapsed_time': elapsed_time,
'description': desc,
'distance': race.distance,
'private': true,
};
strava.activities.create(createArgs, function(err,payload,limits) {
if(!err) {
updateArgs = {
'access_token': creds.STRAVA_ACCESS_TOKEN,
'id': payload.id,
'trainer': 1,
};
strava.activities.update(updateArgs, function(err,payload,limits) {
if(err) {
console.error('ERROR: Problem while updating activity.');
}
});
} else {
console.error('ERROR: Problem while creating activity.');
}
});
} else {
console.log('session already uploaded, skipping...');
}
} else {
console.error('ERROR: Problem while reading activities.');
}
});
});
}