File indexing completed on 2025-01-12 04:46:44

0001 /*
0002    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 function parseAirport(airport, name)
0008 {
0009     airport.name = name;
0010     if (name.startsWith("Brussels Airport"))
0011         airport.iataCode = "BRU"; // disambiguate Brussel airports
0012     return airport;
0013 }
0014 
0015 function main(html) {
0016     var reservations = new Array();
0017     var bookingRef = html.eval("//td[@class='confirmation']")[0].recursiveContent;
0018 
0019     var flightDetailsRoot = html.eval("//h2[text()='Flight details']")[0].nextSibling;
0020     if (!flightDetailsRoot)
0021         return null;
0022 
0023     while (!flightDetailsRoot.isNull && flightDetailsRoot.name != "h2") {
0024         if (flightDetailsRoot.name != "table") {
0025             flightDetailsRoot = flightDetailsRoot.nextSibling;
0026             continue;
0027         }
0028 
0029         var rows = flightDetailsRoot.eval("./tr");
0030         for (var i in rows) {
0031             var cell = rows[i].firstChild;
0032             if (cell.firstChild.isNull)
0033                 continue;
0034 
0035             var res = JsonLd.newFlightReservation();
0036             res.reservationNumber = bookingRef;
0037             parseAirport(res.reservationFor.departureAirport, cell.firstChild.content);
0038             var depTime = cell.recursiveContent.match(/([0-9]{2} [A-Za-z]{3} [0-9]{4}),\s*([0-9]{2}:[0-9]{2})/);
0039             if (!depTime)
0040                 continue;
0041             res.reservationFor.departureTime = JsonLd.toDateTime(depTime[1] + ' ' + depTime[2], "dd MMM yyyy hh:mm", "en");
0042 
0043             cell = cell.nextSibling;
0044             parseAirport(res.reservationFor.arrivalAirport, cell.firstChild.content);
0045             var arrTime = cell.recursiveContent.match(/([0-9]{2} [A-Za-z]{3} [0-9]{4}),\s*([0-9]{2}:[0-9]{2})/);
0046             if (!arrTime)
0047                 continue;
0048             res.reservationFor.arrivalTime = JsonLd.toDateTime(arrTime[1] + ' ' + arrTime[2], "dd MMM yyyy hh:mm", "en");
0049 
0050             cell = cell.nextSibling;
0051             var airline = cell.recursiveContent.match(/([A-Z0-9]{2}) ([0-9]{3,4})\s*([A-Z][A-Za-z0-9 ]*)/);
0052             if (!airline)
0053                 continue;
0054             res.reservationFor.airline.iataCode = airline[1];
0055             res.reservationFor.airline.name = airline[3];
0056             res.reservationFor.flightNumber = airline[2];
0057             reservations.push(res);
0058         }
0059 
0060         flightDetailsRoot = flightDetailsRoot.nextSibling;
0061     }
0062 
0063     return reservations;
0064 }