Add GTFS enrichment to HAFAS response
Implements enrichment for HAFAS API responses by cross-referencing section data against an indexed ÖBB GTFS feed. This involves adding logic to fetch, parse, and index the GTFS data from the specified URL. The enrichment function now uses GTFS time and station data to populate `gtfsName` and `gtfsDirection` fields for missing journey data in HAFAS responses. Updates are also made to: - Update `apps/web/src/lib/constants.ts` with the GTFS URL. - Create `apps/web/src/lib/oebb-gtfs.ts` to handle GTFS fetching and indexing. - Enhance `apps/web/src/app/api/hafas/route.ts` to utilize the new enrichment function. -Package updates include `fflate` and minor fixes to other packages.
This commit is contained in:
@@ -21,7 +21,9 @@ type RawJson = any;
|
||||
* @param queryDate - The original JavaScript Date used for the query (fallback for missing times)
|
||||
*/
|
||||
export function parseHafasJourneys(json: RawJson, hafasDate: string, queryDate: Date): Journey[] {
|
||||
const outConL: RawJson[] = json?.svcResL?.[0]?.res?.outConL ?? [];
|
||||
const res = json?.svcResL?.[0]?.res;
|
||||
const outConL: RawJson[] = res?.outConL ?? [];
|
||||
const prodL: RawJson[] = res?.common?.prodL ?? [];
|
||||
|
||||
return outConL.map((con: RawJson, i: number): Journey => {
|
||||
const first = con.secL?.[0];
|
||||
@@ -38,16 +40,24 @@ export function parseHafasJourneys(json: RawJson, hafasDate: string, queryDate:
|
||||
const trains: string[] = (con.secL ?? [])
|
||||
.filter((s: RawJson) => s.jny)
|
||||
.map((s: RawJson) => {
|
||||
const name = s.jny?.stopL?.[0]?.name ?? "";
|
||||
const direction = s.jny?.dirTxt ?? s.jny?.dir ?? "";
|
||||
return name && direction ? `${name} -> ${direction}` : name;
|
||||
const product = prodL[s.jny?.prodX];
|
||||
const name =
|
||||
s.jny?.gtfsName ??
|
||||
s.jny?.name ??
|
||||
product?.nameS ??
|
||||
product?.prodCtx?.name?.trim() ??
|
||||
product?.name ??
|
||||
s.jny?.stopL?.[0]?.name ??
|
||||
"";
|
||||
const direction = s.jny?.gtfsDirection ?? s.jny?.dirTxt ?? s.jny?.dir ?? "";
|
||||
return name && direction ? `${name.trim()} -> ${direction}` : name.trim();
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
return {
|
||||
id: con.ctxRecon ?? `journey-${i}`,
|
||||
sD, rD, sA, rA, delay,
|
||||
platform: dep?.dPlatfS ?? "",
|
||||
platform: dep?.dPlatfS ?? dep?.dPltfS?.txt ?? "",
|
||||
changes: Math.max(0, (con.secL ?? []).filter((s: RawJson) => s.jny).length - 1),
|
||||
trains,
|
||||
cancelled: (con.secL ?? []).some((s: RawJson) => s.jny?.isCncl === true),
|
||||
|
||||
Reference in New Issue
Block a user