From a723e56b4ce392d2b11d28f2745279aa825a2ee1 Mon Sep 17 00:00:00 2001 From: memdmp Date: Mon, 21 Jul 2025 22:53:11 +0200 Subject: feat: initial commit --- src/lib/Page.svelte | 32 +++ src/lib/Timetable.svelte | 424 ++++++++++++++++++++++++++++++++++ src/lib/Titlebar.svelte | 55 +++++ src/lib/aliases.ts | 36 +++ src/lib/assets/LineGlyph.svelte | 193 ++++++++++++++++ src/lib/assets/LineGlyphSrc.svg | 233 +++++++++++++++++++ src/lib/assets/Pictogram.svelte | 11 + src/lib/index.ts | 51 +++++ src/lib/motis-api.ts | 57 +++++ src/lib/motis-types.ts | 496 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 1588 insertions(+) create mode 100644 src/lib/Page.svelte create mode 100644 src/lib/Timetable.svelte create mode 100644 src/lib/Titlebar.svelte create mode 100644 src/lib/aliases.ts create mode 100644 src/lib/assets/LineGlyph.svelte create mode 100644 src/lib/assets/LineGlyphSrc.svg create mode 100644 src/lib/assets/Pictogram.svelte create mode 100644 src/lib/index.ts create mode 100644 src/lib/motis-api.ts create mode 100644 src/lib/motis-types.ts (limited to 'src/lib') diff --git a/src/lib/Page.svelte b/src/lib/Page.svelte new file mode 100644 index 0000000..92cd92a --- /dev/null +++ b/src/lib/Page.svelte @@ -0,0 +1,32 @@ + + + + {title} + + + +
+ {#if sidepanel} +
+ {@render sidepanel()} +
+ {/if} + {@render children()} +
diff --git a/src/lib/Timetable.svelte b/src/lib/Timetable.svelte new file mode 100644 index 0000000..4a17ca7 --- /dev/null +++ b/src/lib/Timetable.svelte @@ -0,0 +1,424 @@ + + +{#snippet renderLocalTime( + time: Date, + forceAbsolute = false +)}{#await localTime(time, forceAbsolute, now)}{isRelativeTime && !forceAbsolute + ? timeRelative(time.getTime(), now) + : time.getUTCHours() + + ':' + + time.getUTCMinutes() + + (time.getUTCSeconds() !== 0 ? `:${time.getUTCSeconds()}` : '') + + ' UTC'}{:then t}{t}{/await}{/snippet} + +{#if stopTimes} + {#each stopTimes.stopTimes + // garbage data + .filter((v) => v.agencyUrl !== 'http://www.rta.ae') as departure} + {@const expectedTime = + new Date( + (isArrivals + ? departure.place.scheduledArrival + : departure.place.scheduledDeparture) ?? '0' + ).getTime() / + 1000 / + 60} + {@const receivedTime = + new Date( + (isArrivals ? departure.place.arrival : departure.place.departure) ?? + '0' + ).getTime() / + 1000 / + 60} + {@const delayMinutes = receivedTime - expectedTime} + {@const avoidGlyph = departure.routeShortName.startsWith('FlixTrain')} + {@const routeShortName = (() => { + let n = departure.routeShortName; + if (n.startsWith('EC ')) n = n.replace('EC ', 'EC'); + if (departure.mode === 'TRAM' && !isNaN(parseInt(n))) n = 'T ' + n; + if (departure.mode === 'BUS' && !isNaN(parseInt(n))) n = 'B ' + n; + if ( + departure.routeShortName === 'European Sleeper' && + departure.agencyName === 'Eu Sleeper' + ) + n = 'EN'; // TODO: validate these are real euronights + if (n === '?') n = ''; + if (n.startsWith('FlixTrain ')) n = n.substring(10); + // Note: may also catch ECs/ICs/EXTs operated by DB + if ( + departure.agencyId === '12681' && + departure.agencyName === 'DB Fernverkehr AG' && + departure.mode === 'HIGHSPEED_RAIL' && + departure.source.startsWith('de_DELFI.gtfs.zip/') && + !isNaN(parseInt(n)) + ) + n = `ICE ${n}`; + return n; + })()} + {@const pictogram = (() => { + switch (true) { + case departure.mode === Mode.Bike: + return 'Velo_l'; + case departure.mode === Mode.ODM: + case departure.mode === Mode.Rental: + return 'Taxi_l'; + case departure.mode === Mode.Car: + case departure.mode === Mode.CarDropoff: + case departure.mode === Mode.CarParking: + return 'Auto_l'; + + // Transit // + case departure.mode === Mode.Airplane: + return 'Abflug_l'; + case departure.mode === Mode.LongDistanceRail: + case departure.mode === Mode.RegionalFastRail: + case departure.mode === Mode.RegionalRail: + case departure.mode === Mode.Metro: + case departure.mode === Mode.HighspeedRail: + return 'Zug_l'; + case departure.mode === Mode.NightRail: + return 'Schlafwagen'; + case departure.mode === Mode.Subway: + return 'Metro_l_' + (m.lang_short() === 'en' ? 'de' : m.lang_short()); + case departure.mode === Mode.Bus: + return 'Bus_l'; + case departure.mode === Mode.Coach: + return 'Fernbus_l'; + case departure.mode === Mode.Tram: + case departure.mode === Mode.CableTram: + return 'Tram_l'; + case departure.mode === Mode.Funicular: + return 'Zahnradbahn_l'; + case departure.mode === Mode.AerialLift: + // return 'Gondelbahn_l'; + return 'Luftseilbahn_l'; + case departure.mode === Mode.Ferry: + return 'Schiff_l'; + case departure.mode === Mode.Other: + default: + return 'Licht'; + } + })()} + {@const notices = (() => { + let notices = [] as [pictogram: string[], content: string][]; + if (departure.cancelled) + notices.push([ + ['Cancellation', 'Attention'], + m.brief_north_otter_cherish(), + ]); + if (delayMinutes < -0.5) { + notices.push([ + ['Hint'], + m.vehicle_is_early({ + minutes: -delayMinutes, + arrival: isArrivals.toString(), + }), + ]); + } else if (delayMinutes >= 1) { + notices.push([ + delayMinutes >= 5 + ? ['Delay', 'Attention'] + : delayMinutes >= 3 + ? ['Delay', 'Hint'] + : ['Hint'], + m.next_long_lark_bask({ + minutes: delayMinutes.toFixed(0), + }), + ]); + } + return notices; + })()} + {@const situationIsBad = notices.find((v) => v[0].includes('Attention'))} +
+
+
+ {#if pictogram} + + {/if} + {#if ([Mode.NightRail || Mode.HighspeedRail || Mode.LongDistanceRail || Mode.RegionalFastRail || Mode.RegionalRail].includes(departure.mode) || (departure.mode === 'BUS' && routeShortName.startsWith('EV')) || (departure.mode === 'METRO' && departure.routeShortName.startsWith('S'))) && !avoidGlyph} + + {:else} + + {routeShortName} + + {/if} + + + {m.to()} + {departure.headsign} + +
+
+ {#if departure.place.scheduledTrack && departure.place.track} + + {#if departure.place.name !== placeName}{`${ + departure.place.name === placeName + ', Bahnhof' + ? placeName + ', Busbahnhof' + : departure.place.name + }, `} + {/if}{m.station_location({ + track: departure.place.track, + mode: departure.mode, + })} + + {:else if departure.place.name !== placeName}{departure.place.name === + placeName + ', Bahnhof' + ? placeName + ', Busbahnhof' + : departure.place.name} + {/if} +
+
+ {departure.cancelled + ? m.antsy_weird_cowfish_wish() + ' ' + : ''}{isRelativeTime && Math.abs(expectedTime - receivedTime) < 1 + ? isArrivals + ? m.hour_tidy_hawk_explore() + : m.free_knotty_ray_soar() + : isArrivals + ? m.polite_lucky_angelfish_amaze() + : m.home_flaky_jurgen_ascend()} + {#if Math.abs(expectedTime - receivedTime) < 1} + + {@render renderLocalTime(new Date(receivedTime * 60 * 1000))} + + {:else} + + {@render renderLocalTime(new Date(expectedTime * 60 * 1000), true)} + + + {@render renderLocalTime(new Date(receivedTime * 60 * 1000), true)} + + {#if isRelativeTime} + + ({@render renderLocalTime(new Date(receivedTime * 60 * 1000))}) + + {/if} + {/if} +
+ {#if notices.length !== 0} +
+ {#each notices as notice} +
+ {#each notice[0] as pictogram}{/each} + + {notice[1]} + +
+ {/each} +
+ {/if} + + {#if departure.agencyName} + {m.operated_by({ + operator: operators.has(departure.agencyName) + ? operators.get(departure.agencyName)! + : departure.agencyName, + })}{#if departure.agencyName === 'DB Fernverkehr AG'} + {' '} + · + {m.line_number_accuracy()}{/if} + {/if} +
+ {/each} +{:else} +
+
+ {#if (placeName || placeId) && isResultsPage} +

No results

+

+ No results have been found for the station {placeName ?? placeId}.
+ Please try again. +

+ {:else if placeId} +

No results

+

+ No results have been found for the station {placeName ?? placeId}.
+ Please try again. +

+ {:else} +

No Station

+

+ Please input a station in the search field above and select a search + result. +

+

+ Examples: + + + + + + +

+ {/if} +
+
+{/if} diff --git a/src/lib/Titlebar.svelte b/src/lib/Titlebar.svelte new file mode 100644 index 0000000..35cfd0d --- /dev/null +++ b/src/lib/Titlebar.svelte @@ -0,0 +1,55 @@ + + +
+ +
+ {@render children?.()} +
+
diff --git a/src/lib/aliases.ts b/src/lib/aliases.ts new file mode 100644 index 0000000..4fc08ec --- /dev/null +++ b/src/lib/aliases.ts @@ -0,0 +1,36 @@ +import { m } from './paraglide/messages'; + +export const placeNameMap = new Map(); +for (const [v1, v2] of [ + ['Freiburg(Brsg)', 'Freiburg(Breisgau) Hbf'], + ['Freiburg(Breisgau)', 'Freiburg(Breisgau) Hbf'], + ['Freiburg(Breisgau) Hbf', 'Freiburg(Breisgau) Hbf'], + ['Freiburg (Breisgau)', 'Freiburg(Breisgau) Hbf'], + ['Freiburg (Breisgau) Hbf', 'Freiburg(Breisgau) Hbf'], + ['Freiburg im Breisgau', 'Freiburg(Breisgau) Hbf'], + ['Freiburg im Breisgau Hbf', 'Freiburg(Breisgau) Hbf'], + ['Freiburg im Breisgau Hauptbahnhof', 'Freiburg(Breisgau) Hbf'], + ['Freiburg (D)', 'Freiburg(Breisgau) Hbf'], + ['Freiburg (D), Busbahnhof', 'Freiburg(Breisgau) Hbf'], + ['Freiburg Hauptbahnhof', 'Freiburg(Breisgau) Hbf'], + ['S+U Berlin Hauptbahnhof', 'Berlin Hbf'], + ['Berlin Hauptbahnhof', 'Berlin Hbf'], +]) + placeNameMap.set(v1.toLowerCase(), v2); +export const operators = new Map(); +operators.set('Schweizerische Bundesbahnen SBB', m.operator_sbb()); +operators.set('SBB', m.operator_sbb()); + +operators.set('SZU', m.operator_szu()); +operators.set('Sihltal-Zürich-Uetliberg-Bahn', m.operator_szu()); + +operators.set('Verkehrsbetriebe Zürich', m.operator_vbz()); // buses +operators.set('Verkehrsbetriebe Zürich INFO+', m.operator_vbz()); // trams + +operators.set('BLS AG (bls)', m.operator_bls()); + +operators.set('Städtische Verkehrsbetriebe Bern', m.operator_bernmobil()); + +operators.set('Regionalverkehr Bern-Solothurn', m.operator_rbs()); + +operators.set('Verkehrsbetriebe Glattal', m.operator_vbg()); diff --git a/src/lib/assets/LineGlyph.svelte b/src/lib/assets/LineGlyph.svelte new file mode 100644 index 0000000..f5df523 --- /dev/null +++ b/src/lib/assets/LineGlyph.svelte @@ -0,0 +1,193 @@ + + + + + + + + + + {#if isFernverkehr} + + + + {#if lineType && lineType.toUpperCase() in fernverkehrIconMap} + + {/if} + + {#if line !== undefined && line !== ''} + {line} + {/if} + + {:else} + + A + + {routeShortName} + + {/if} + diff --git a/src/lib/assets/LineGlyphSrc.svg b/src/lib/assets/LineGlyphSrc.svg new file mode 100644 index 0000000..8d7bc59 --- /dev/null +++ b/src/lib/assets/LineGlyphSrc.svg @@ -0,0 +1,233 @@ + + + + diff --git a/src/lib/assets/Pictogram.svelte b/src/lib/assets/Pictogram.svelte new file mode 100644 index 0000000..fe97de6 --- /dev/null +++ b/src/lib/assets/Pictogram.svelte @@ -0,0 +1,11 @@ + + +{which} diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..a961354 --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1,51 @@ +// place files you want to import through the `$lib` alias in this folder. +export type ButtonColours = { + fg: string; + fgDisabled: string; + bg: string; + bgActive: string; + bgHover: string; +}; +export const S = { + card_bg: 'bg-[#ffffff]/20' as const, + shadow_large: 'shadow-[0_5px_25px_-3px_#00000077]' as const, + window: + 'outline-[1.5px] outline-[#313135] rounded-xl flex flex-row items-stretch justify-stretch max-h-full' as const, + window_sidepanel: 'bg-[#28282C] rounded-l-xl p-2 pt-0 min-w-48' as const, + window_content: 'bg-[#1D1D20] rounded-r-xl p-2 pt-0 flex-1' as const, + window_content_fullscreen: + 'bg-[#1D1D20] rounded-xl p-2 pt-0 flex-1 max-w-[100%]' as const, + window_topbar: + 'box-border min-h-[3.5rem] px-1 py-2 font-bold flex items-center justify-center gap-2 sticky top-0 bg-inherit z-50' as const, + button(colours: 'primary' | 'secondary' | ButtonColours) { + switch (colours) { + case 'primary': + colours = { + fg: '#dedede', + fgDisabled: '#2e3436', + bg: '#3584E4', + bgActive: '#2A6AB8', + bgHover: '#4990E7', + }; + break; + case 'secondary': + colours = { + fg: '#dedede', + fgDisabled: '#2e3436', + bg: '#0000', + bgActive: '#dedede22', + bgHover: '#dedede11', + }; + break; + } + colours = colours as ButtonColours; + return `transition-all disabled:grayscale disabled:text-[${colours.fgDisabled}] not-disabled:text-[${colours.fg}] not-disabled:bg-[${colours.bg}] not-disabled:hover:bg-[${colours.bgHover}] not-disabled:active:bg-[${colours.bgActive}] flex items-center justify-center p-2 rounded-xl outline-2 outline-[#0000] not-disabled:focus:outline-[#4C82C999]`; + }, + input: + 'transition-all bg-[#2E2E32] not-disabled:not-focus:not-active:hover:bg-[#dedede11] p-2 outline-2 outline-[#0000] not-disabled:focus:outline-[#4C82C999]' as const, + heading: [ + // + 'text-2xl font-bold', + 'text-lg', + ] as const, +}; diff --git a/src/lib/motis-api.ts b/src/lib/motis-api.ts new file mode 100644 index 0000000..1d43eea --- /dev/null +++ b/src/lib/motis-api.ts @@ -0,0 +1,57 @@ +import type { StoptimesResponse } from './motis-types'; +export class MotisAPI { + backend = 'https://api.transitous.org'; + fetch: typeof fetch = (url, init) => + fetch( + `${this.backend}${this.backend.endsWith('/') ? '' : '/'}${ + typeof url === 'string' + ? url.startsWith('/') + ? url.substring(1) + : url + : url instanceof URL + ? url.pathname + url.search + : url + }`, + init + ); + async getStopTimes( + id: string, + abortSignal?: AbortSignal, + arrivals = false, + limit = 128, + time: Date | undefined = arrivals + ? new Date( + // '2025-07-19T02:03:11Z' + Date.now() - 1000 * 60 + ) + : undefined + ) { + const res = await this.fetch( + `/api/v1/stoptimes?stopId=${encodeURIComponent( + id + )}&n=${encodeURIComponent(limit.toString())}&arriveBy=${ + arrivals ? 'true' : 'false' + }&withScheduledSkippedStops=${ + localStorage.getItem('with-scheduled-skipped-stops') ?? true + }&radius=${localStorage.getItem('radius') ?? 350}${ + time ? `&time=${time.toISOString()}` : '' + }`, + { + signal: abortSignal, + } + ); + if (res.status !== 200) + throw new Error( + `Stop Times: Expected 200 OK, got ${res.status} ${ + res.statusText + } (${await res + .text() + .catch((e) => `Could not get response body: ${e}`)})` + ); + const json = (await res.json()) as StoptimesResponse; + // TODO: validate + return json; + } +} +export const motis = new MotisAPI(); +export default motis; diff --git a/src/lib/motis-types.ts b/src/lib/motis-types.ts new file mode 100644 index 0000000..2f05229 --- /dev/null +++ b/src/lib/motis-types.ts @@ -0,0 +1,496 @@ +export type Area = { + /** Name of the area */ + name: string; + /** {@link https://wiki.openstreetmap.org/wiki/Key:admin_level OpenStreetMap `admin_level`} of the area */ + adminLevel: number; + /** Whether this area was matched by the input text */ + matched: boolean; + /** Set for the first area after the `default` area that distinguishes areas if the match is ambiguous regarding (`default` area + place name / street [+ house number]). */ + unique: boolean; + /** Whether this area should be displayed as default area (area with admin level closest 7) */ + default: boolean; +}; +export type Match = { + /** location type */ + type: 'ADDRESS' | 'PLACE' | 'STOP'; + /** list of non-overlapping tokens that were matched */ + tokens: number[][]; + /** name of the location (transit stop / PoI / address) */ + name: string; + /** unique id of the location */ + id: string; + /** latitude */ + lat: number; + /** longitude */ + lon: number; + /** level according to OpenStreetMap (at the moment only for public transport) */ + level?: number; + /** street name */ + street?: strin