Skip to content

Commit

Permalink
DB: optionally parse ticket URLs
Browse files Browse the repository at this point in the history
Co-Authored-By: Jannis R <[email protected]>
  • Loading branch information
PaulSut and derhuerst committed Mar 12, 2024
1 parent 784d273 commit c4966ae
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions p/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,55 @@ const formatRefreshJourneyReq = (ctx, refreshToken) => {
};
};

const parseShpCtx = (addDataTicketInfo) => {
try {
return JSON.parse(atob(addDataTicketInfo)).shpCtx;
} catch (e) {
// in case addDataTicketInfo is not a valid base64 string
return null;
}
};


const addDbOfferSelectionUrl = (journey, opt) => {

// if no ticket contains addData, we can't get the offer selection URL
if (journey.tickets.some((t) => t.addDataTicketInfo)) {

const queryParams = new URLSearchParams();

// Add individual parameters
queryParams.append('A.1', opt.age);
queryParams.append('E', 'F');
queryParams.append('E.1', opt.loyaltyCard ? formatLoyaltyCard(opt.loyaltyCard) : '0');
queryParams.append('K', opt.firstClass ? '1' : '2');
queryParams.append('M', 'D');
queryParams.append('RT.1', 'E');
queryParams.append('SS', journey.legs[0].origin.id);
queryParams.append('T', journey.legs[0].departure);
queryParams.append('VH', journey.refreshToken);
queryParams.append('ZS', journey.legs[journey.legs.length - 1].destination.id);
queryParams.append('journeyOptions', '0');
queryParams.append('journeyProducts', '1023');
queryParams.append('optimize', '1');
queryParams.append('returnurl', 'dbnavigator://');
const endpoint = opt.language === 'de' ? 'dox' : 'eox';

journey.tickets.forEach((t) => {
const shpCtx = parseShpCtx(t.addDataTicketInfo);
if (shpCtx) {
const url = new URL(`https://mobile.bahn.de/bin/mobil/query.exe/${endpoint}`);
url.searchParams = new URLSearchParams(queryParams);
url.searchParams.append('shpCtx', shpCtx);
t.url = url.href;
} else {
t.url = null;
}
});
}
};


// todo: fix this
// line: {
// type: 'line',
Expand Down Expand Up @@ -357,6 +406,10 @@ const mutateToAddTickets = (parsed, opt, j) => {
currency: 'EUR',
};
}
if (opt.generateUnreliableTicketUrls) {
addDbOfferSelectionUrl(parsed, opt);
}

}
};

Expand Down Expand Up @@ -635,6 +688,7 @@ const profile = {

formatStation,

generateUnreliableTicketUrls: false,
refreshJourneyUseOutReconL: true,
trip: true,
journeysFromTrip: true,
Expand Down

0 comments on commit c4966ae

Please sign in to comment.