-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clarify code vs. refresh token and use workflow config
- Loading branch information
1 parent
47c635a
commit c2dab56
Showing
5 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,79 @@ | ||
import alfy from 'alfy'; | ||
import { Strava } from 'strava'; | ||
import alfy from "alfy"; | ||
import { Strava } from "strava"; | ||
import fs from "fs"; | ||
import fetch from "node-fetch"; | ||
import moment from "moment"; | ||
|
||
const METERS_TO_MILES = 0.0006213712; | ||
const TOKEN_PATH = "./token.json"; | ||
|
||
const formatTime = (date) => { | ||
const dateString = moment(date).format("dddd, MMMM DD, YYYY"); | ||
const timeString = moment(date).format("H:mmA"); | ||
return `${timeString} on ${dateString}`; | ||
} | ||
}; | ||
|
||
const formatDistance = (distance) => { | ||
if (process.env.METRIC_UNITS) { | ||
return `${(distance / 1000).toFixed(1)} km`; | ||
} | ||
|
||
return `${(distance * METERS_TO_MILES).toFixed(1)} mi`; | ||
} | ||
}; | ||
|
||
const getRefreshToken = async () => { | ||
const url = `https://www.strava.com/oauth/token?client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}&code=${process.env.CODE}&grant_type=authorization_code`; | ||
const response = await fetch(url, { | ||
method: "POST", | ||
}); | ||
return response.json(); | ||
}; | ||
|
||
const loadRefreshToken = async () => { | ||
let token; | ||
try { | ||
const data = JSON.parse(fs.readFileSync(TOKEN_PATH, "utf8")); | ||
token = data.refresh_token; | ||
} catch (e) { | ||
const data = await getRefreshToken(); | ||
fs.writeFileSync( | ||
TOKEN_PATH, | ||
JSON.stringify({ refresh_token: data.refresh_token }) | ||
); | ||
} | ||
|
||
const strava = new Strava({ | ||
client_id: process.env.CLIENT_ID, | ||
client_secret: process.env.CLIENT_SECRET, | ||
refresh_token: process.env.REFRESH_TOKEN, | ||
}) | ||
return token; | ||
}; | ||
|
||
const activities = await strava.activities.getLoggedInAthleteActivities() | ||
const refreshToken = await loadRefreshToken(); | ||
|
||
const items = alfy | ||
.inputMatches(activities, 'name') | ||
.map(activity => { | ||
let output; | ||
|
||
if (!!refreshToken) { | ||
const strava = new Strava({ | ||
client_id: process.env.CLIENT_ID, | ||
client_secret: process.env.CLIENT_SECRET, | ||
refresh_token: refreshToken, | ||
}); | ||
|
||
const activities = await strava.activities.getLoggedInAthleteActivities(); | ||
|
||
output = alfy.inputMatches(activities, "name").map((activity) => { | ||
return { | ||
title: activity.name, | ||
subtitle: `${formatDistance(activity.distance)} (${formatTime(activity.start_date)})`, | ||
arg: activity.id | ||
} | ||
subtitle: `${formatDistance(activity.distance)} (${formatTime( | ||
activity.start_date | ||
)})`, | ||
arg: activity.id, | ||
}; | ||
}); | ||
} else { | ||
output = [ | ||
{ | ||
title: "Please check your workflow configuration!", | ||
subtitle: "Confirm your values for CLIENT_ID, CLIENT_SECRET, and CODE.", | ||
}, | ||
]; | ||
} | ||
|
||
alfy.output(items); | ||
alfy.output(output); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"dependencies": { | ||
"alfy": "^1.0.0", | ||
"moment": "^2.29.4", | ||
"node-fetch": "^3.2.10", | ||
"strava": "^2.0.1" | ||
} | ||
} |