File indexing completed on 2024-12-29 04:51:05

0001 /*
0002    SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003    SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 function parseAirport(input) {
0007     let l = [];
0008     for (line of input.split('\n')) {
0009         l.push(line.replace(/^(?:.*?)?  +/, ''));
0010     }
0011     return l.join(' ').match(/(\S{2,}.*\S) \(([A-Z]{3})\), (.*)/);
0012 }
0013 
0014 function parseETicket(pdf) {
0015     const pnr = pdf.pages[0].text.match(/Booking Reference +([A-Z0-9]{6})\n/)[1];
0016     const leftCol = pdf.pages[0].textInRect(0.0, 0.0, 0.44, 1.0);
0017     let reservations = [];
0018     for(let idx = 0;;) {
0019         // select flight number until second date/time
0020         const row = leftCol.substr(idx).match(/[A-Z0-9]{2} *\d{1,4}  [\s\S]*?\S+, \d+ \S+ \d{4} \d\d:\d\d\n[\s\S]*?\S+, \d+ \S+ \d{4} \d\d:\d\d(?: \+.*)?\n/);
0021         if (!row)
0022             break;
0023         const flight = row[0].match(/([A-Z0-9]{2}) *(\d{1,4}).*\n(.*\S)  +/);
0024         const depTime = row[0].match(/\S+, (\d+ \S+ \d{4} \d\d:\d\d)\n/);
0025         const arrTime = row[0].substr(depTime.index + depTime[0].length).match(/\S+, (\d+ \S+ \d{4} \d\d:\d\d)(?: \+.*)?\n/);
0026         const departure = parseAirport(row[0].substr(0, depTime.index));
0027         const arrival = parseAirport(row[0].substr(depTime.index + depTime[0].length, arrTime.index));
0028         idx += row.index + row[0].length;
0029         let res = JsonLd.newFlightReservation();
0030         res.reservationFor.airline.iataCode = flight[1];
0031         res.reservationFor.flightNumber = flight[2];
0032         res.reservationFor.departureAirport.iataCode = departure[2];
0033         res.reservationFor.departureAirport.name = departure[3];
0034         res.reservationFor.airline.name = flight[3];
0035         res.reservationFor.departureTime = JsonLd.toDateTime(depTime[1], 'd MMM yyyy hh:mm', 'en');
0036         res.reservationFor.arrivalAirport.iataCode = arrival[2];
0037         res.reservationFor.arrivalAirport.name = arrival[3];
0038         res.reservationFor.arrivalTime = JsonLd.toDateTime(arrTime[1], 'd MMM yyyy hh:mm', 'en');
0039         res.reservationNumber = pnr;
0040         reservations.push(res);
0041     }
0042 
0043     return reservations;
0044 }