db-vendo-client/parse/journey.js

87 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2024-12-07 16:16:31 +00:00
import {parseRemarks} from './remarks.js';
const createFakeWalkingLeg = (prevLeg, leg) => {
const fakeWalkingLeg = {
origin: prevLeg.destination,
destination: leg.origin,
};
fakeWalkingLeg.departure = prevLeg.arrival;
fakeWalkingLeg.plannedDeparture = prevLeg.plannedArrival;
fakeWalkingLeg.departureDelay = prevLeg.delay;
fakeWalkingLeg.arrival = fakeWalkingLeg.departure;
fakeWalkingLeg.plannedArrival = fakeWalkingLeg.plannedDeparture;
fakeWalkingLeg.arrivalDelay = fakeWalkingLeg.departureDelay;
fakeWalkingLeg.public = true;
fakeWalkingLeg.walking = true;
fakeWalkingLeg.distance = null;
return fakeWalkingLeg;
2025-01-11 18:41:16 +00:00
};
2024-12-17 19:41:00 +00:00
const parseLocationsFromCtxRecon = (ctx, j) => {
2025-01-03 10:57:24 +00:00
return (j.ctxRecon || j.kontext)
2024-12-17 19:41:00 +00:00
.split('$')
.map(e => ctx.profile.parseLocation(ctx, {id: e}))
.filter(e => e.latitude || e.location?.latitude)
.reduce((map, e) => {
map[e.id] = e;
map[e.name] = e;
return map;
}, {});
};
const trimJourneyId = (journeyId) => {
2025-01-20 20:32:38 +00:00
if (!journeyId) {
return null;
}
const endOfHafasId = journeyId.lastIndexOf('$');
if (endOfHafasId != -1) {
2025-01-20 20:32:38 +00:00
return journeyId.substring(0, endOfHafasId + 1);
}
return journeyId;
2025-01-20 20:32:38 +00:00
};
2025-01-03 10:57:24 +00:00
const parseJourney = (ctx, jj) => { // j = raw journey
const {profile, opt} = ctx;
2025-01-03 10:57:24 +00:00
const j = jj.verbindung || jj;
2024-12-17 19:41:00 +00:00
const fallbackLocations = parseLocationsFromCtxRecon(ctx, j);
const legs = [];
2024-12-07 16:16:31 +00:00
for (const l of j.verbindungsAbschnitte) {
2024-12-17 19:41:00 +00:00
const leg = profile.parseJourneyLeg(ctx, l, null, fallbackLocations);
2025-01-11 18:41:16 +00:00
if (legs.length > 0 && !legs[legs.length - 1].walking && !leg.walking) {
const fakeWalkingLeg = createFakeWalkingLeg(legs[legs.length - 1], leg);
legs.push(fakeWalkingLeg);
}
legs.push(leg);
}
const res = {
type: 'journey',
legs,
refreshToken: trimJourneyId(j.ctxRecon || j.kontext),
};
2018-12-02 01:05:19 +01:00
2024-12-07 16:16:31 +00:00
// TODO freq
2024-12-07 16:16:31 +00:00
if (opt.remarks) {
res.remarks = parseRemarks(ctx, j);
}
2018-07-23 20:42:22 +02:00
2024-12-07 16:16:31 +00:00
// TODO
if (opt.scheduledDays && j.serviceDays) {
2022-11-16 15:18:12 +01:00
// todo [breaking]: rename to scheduledDates
2024-12-08 21:42:57 +00:00
// res.scheduledDays = profile.parseScheduledDays(ctx, j.serviceDays);
2017-11-11 22:35:41 +01:00
}
2025-01-03 10:57:24 +00:00
res.price = profile.parsePrice(ctx, jj);
const tickets = profile.parseTickets(ctx, jj);
2024-12-21 15:26:49 +00:00
if (tickets) {
res.tickets = tickets;
}
return res;
};
2017-11-11 22:35:41 +01:00
2022-05-07 16:17:37 +02:00
export {
parseJourney,
};