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

0001 /*
0002    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003    SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 var regExMap = new Array();
0009 regExMap["cs_CZ"] = new Array();
0010 regExMap["cs_CZ"]["ticketId"] = /Elektronická jízdenka č\.\s+([0-9]+)/;
0011 regExMap["cs_CZ"]["singleTripHeader"] = /Cesta/;
0012 regExMap["cs_CZ"]["thereTripHeader"] = /Cesta tam/;
0013 regExMap["cs_CZ"]["returnTripHeader"] = /Cesta zpět/;
0014 regExMap["cs_CZ"]["columns"] = [ /Datum/, /Zastávka\/Přestup/, /Příjezd/, /Odjezd/, /Nást\./, /Spoj/, /Vůz\/sedadla/ ];
0015 regExMap["cs_CZ"]["date"] = /([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{2,4}$)/;
0016 regExMap["en_US"] = new Array();
0017 regExMap["en_US"]["ticketId"] = /Electronic ticket\s+([0-9]+)/;
0018 regExMap["en_US"]["singleTripHeader"] = /Route/;
0019 regExMap["en_US"]["thereTripHeader"] = /Route there/;
0020 regExMap["en_US"]["returnTripHeader"] = /Route back/;
0021 regExMap["en_US"]["columns"] = [ /Date/, /Station\/Transfer/, /Arrival/, /Departure/, /Platf\./, /Connection/, /Coach\/Seats/ ];
0022 regExMap["en_US"]["date"] = /([0-9]{2})\/([0-9]{2})\/([0-9]{2,4}$)/;
0023 
0024 function padDigit(s) {
0025     while (s.length < 2) {
0026         s = '0' + s;
0027     }
0028     return s;
0029 }
0030 
0031 function parseDate(date, time, locale) {
0032     var d = date.match(regExMap[locale]["date"]);
0033     var t = time.match(/([0-9]{1,2}):([0-9]{1,2})/);
0034     if (!d || !t) {
0035         return null;
0036     }
0037     return JsonLd.toDateTime(padDigit(d[1]) + "." + padDigit(d[2]) + ".20" + d[3] + " "
0038                                 + padDigit(t[1]) + ":" + padDigit(t[2]),
0039                              "dd.MM.yyyy HH:mm", locale);
0040 }
0041 
0042 var Columns = Object.freeze({
0043     Date: 0,
0044     Station: 1,
0045     ArrivalTime: 2,
0046     DepartureTime: 3,
0047     Platform: 4,
0048     Connection: 5,
0049     Seat: 6,
0050 
0051     ColumnCount: 7
0052 });
0053 
0054 function columnValue(line, columns, column, locale) {
0055     var start = columns.match(regExMap[locale]["columns"][column]);
0056     if (!start) {
0057         return "";
0058     }
0059     if (column < Columns.ColumnCount - 1) {
0060         var end = columns.match(regExMap[locale]["columns"][column + 1]);
0061         if (!end) {
0062             return "";
0063         }
0064         return line.substr(start.index, end.index - start.index).trim();
0065     } else {
0066         return line.substr(start.index).trim();
0067     }
0068 }
0069 
0070 function parseTrip(trip, locale) {
0071     var text = trip.split("\n")
0072     var columns = text[0];
0073     var reservations = new Array();
0074     var transportType = "Bus";
0075     for (var i = 1; i < text.length; i++) {
0076         // Skip the destination arrival part, we already populated it as part
0077         // of completing the previous departure line
0078         if (i < text.length - 1 && !text[i + 1]) {
0079             break;
0080         }
0081 
0082         var connection = columnValue(text[i], columns, Columns.Connection, locale);
0083         var number = null;
0084         var name = null;
0085         if (connection) {
0086             var split = connection.lastIndexOf("(")
0087             name = connection.substr(0, split - 1);
0088             number = connection.substr(split + 1, connection.length - split - 2);
0089             transportType = number.match(/RJ [0-9]+/) ? "Train" : "Bus";
0090         }
0091 
0092         var res = JsonLd.newObject(transportType + "Reservation");
0093         res.reservationFor = JsonLd.newObject(transportType + "Trip");
0094         if (transportType == "Bus") {
0095             if (number) {
0096                 res.reservationFor.busNumber = number;
0097             }
0098             if (name) {
0099                 res.reservationFor.busName = name;
0100             }
0101         } else if (transportType == "Train") {
0102             if (number) {
0103                 res.reservationFor.trainNumber = number;
0104             }
0105             if (name) {
0106                 res.reservationFor.trainName = name;
0107             }
0108         }
0109 
0110         var arrivalTime = columnValue(text[i + 1], columns, Columns.ArrivalTime, locale);
0111         var arrivalDate = columnValue(text[i + 1], columns, Columns.Date, locale);
0112         if (!arrivalTime) {
0113             arrivalTime = columnValue(text[i], columns, Columns.ArrivalTime, locale);
0114         }
0115         if (!arrivalDate) {
0116             arrivalDate = columnValue(text[i], columns, Columns.Date, locale);
0117         }
0118         if (arrivalDate && arrivalTime) {
0119             res.reservationFor.arrivalStation = JsonLd.newObject(transportType + "Station");
0120             res.reservationFor.arrivalStation.name = columnValue(text[i+1], columns, Columns.Station, locale);
0121             res.reservationFor.arrivalTime = parseDate(arrivalDate, arrivalTime, locale);
0122         }
0123 
0124         var departureTime = columnValue(text[i], columns, Columns.DepartureTime, locale);
0125         var departure = "";
0126         if (departureTime !== "") {
0127             departure = text[i];
0128         } else if (i > 0) {
0129             departure = text[i - 1];
0130             departureTime = columnValue(departure, columns, Columns.DepartureTime, locale);
0131         }
0132         if (departure) {
0133             res.reservationFor.departureStation = JsonLd.newObject(transportType + "Station");
0134             res.reservationFor.departureStation.name = columnValue(departure, columns, Columns.Station, locale);
0135             res.reservationFor.departureTime = parseDate(columnValue(departure, columns, Columns.Date, locale),
0136                                                          departureTime, locale);
0137 
0138             var platform = columnValue(departure, columns, Columns.Platform, locale);
0139             if (platform) {
0140                 res.reservationFor.departurePlatform = platform;
0141             }
0142 
0143             // seats are always bound to departur
0144             var seat = columnValue(departure, columns, Columns.Seat, locale);
0145             if (seat) {
0146                 var r = seat.match(/([0-9]+)\/([0-9]+)/);
0147                 res.reservedTicket = JsonLd.newObject("Ticket");
0148                 res.reservedTicket.ticketedSeat = JsonLd.newObject("Seat");
0149                 if (r) {
0150                     res.reservedTicket.ticketedSeat.seatSection = r[1];
0151                     res.reservedTicket.ticketedSeat.seatNumber = r[2];
0152                 } else {
0153                     res.reservedTicket.ticketedSeat.seatNumber = seat;
0154                 }
0155             }
0156         }
0157         reservations.push(res);
0158     }
0159     return reservations;
0160 }
0161 
0162 function main(text) {
0163     var reservations = new Array();
0164 
0165     for (var locale in regExMap) {
0166         var ticketId = text.match(regExMap[locale]["ticketId"]);
0167         if (!ticketId) {
0168             continue;
0169         }
0170 
0171         var resUrl = text.match(/http(s)?:\/\/jizdenky\.(regiojet|studentagency)\.cz\/OnlineTicket\?pam1=[0-9]+\&pam2=[0-9]+/)
0172 
0173         var returnHeader = text.match(regExMap[locale]["returnTripHeader"]);
0174         var isReturn = (returnHeader !== null);
0175         var routeHeader = text.match(regExMap[locale][isReturn ? "thereTripHeader" : "singleTripHeader"]);
0176         if (!routeHeader) {
0177             break;
0178         }
0179 
0180         var trip = text.substr(routeHeader.index + routeHeader[0].length + 1);
0181         var newRes = parseTrip(trip, locale);
0182         if (newRes.length === 0) {
0183             break;
0184         }
0185         reservations = reservations.concat(newRes);
0186 
0187         if (isReturn) {
0188             trip = text.substr(returnHeader.index + returnHeader[0].length + 1);
0189             reservations = reservations.concat(parseTrip(trip, locale));
0190         }
0191 
0192         for (var i = 0; i < reservations.length; ++i) {
0193             reservations[i].reservationNumber = ticketId[1];
0194             if (resUrl) {
0195                 reservations[i].modifyReservationUrl = resUrl[0];
0196             }
0197         }
0198 
0199         // No need to scan any further locales
0200         break;
0201     }
0202 
0203     return reservations;
0204 }
0205 
0206 function parseEvent(event)
0207 {
0208     var res = JsonLd.newTrainReservation();
0209     res.reservationFor.departureTime = JsonLd.readQDateTime(event, "dtStart");
0210     res.reservationFor.arrivalTime = JsonLd.readQDateTime(event, "dtEnd");
0211     res.underName.name = event.attendees[0].name;
0212 
0213     var summary = event.summary.match(/#(.+?): From (.+?), to (.+?), seat: (.+)/);
0214     res.reservationNumber = summary[1];
0215     res.reservationFor.departureStation.name = summary[2];
0216     res.reservationFor.arrivalStation.name = summary[3];
0217     res.reservedTicket.ticketedSeat.seatNumber =summary[4];
0218 
0219     var desc = event.description.match(/\((.+?)\)/);
0220     res.reservationFor.trainNumber = desc[1];
0221 
0222     var loc = event.location.match(/(.+?), (.+)/);
0223     res.reservationFor.departureStation.geo.latitude = 1.0 * loc[1];
0224     res.reservationFor.departureStation.geo.longitude = 1.0 * loc[2];
0225     return res;
0226 }