-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (65 loc) · 2.21 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
const axios = require('axios')
const auth = require('./auth.json')
const { DARKSKY_API_KEY } = require('./config.json')
// For interfacing with the Strava API.
const Strava = axios.create({
baseURL: 'https://www.strava.com/api/v3/',
headers: {
'Authorization': `Bearer ${auth.access_token}`
}
})
// For interfacing with the Dark Sky API.
const DarkSky = axios.create({
baseURL: `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/`
})
// Tuple of (latitude, longitude, timestamp) representing the specific location
// and time of the activity.
const activityLoctime = activity => (
[...activity.start_latlng, Math.floor(Date.parse(activity.start_date) / 1000)]
)
// Draw ASCII arrow in the direction the wind is blowing.
const arrow = bearing => (
'↓↙←↖↑↗→↘↓'[Math.round(bearing / 45)]
)
// Wind speed and gust (only display gust if faster than average wind speed).
const windDetails = (speed, gust) => (
`${speed} km/h${gust > speed ? ` (${gust} km/h gust)` : ''}`
)
// Generate stamp from weather conditions. Example output:
// 23.31 °C | 4.78 km/h (22.35 km/h gust) ↗
const createStamp = ({ temperature, windSpeed, windGust, windBearing }) => (
[
`${temperature} °C`,
`${windDetails(windSpeed, windGust)} ${arrow(windBearing)}`
].join(' | ')
)
// Get the athlete's most recent activity.
const getLatestActivity = () => (
Strava.get('athlete/activities', { params: { per_page: 1 }}).then(res => (
res.data[0]
))
)
// Get the weather conditions at the specified loctime.
const getConditions = loctime => (
DarkSky.get(loctime.join(), { params: { units: 'ca' }}).then(res => (
res.data.currently
))
)
// Update the activity with a weather stamp.
const stampActivity = async activity => {
const conditions = await getConditions(activityLoctime(activity))
const description = createStamp(conditions)
await Strava.put(`activities/${activity.id}`, { description })
return description
}
// Weather stamp the athlete's latest activity.
(async () => {
try {
const activity = await getLatestActivity()
const stamp = await stampActivity(activity)
console.log(activity.name)
console.log(stamp)
} catch(error) {
console.error(error.response.data)
}
})()