File indexing completed on 2024-12-29 04:51:05
0001 /* 0002 SPDX-FileCopyrightText: 2018 Sune Vuorela <sune@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 function parseDate(day,time, issueDateString) { 0008 var issueDate = JsonLd.toDateTime(issueDateString, "ddMMMyy","en"); 0009 var ticketDate = JsonLd.toDateTime(day +"" + issueDate.getFullYear() + " " + time, "ddMMMyyyy hh:mm", "en" ); 0010 if (ticketDate < issueDate) { 0011 ticketDate.setFullYear(issueDate.getFullYear() +1); 0012 } 0013 return ticketDate; 0014 } 0015 0016 function extractInformation(page) { 0017 var text = page.textInRect(0, 0, 1, 1); 0018 if (text.match(/\s+Electronic Ticket Itinerary and Receipt/)) { 0019 var res = new Array(); 0020 var lines = text.split("\n"); 0021 var lastFlight = null; 0022 var bookingRef = text.match(/Booking Reference:\s* ([A-Z0-9]+)\s/); 0023 var issueDate = text.match(/.*Date of Issue:\s([0-9A-Z]+)/); 0024 var state = 0; 0025 var operatedBy = ""; 0026 for(var i = 0 ; i < lines.length; i++ ) { 0027 if (lines[i].match(/\s+Flight\/Date\s+Route\s+Departure\s+Arrival\s+Latest\s+Flight\s+Baggage/)) { 0028 state = 1; // headers found 0029 continue; 0030 } 0031 if(lines[i].match(/Ticket Number:.*/)) { 0032 break; 0033 } 0034 if (state === 1) { 0035 state = 2; 0036 continue; 0037 } 0038 if (state === 2) { 0039 operatedBy = lines[i].trim(); 0040 state = 3; 0041 continue; 0042 } 0043 if (state ===3) { 0044 // Parsing lines like 0045 // LH 160 / 27DEC Frankfurt FRA - Leipzig/Halle 17:00 17:55 Terminal 1 1PC 0046 // SK 1674 / 03JAN Berlin Tegel - Copenhagen Kastrup 13:30 14:30 12:30 1PC 0047 // SK 675 / 27DEC Copenhagen Kastrup - Frankfurt FRA 13:40 15:15 12:40 Terminal 3 1PC 0048 var flightLine = lines[i].match(/([A-Z0-9]{2}) ([0-9]{1,4}) \/ ([0-9]{2}[A-Z]{3})\s+([^0-9]+)([0-9]{2}:[0-9]{2})\s+([0-9]{2}:[0-9]{2})\s+([0-9]{2}:[0-9]{2})?.*/) 0049 if (flightLine) { 0050 var flight = JsonLd.newFlightReservation(); 0051 flight.reservationNumber = bookingRef[1]; 0052 flight.reservationFor.flightNumber = flightLine[2]; 0053 flight.reservationFor.airline.iataCode = flightLine[1]; 0054 flight.reservationFor.airline.name = operatedBy; 0055 flight.reservationFor.departureTime = parseDate(flightLine[3], flightLine[5], issueDate[1] ); 0056 var airports = flightLine[4].split(" - "); 0057 flight.reservationFor.departureAirport.name = airports[0].trim();; 0058 flight.reservationFor.arrivalTime = parseDate(flightLine[3], flightLine[6], issueDate[1] ); 0059 flight.reservationFor.arrivalAirport.name = airports[1].trim(); 0060 res.push(flight); 0061 } 0062 state = 4; 0063 continue; 0064 } 0065 if(state === 4) { 0066 state = 2; 0067 } 0068 0069 } 0070 return res; 0071 0072 } 0073 return null; 0074 } 0075 0076 0077 function main(pdf) { 0078 var result = new Array(); 0079 0080 for (var i = 0; i < pdf.pageCount; ++i) { 0081 var page = pdf.pages[i]; 0082 var parseResult = extractInformation(page); 0083 if (Array.isArray(parseResult)) { 0084 result = result.concat(parseResult); 0085 } 0086 } 0087 0088 return result; 0089 }