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

0001 /*
0002    SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 function lastChild(elem) {
0008     var child = elem.firstChild;
0009     while (!child.nextSibling.isNull) {
0010         child = child.nextSibling;
0011     }
0012     return child;
0013 }
0014 
0015 function parseDateTime(text) {
0016     var dt = text.match(/([\d/]+).*?([\d:]+)/);
0017     return JsonLd.toDateTime(dt[1] + dt[2], "dd/MM/yyyyhh:mm", "en");
0018 }
0019 
0020 function parseHtml(doc) {
0021 
0022     var res = JsonLd.newLodgingReservation();
0023 
0024     var nameElem = doc.eval("//td[@class=\"title-hotel\" or @class=\"hotel-title\"]")[0];
0025     res.reservationFor.name = nameElem.content;
0026 
0027     let addrElem = doc.eval("//td[@class=\"hotel-address\"]");
0028     if (addrElem.length > 0) {
0029         addrElem = addrElem[0];
0030     } else {
0031         addrElem = nameElem.parent.nextSibling.nextSibling.nextSibling.firstChild;
0032     }
0033     var addr = addrElem.content.match(/(.*), ?([^,]*)/);
0034     res.reservationFor.address.streetAddress = addr[1];
0035     res.reservationFor.address.addressLocality = addr[2];
0036     res.reservationFor.url = lastChild(addrElem).attribute("href");
0037 
0038     var linkRoot = addrElem.parent.parent;
0039     var links = linkRoot.eval(".//a");
0040     for (var i = 0; i < links.length; ++i) {
0041         var url = links[i].attribute("href");
0042         if (url.startsWith("tel:"))
0043             res.reservationFor.telephone = url.substr(4);
0044         else if (url.startsWith("mailto:"))
0045             res.reservationFor.email = url.substr(7);
0046     }
0047 
0048     var bookingRef = doc.eval("//table//tr/td/table//tr/td[2]")[0];
0049     if (bookingRef.content) {
0050         res.reservationNumber = bookingRef.content;
0051         var bookingRow = bookingRef.parent.nextSibling.nextSibling;
0052 
0053         res.underName.name = bookingRow.firstChild.nextSibling.content;
0054         bookingRow = bookingRow.nextSibling.nextSibling;
0055 
0056         res.underName.email = bookingRow.firstChild.nextSibling.recursiveContent;
0057         bookingRow = bookingRow.nextSibling.nextSibling.nextSibling;
0058 
0059         res.checkinTime = parseDateTime(bookingRow.firstChild.nextSibling.content);
0060         bookingRow = bookingRow.nextSibling.nextSibling;
0061         res.checkoutTime = parseDateTime(bookingRow.firstChild.nextSibling.content);
0062     } else {
0063         const resNumElem = doc.eval("//td[@class='table-confirmation text-bold']/a");
0064         if (resNumElem.length > 0) {
0065             res.reservationNumber = resNumElem[0].content;
0066 
0067             dts = doc.eval("//td[2]/strong");
0068             res.checkinTime = parseDateTime(dts[0].content);
0069             res.checkoutTime = parseDateTime(dts[1].content);
0070         } else {
0071             const text = doc.root.recursiveContent;
0072             res.reservationNumber = text.match(/Reservation number:[\n ](\d+)/)[1];
0073             res.checkinTime = parseDateTime(text.match(/Check-in:[\n ](.*)/i)[1]);
0074             res.checkoutTime = parseDateTime(text.match(/Check-out:[\n ](.*)/i)[1]);
0075         }
0076     }
0077     return res;
0078 }