Skip to content

Commit

Permalink
clarify code vs. refresh token and use workflow config
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmetcalfe committed Sep 12, 2022
1 parent 47c635a commit c2dab56
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 30 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This workflow requires three environment variables to connect to Strava. These v

- `CLIENT_ID`
- `CLIENT_SECRET`
- `REFRESH_TOKEN`
- `CODE`

### Follow these steps to get the values for these variables

Expand All @@ -24,7 +24,7 @@ This workflow requires three environment variables to connect to Strava. These v

4. Click "Authorize".

5. Inspect the URL in the search bar and copy the value for `code`. This is the value for `REFRESH_TOKEN`.
5. Inspect the URL in the search bar and copy the value for `code`. This is the value for `CODE`.

6. That's it! Enter those values into the workflow configuration.

Expand All @@ -34,11 +34,10 @@ The default keyword is "strava". After entering this keyword, hit 'return' and a

![screenshot](./screenshot.png "screenshot")


## Optional Parameters

To use metric units, set `METRIC_UNITS` to `true`.

## License

MIT
MIT
Binary file modified Strava.alfredworkflow
Binary file not shown.
74 changes: 57 additions & 17 deletions index.js
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);
148 changes: 139 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"alfy": "^1.0.0",
"moment": "^2.29.4",
"node-fetch": "^3.2.10",
"strava": "^2.0.1"
}
}

0 comments on commit c2dab56

Please sign in to comment.