Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default defineConfig(({ mode }) => {
server: {
port: VITE_DEV_PORT,
proxy: {
'^/api/': { target: `http://localhost:${SERVER_PORT}`, ws: true },
'^/(api|calendar)': { target: `http://localhost:${SERVER_PORT}`, ws: true },
},
},
build: {
Expand Down
3 changes: 3 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import express, { type RequestHandler } from 'express';
import expressWs from 'express-ws';

import apiRouter from './api';
import calendarRouter from './calendar';

const app = express();
const wsInstance = expressWs(app);
Expand All @@ -25,6 +26,8 @@ app.use(corsConfig);

app.use('/api', apiRouter(SERVER_DOMAIN, wsInstance));

app.use('/calendar', calendarRouter(SERVER_DOMAIN));

// Support history api
app.use(
history({
Expand Down
86 changes: 86 additions & 0 deletions server/calendar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'moment-timezone';

import express from 'express';
import ical from 'ical-generator';
import moment from 'moment-timezone';

import eagerIterator from './eager-iterator';
import { getSportIcon } from './sport-icons';
import { Strava } from './strava';

export default function calendarRouter(domain: string): express.Router {
const router = express.Router();

const mkRequestLogin = (res: express.Response) => async () => {
res.status(403).send('Not logged in');

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Handle logging in

return false;
};

router.get('/', async (req: express.Request, res: express.Response) => {
const token = req.cookies.token;
const requestLogin = mkRequestLogin(res);
if (token && (await new Strava(domain, token, requestLogin).hasToken())) {
const webcalDomain = domain.replace(/\w+(?=:\/\/)/, 'webcal');
const pathWithSlash = req.originalUrl.replace(/\/?$/, '/');
const fullPath = webcalDomain + pathWithSlash + token + '.ics';
res.redirect(fullPath);
} else {
await requestLogin();
}
});

router.get('/:token.ics', async (req: express.Request, res: express.Response) => {
const strava = new Strava(domain, req.params.token, mkRequestLogin(res));

res.type('text/calendar; charset=UTF-8');

const cal = ical({ name: 'Strava activities' });

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Add proper caching of ical (only fetch new activities, e.g. from past week)
  • Clear old caches after certain time
  • Handle logout through GUI
  • Add link to GUI


for await (const activity of eagerIterator(strava.getStravaActivities())) {
const startTime = moment(activity.start_date).tz(activity.timezone.split(' ')[1]);
const location = activity.start_latlng.join(', ');

@c-harding c-harding May 5, 2022 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Consider geocoding here


const twoDigits = (val: number) => String(val.toFixed(0)).padStart(2, '0');

const toHms = (totalSeconds: number) => {
const hours = Math.floor(totalSeconds / 60 / 60),
minutes = Math.floor(totalSeconds / 60) % 60,
seconds = totalSeconds % 60;
return (hours ? [hours, twoDigits(minutes), twoDigits(seconds)] : [minutes, twoDigits(seconds)]).join(':');
};

const descriptionFields = {
'Elapsed time': activity.elapsed_time && `${toHms(activity.elapsed_time)}`,
'Moving time': activity.moving_time && `${toHms(activity.moving_time)}`,
Distance: activity.distance && `${(activity.distance / 1000).toFixed(2)} km`,
'Elevation gain': activity.total_elevation_gain && `${activity.total_elevation_gain.toFixed(0)} m`,
'Average speed':
activity.average_speed &&
activity.sport_type !== 'Run' &&
`${(activity.average_speed * 3.6).toFixed(1)} km/h`,
'Average pace':
activity.average_speed && activity.sport_type === 'Run' && `${toHms(1000 / activity.average_speed)}/km`,
'Average heart rate': activity.has_heartrate && `${activity.average_heartrate?.toFixed(0)} bpm`,
'Max heart rate': activity.has_heartrate && `${activity.max_heartrate?.toFixed(0)} bpm`,
};
const descriptionData = Object.entries(descriptionFields)
.filter(([, value]) => value)
.map(([label, value]) => `${label}: ${value}`)
.join('\n');

cal.createEvent({
summary: `${activity.name} ${getSportIcon(activity)}`,
location: location,
timezone: startTime.tz(),
start: startTime,
end: moment(startTime).add(activity.elapsed_time, 'seconds'),
url: `https://strava.com/activities/${activity.id}`,
description: descriptionData,
});
}

cal.serve(res);
});

return router;
}
4 changes: 4 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"cookie-parser": "^1.4.7",
"express": "^5.2.1",
"express-ws": "^5.0.2",
"ical-generator": "^3.4.2",
"moment": "^2.29.4",
"moment-timezone": "^0.5.40",
"node-fetch": "^2.7.0",
"proper-lockfile": "^4.1.2",
"uuid": "^13.0.0"
Expand All @@ -31,6 +34,7 @@
"@types/cookie-parser": "^1.4.7",
"@types/express": "^5.0.6",
"@types/express-ws": "^3.0.6",
"@types/luxon": "^2.3.2",
"@types/node": "^20.19.33",
"@types/node-fetch": "^2.6.13",
"@typescript-eslint/eslint-plugin": "^8.56.0",
Expand Down
80 changes: 80 additions & 0 deletions server/sport-icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { type SummaryActivity } from './strava/model';

export function getSportIcon(source: SummaryActivity): string {
switch (source.sport_type) {
case 'Run':
case 'VirtualRun':
case 'TrailRun':
return '🏃';
case 'Walk':
return '🚶';
case 'Ride':
case 'EBikeRide':
case 'VirtualRide':
case 'Handcycle':
case 'Velomobile':
return '🚲';
case 'Swim':
return '🏊';
case 'AlpineSki':
case 'BackcountrySki':
case 'NordicSki':
return '⛷️';
case 'Snowboard':
return '🏂';
case 'IceSkate':
case 'Snowshoe':
return '⛸️';
case 'Skateboard':
return '🛹';
case 'RockClimbing':
return '🧗';
case 'Surfing':
case 'Windsurf':
case 'Kitesurf':
case 'StandUpPaddling':
return '🏄';
case 'Canoeing':
case 'Kayaking':
case 'Rowing':
case 'VirtualRow':
return '🛶';
case 'Sail':
return '⛵';
case 'Golf':
return '🏌';
case 'Soccer':
return '⚽';
case 'Crossfit':
case 'Elliptical':
case 'WeightTraining':
case 'HighIntensityIntervalTraining':
case 'Pilates':
case 'StairStepper':
return '🏋';
case 'Yoga':
return '🧘';
case 'Wheelchair':
return '🧑‍🦽';
case 'Hike':
return '🥾';
case 'Badminton':
return '🏸';
case 'Tennis':
return '🎾';
case 'EMountainBikeRide':
case 'MountainBikeRide':
case 'GravelRide':
return '🚵';
case 'InlineSkate':
return '🛼';
case 'TableTennis':
case 'Pickleball':
case 'Racquetball':
case 'Squash':
return '🏓';
case 'Workout':
default:
return '';
}
}
6 changes: 5 additions & 1 deletion server/strava/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Strava {
/**
* Fetches your data from the Strava API
*/
async *getStravaActivities(start?: number, end?: number): AsyncGenerator<unknown, void, undefined> {
async *getStravaActivities(start?: number, end?: number): AsyncGenerator<SummaryActivity, void, undefined> {
for await (const page of this.getStravaActivitiesPages(start, end)) {
yield* page;
}
Expand Down Expand Up @@ -99,6 +99,10 @@ export class Strava {
return await this.rawApi.get(`/activities/${id}`);
}

hasToken(): Promise<boolean> {
return this.rawApi.hasToken();
}

async logout(global?: boolean);
async logout(global: true, athlete: number);
async logout(global = false, athlete?: number) {
Expand Down
9 changes: 9 additions & 0 deletions server/strava/raw-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ export default class RawStravaApi {
};
}

async hasToken(): Promise<boolean> {
try {
await this.loadCache();
return true;
} catch (e) {
return false;
}
}

private async rawStravaAPI(endpoint: string, query: Record<string, string | number | undefined> = {}) {
const API_BASE = 'https://www.strava.com/api/v3';
const queryString = (Object.entries(query).filter(([, value]) => value) as [string, string | number][])
Expand Down
72 changes: 72 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ __metadata:
"@types/cookie-parser": "npm:^1.4.7"
"@types/express": "npm:^5.0.6"
"@types/express-ws": "npm:^3.0.6"
"@types/luxon": "npm:^2.3.2"
"@types/node": "npm:^20.19.33"
"@types/node-fetch": "npm:^2.6.13"
"@types/proper-lockfile": "npm:^4.1.4"
Expand All @@ -1337,6 +1338,9 @@ __metadata:
express: "npm:^5.2.1"
express-ws: "npm:^5.0.2"
globals: "npm:^17.3.0"
ical-generator: "npm:^3.4.2"
moment: "npm:^2.29.4"
moment-timezone: "npm:^0.5.40"
node-fetch: "npm:^2.7.0"
nodemon: "npm:^3.1.7"
prettier: "npm:^3.8.1"
Expand Down Expand Up @@ -1547,6 +1551,13 @@ __metadata:
languageName: node
linkType: hard

"@types/luxon@npm:^2.3.2":
version: 2.4.0
resolution: "@types/luxon@npm:2.4.0"
checksum: 10c0/2686d03827a3799f0374923323148f4d0d07f9e4321551389b2ae285e5085e178cfe908806041ab32bf66534497702b4ae8459d1b4190258af133818d4b846c9
languageName: node
linkType: hard

"@types/mapbox__point-geometry@npm:^0.1.4":
version: 0.1.4
resolution: "@types/mapbox__point-geometry@npm:0.1.4"
Expand Down Expand Up @@ -4857,6 +4868,44 @@ __metadata:
languageName: node
linkType: hard

"ical-generator@npm:^3.4.2":
version: 3.6.1
resolution: "ical-generator@npm:3.6.1"
dependencies:
uuid-random: "npm:^1.3.2"
peerDependencies:
"@touch4it/ical-timezones": ">=1.6.0"
"@types/luxon": ">= 1.26.0"
"@types/mocha": ">= 8.2.1"
"@types/node": ">= 15.0.0"
dayjs: ">= 1.10.0"
luxon: ">= 1.26.0"
moment: ">= 2.29.0"
moment-timezone: ">= 0.5.33"
rrule: ">= 2.6.8"
peerDependenciesMeta:
"@touch4it/ical-timezones":
optional: true
"@types/luxon":
optional: true
"@types/mocha":
optional: true
"@types/node":
optional: true
dayjs:
optional: true
luxon:
optional: true
moment:
optional: true
moment-timezone:
optional: true
rrule:
optional: true
checksum: 10c0/969e1af967e1e84ba0bd5a723b56399009b89fbf9ee0bb9a27fdc03ac346162a4a4cd2c748ac3b13f0d37a60d6ec350c473fc81e48b32ccc8f446ad1db85658d
languageName: node
linkType: hard

"iconv-lite@npm:0.4.24":
version: 0.4.24
resolution: "iconv-lite@npm:0.4.24"
Expand Down Expand Up @@ -5973,6 +6022,22 @@ __metadata:
languageName: node
linkType: hard

"moment-timezone@npm:^0.5.40":
version: 0.5.48
resolution: "moment-timezone@npm:0.5.48"
dependencies:
moment: "npm:^2.29.4"
checksum: 10c0/ab14ec9d94bc33f29ac18e5417b7f8aca0b17130b952c5cc9697b8fea839e5ece9313af5fd3c9703a05db472b1560ddbfc7ad2aa24aac9afd047d6da6c3c6033
languageName: node
linkType: hard

"moment@npm:^2.29.4":
version: 2.30.1
resolution: "moment@npm:2.30.1"
checksum: 10c0/865e4279418c6de666fca7786607705fd0189d8a7b7624e2e56be99290ac846f90878a6f602e34b4e0455c549b85385b1baf9966845962b313699e7cb847543a
languageName: node
linkType: hard

"ms@npm:2.0.0":
version: 2.0.0
resolution: "ms@npm:2.0.0"
Expand Down Expand Up @@ -8359,6 +8424,13 @@ __metadata:
languageName: node
linkType: hard

"uuid-random@npm:^1.3.2":
version: 1.3.2
resolution: "uuid-random@npm:1.3.2"
checksum: 10c0/14cb0fc8d52738b9f3b856df39e82f50650dad3dca254d3e62742148ba57292697c53d7bb171b84ad9d0f9d9231fd0f13d13eb65cb67b7d6c994f78d40e5e363
languageName: node
linkType: hard

"uuid@npm:^13.0.0":
version: 13.0.0
resolution: "uuid@npm:13.0.0"
Expand Down