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

0001 /*
0002    SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 function main(doc) {
0008 
0009     var reservations = new Array()
0010 
0011     var itineraryTable = doc.eval("//table/tr/td/div/div/div/table")[0]
0012 
0013     // First child is the header
0014     var item = itineraryTable.firstChild.nextSibling
0015 
0016     while (!item.isNull) {
0017 
0018         var res = JsonLd.newTrainReservation()
0019 
0020         res.reservationNumber = doc.eval("//table/tr/td/table[1]/tr[1]/td[2]")[0].content
0021         res.underName.name = doc.eval("//table/tr/td/table[1]/tr[2]/td[2]")[0].content
0022 
0023         res.reservationFor.departureStation.name = item.eval("td[1]")[0].content
0024         res.reservationFor.arrivalStation.name = item.eval("td[2]")[0].content
0025 
0026         var depDate = item.eval("td[3]")[0].content
0027         var arrDate = item.eval("td[4]")[0].content
0028 
0029         res.reservationFor.departureTime = JsonLd.toDateTime(depDate, "yyyy-MM-dd hh:mm", "en")
0030         res.reservationFor.arrivalTime = JsonLd.toDateTime(arrDate, "yyyy-MM-dd hh:mm", "en")
0031 
0032         res.reservationFor.trainNumber = item.eval("td[5]")[0].content
0033 
0034         reservations.push(res)
0035 
0036         item = item.nextSibling
0037     }
0038 
0039     var cont = true
0040     var idx = 2
0041     // Reserved seats come after the itinerary block and there might not be a reserved seat for all legs, so we need to match the reservations to the legs
0042     while (cont) {
0043         var reservationBlock = doc.eval("//table/tr/td/div/div/div")[idx]
0044 
0045         var title = reservationBlock.eval("span/strong")[0].content
0046 
0047         // Unfortunately we cannot know when to stop from the DOM structure
0048         if (!title.includes("Seating")) {
0049             cont = false
0050         }
0051 
0052         var departureStation = title.split("\n")[1]
0053 
0054         // Find from itinerary based on station names
0055         for (res of reservations) {
0056             if (departureStation == res.reservationFor.departureStation.name + " - " + res.reservationFor.arrivalStation.name) {
0057                 var seat = reservationBlock.eval("table/tr[2]/td[2]")[0].content
0058                 var coach = reservationBlock.eval("table/tr[2]/td[3]")[0].content
0059 
0060                 res.reservedTicket.ticketedSeat.seatNumber = reservationBlock.eval("table/tr[2]/td[2]")[0].content
0061                 res.reservedTicket.ticketedSeat.seatSection = reservationBlock.eval("table/tr[2]/td[3]")[0].content
0062 
0063                 console.log(seat, coach)
0064             }
0065         }
0066         idx++
0067     }
0068 
0069     return reservations
0070 }