-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
287 lines (221 loc) · 9.71 KB
/
index.ts
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import dotenv = require('dotenv');
dotenv.config();
import request = require('request-promise-native');
import _authorize = require('strava-v3-cli-authenticator');
const authorize = async (options: any): Promise<string> => {
return new Promise<string>((resolve, reject) => {
_authorize(options, (error, value) => {
if (error) {
reject(error);
} else {
resolve(value);
}
});
});
}
import * as Strava from './strava';
import { WeatherResponse, WeatherSnapshot } from './types/darksky-api';
import * as inquirer from 'inquirer';
import { Question } from 'inquirer';
import { ActivityId, AuthToken, UserId } from './models';
import { DataProvider } from './dataProvider/dataProvider';
const dataProvider = new DataProvider();
dataProvider.init();
const DARK_SKY_API_KEY = process.env.DARK_SKY_API_KEY;
let CURRENT_TOKEN: AuthToken;
let CURRENT_USER_ID: UserId;
const getUserToken = async (): Promise<AuthToken> => {
if (!CURRENT_TOKEN) {
const token = await authorize({
clientId: process.env.STRAVA_CLIENT_ID,
clientSecret: process.env.STRAVA_CLIENT_SECRET,
scope: 'write',
httpPort: 8888
}).catch(console.error);
if (token) {
CURRENT_TOKEN = token;
}
}
return CURRENT_TOKEN;
}
const getUserId = async (): Promise<UserId> => {
if (!CURRENT_USER_ID) {
const token = await getUserToken();
CURRENT_USER_ID = await getUserIdForToken(token);
}
return CURRENT_USER_ID;
}
const getUserIdForToken = async (token: AuthToken): Promise<UserId> => {
const storedUserId = await dataProvider.getUserIdForToken(token);
if (storedUserId) {
return storedUserId;
}
const athleteApi = new Strava.AthletesApi();
athleteApi.accessToken = token;
const loggedInAthlete = await athleteApi.getLoggedInAthlete();
const userId = loggedInAthlete.body.id
await dataProvider.storeUserIdForToken(token, userId);
return userId;
}
const getTokenForUserId = async (userId: UserId): Promise<AuthToken> => {
return dataProvider.getTokenForUserId(userId);
}
const getActivitiesForToken = async (token: AuthToken): Promise<Strava.SummaryActivity[]> => {
const activitiesApi = new Strava.ActivitiesApi();
activitiesApi.accessToken = token;
const activitiesReponse = await activitiesApi.getLoggedInAthleteActivities()
.catch(console.error);
return activitiesReponse && activitiesReponse.body;
}
const getActivitiesForUserId = async (userId: UserId): Promise<Strava.SummaryActivity[]> => {
const token = await getTokenForUserId(userId)
.catch(console.error);
return token && getActivitiesForToken(token);
}
const getDetailedActivity = async (token: AuthToken, activityId: ActivityId): Promise<Strava.DetailedActivity> => {
const activitiesApi = new Strava.ActivitiesApi();
activitiesApi.accessToken = token;
const activityResponse = await activitiesApi.getActivityById(activityId)
.catch(console.error);
return activityResponse && activityResponse.body;
}
const updateActivity = async (token: AuthToken, userId: UserId, activityId: ActivityId, update: Strava.UpdatableActivity): Promise<void> => {
console.log(`Update activity:\nToken: ${token}\nUserId: ${userId}\nactivityId: ${activityId}\nUpdate: ${JSON.stringify(update)}`);
const activitiesApi = new Strava.ActivitiesApi();
activitiesApi.accessToken = token;
const response = await activitiesApi.updateActivityById(activityId, update)
.catch(console.error);
if (response) {
await dataProvider.storeProcessedActivity(activityId, userId);
}
}
const getWeatherFromDetailedRun = async (run: Strava.DetailedActivity): Promise<WeatherSnapshot> => {
const url = `https://api.darksky.net/forecast/${DARK_SKY_API_KEY}/${run.startLatlng[0]},${run.startLatlng[1]},${Math.floor(Number(run.startDate) / 1000)}`;
console.log(url);
const response = await request(url)
.catch(console.error);
if (!response) {
return;
}
const weatherResponse = JSON.parse(response) as WeatherResponse;
return weatherResponse && weatherResponse.currently;
}
const getDescriptionFromWeather = (weather: WeatherSnapshot): string => {
const strings = [];
strings.push(`Weather Summary: ${weather.summary}`);
strings.push(`Temperature: ${tempToString(weather.temperature)}`);
const heatIndexDiff = Math.abs(weather.apparentTemperature - weather.temperature);
if (heatIndexDiff > 10) {
strings.push(`Felt Like: ${tempToString(weather.apparentTemperature)}`);
}
strings.push(`Humidity: ${weather.humidity * 100}%`);
if (weather.uvIndex >= 7) {
strings.push(`UV Index: ${weather.uvIndex}`);
}
strings.push(`Wind Speed: ${speedToString(weather.windSpeed)}`);
// Does not have to be an absolute value
const windGustDiff = weather.windSpeed - weather.windGust;
if (windGustDiff > 5) {
strings.push(`Gusts up to: ${speedToString(weather.windGust)}`);
}
return strings.join('\n');
}
const getUpdatedDescriptionForActivityId = async (token: AuthToken, activityId: number) => {
const mostRecentActivityDetails = await getDetailedActivity(token, activityId)
.catch(console.error);
if (!mostRecentActivityDetails) return;
const weatherDetails = await getWeatherFromDetailedRun(mostRecentActivityDetails)
.catch(console.error);
if (!weatherDetails) return;
const weatherDescription = getDescriptionFromWeather(weatherDetails);
// If the comment already contains the weather information, don't add it again
if (mostRecentActivityDetails.description && mostRecentActivityDetails.description.indexOf("Weather Summary") >= 0) {
return mostRecentActivityDetails.description;
}
const newComment = [mostRecentActivityDetails.description, weatherDescription].join('\n\n').trim();
return newComment;
}
// STARTREGION Weather Utils
const farenheitToCelcius = (temp: number): string => {
return getRoundedString((temp - 32) * (5 / 9), 2);
}
const tempToString = (temp: number): string => {
return `${temp}° (${farenheitToCelcius(temp)}C)`;
}
const speedToString = (speed: number): string => {
return `${speed} mph (${getRoundedString(speed * 0.44704, 2)} m/s)`;
}
function getRoundedString(value: number | string, decimals: number): string {
return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals).toFixed(decimals);
}
// ENDREGION Weather Utils
// STARTREGION Prompts
const listProcessedActivities = async () => {
const userId = await getUserId();
const processedActivities = await dataProvider.getProcessedActivities(userId);
console.log('Processed Activities:');
processedActivities.map(console.log);
}
const startUpdateActivitiesPrompt = async () => {
const token = await getUserToken();
const userId = await getUserId();
if (!token) {
console.log('Unable to authenticate.');
return;
}
const userActivities = await getActivitiesForToken(token)
.catch(console.error);
if (!Array.isArray(userActivities)) {
console.log('Error fetching activities.');
return;
}
const processedActivities = await dataProvider.getProcessedActivities(userId);
const unprocessedUserActivities = userActivities.filter(activity => processedActivities.indexOf(activity.id) < 0);
const activityOptions = unprocessedUserActivities.map(activity => {
return {
name: `${activity.startDateLocal} - ${activity.name}`,
value: activity.id,
} as inquirer.ChoiceType;
});
const whichActivityQuestion: Question = {
name: 'which_activity',
type: 'list',
message: 'which activity would you like to update?',
choices: activityOptions
}
const whichActivityAnswer = await inquirer.prompt(whichActivityQuestion)
.catch(console.error);
const selectedActivity = whichActivityAnswer[whichActivityQuestion && whichActivityQuestion.name];
if (!selectedActivity) {
console.log(`I didn't understand that response.`);
return;
}
const newDescription = await getUpdatedDescriptionForActivityId(token, selectedActivity)
.catch(console.error);
if (!newDescription) {
console.log('Unable to generate a new description.');
return;
}
const confirmDescriptionQuestion: Question = {
name: 'confirm_description',
type: 'confirm',
message: ['Would you like to update activity to have this description?', newDescription].join('\n')
};
const confirmDescriptionAnswer = await inquirer.prompt(confirmDescriptionQuestion)
.catch(console.error);
const newDescriptionConfirmed = confirmDescriptionAnswer[confirmDescriptionQuestion && confirmDescriptionQuestion.name];
if (newDescriptionConfirmed === undefined) {
console.log(`I didn't understand that response.`);
return;
}
if (newDescriptionConfirmed) {
console.log('Updating activity...');
const updatedActivity: Strava.UpdatableActivity = { description: newDescription };
await updateActivity(token, userId, selectedActivity, updatedActivity)
.then(() => console.log(`Activity updated! See it here https://www.strava.com/activities/${selectedActivity}`))
.catch(console.error)
}
startUpdateActivitiesPrompt();
}
// ENDREGION Prompts
startUpdateActivitiesPrompt();