diff --git a/p/db/index.js b/p/db/index.js index 5110d08c..c40e8094 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -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', @@ -357,6 +406,10 @@ const mutateToAddTickets = (parsed, opt, j) => { currency: 'EUR', }; } + if (opt.generateUnreliableTicketUrls) { + addDbOfferSelectionUrl(parsed, opt); + } + } }; @@ -635,6 +688,7 @@ const profile = { formatStation, + generateUnreliableTicketUrls: false, refreshJourneyUseOutReconL: true, trip: true, journeysFromTrip: true,