-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calendar #43
Open
c-harding
wants to merge
7
commits into
main
Choose a base branch
from
calendar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Calendar #43
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba119ce
Add calendar endpoint
c-harding 0bbce26
Fix seconds with decimal places in calendar
c-harding 4bc983c
Fix case of stat labels
c-harding 9b18b2d
Add redirect to show the calendar
c-harding df8cb45
Serve the response natively
c-harding 27b9414
Add elapsed time
c-harding 37ddb75
Fix timezones
c-harding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -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'); | ||
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' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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(', '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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; | ||
} |
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
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 |
---|---|---|
@@ -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 ''; | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -849,6 +849,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" | ||
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== | ||
|
||
"@types/luxon@^2.3.2": | ||
version "2.4.0" | ||
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-2.4.0.tgz#897d3abc23b68d78b69d76a12c21e01eb5adab95" | ||
integrity sha512-oCavjEjRXuR6URJEtQm0eBdfsBiEcGBZbq21of8iGkeKxU1+1xgKuFPClaBZl2KB8ZZBSWlgk61tH6Mf+nvZVw== | ||
|
||
"@types/mapbox__point-geometry@*", "@types/mapbox__point-geometry@^0.1.4": | ||
version "0.1.4" | ||
resolved "https://registry.yarnpkg.com/@types/mapbox__point-geometry/-/mapbox__point-geometry-0.1.4.tgz#0ef017b75eedce02ff6243b4189210e2e6d5e56d" | ||
|
@@ -2576,6 +2581,13 @@ [email protected]: | |
statuses "2.0.1" | ||
toidentifier "1.0.1" | ||
|
||
ical-generator@^3.4.2: | ||
version "3.6.1" | ||
resolved "https://registry.yarnpkg.com/ical-generator/-/ical-generator-3.6.1.tgz#097e9b4124de5554912ad30d341bdba644f6c312" | ||
integrity sha512-tEH0OTNn00Mp61DcTxIFR+5fhsAivKk1LWAJUAbkMCI+M4yu+cZzT5X8rZf3b5PzFkMogh0zj3PsFh9bHvGGIQ== | ||
dependencies: | ||
uuid-random "^1.3.2" | ||
|
||
[email protected]: | ||
version "0.4.24" | ||
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" | ||
|
@@ -3117,6 +3129,18 @@ minimist@^1.2.0, minimist@^1.2.6: | |
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c" | ||
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA== | ||
|
||
moment-timezone@^0.5.40: | ||
version "0.5.40" | ||
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.40.tgz#c148f5149fd91dd3e29bf481abc8830ecba16b89" | ||
integrity sha512-tWfmNkRYmBkPJz5mr9GVDn9vRlVZOTe6yqY92rFxiOdWXbjaR0+9LwQnZGGuNR63X456NqmEkbskte8tWL5ePg== | ||
dependencies: | ||
moment ">= 2.9.0" | ||
|
||
"moment@>= 2.9.0", moment@^2.29.4: | ||
version "2.29.4" | ||
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108" | ||
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w== | ||
|
||
[email protected]: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" | ||
|
@@ -4264,6 +4288,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" | ||
integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== | ||
|
||
uuid-random@^1.3.2: | ||
version "1.3.2" | ||
resolved "https://registry.yarnpkg.com/uuid-random/-/uuid-random-1.3.2.tgz#96715edbaef4e84b1dcf5024b00d16f30220e2d0" | ||
integrity sha512-UOzej0Le/UgkbWEO8flm+0y+G+ljUon1QWTEZOq1rnMAsxo2+SckbiZdKzAHHlVh6gJqI1TjC/xwgR50MuCrBQ== | ||
|
||
uuid@^9.0.1: | ||
version "9.0.1" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.