diff --git a/autotests/postprocessordata/lh-incomplete-departure-time.post.json b/autotests/postprocessordata/lh-incomplete-departure-time.post.json new file mode 100644 index 0000000..03b0ec9 --- /dev/null +++ b/autotests/postprocessordata/lh-incomplete-departure-time.post.json @@ -0,0 +1,43 @@ +[ + { + "@context": "http://schema.org", + "@type": "FlightReservation", + "airplaneSeat": "5A", + "reservationFor": { + "@type": "Flight", + "airline": { + "@type": "Airline", + "iataCode": "LH", + "name": "Lufthansa" + }, + "arrivalAirport": { + "@type": "Airport", + "geo": { + "@type": "GeoCoordinates", + "latitude": 52.55970001220703, + "longitude": 13.287799835205078 + }, + "iataCode": "TXL", + "name": "Berlin" + }, + "boardingTime": { + "@type": "QDateTime", + "@value": "2018-03-06T17:05:00+01:00", + "timezone": "Europe/Berlin" + }, + "departureAirport": { + "@type": "Airport", + "geo": { + "@type": "GeoCoordinates", + "latitude": 48.35390090942383, + "longitude": 11.786100387573242 + }, + "iataCode": "MUC", + "name": "Munich" + }, + "departureDay": "2018-03-06", + "flightNumber": "2724" + }, + "reservationNumber": "XXX007" + } +] diff --git a/autotests/postprocessordata/lh-incomplete-departure-time.pre.json b/autotests/postprocessordata/lh-incomplete-departure-time.pre.json new file mode 100644 index 0000000..a0d7093 --- /dev/null +++ b/autotests/postprocessordata/lh-incomplete-departure-time.pre.json @@ -0,0 +1,39 @@ +[ + { + "@context": "http://schema.org", + "@type": "FlightReservation", + "airplaneSeat": "5A", + "airplaneSeatClass": { + "@type": "AirplaneSeatClass", + "name": "ECO LIGHT" + }, + "passengerSequenceNumber": "42", + "programMembership": { + "@type": "ProgramMembership", + "program": "" + }, + "reservationFor": { + "@type": "Flight", + "airline": { + "@type": "Airline", + "iataCode": "LH", + "name": "Lufthansa" + }, + "arrivalAirport": { + "@type": "Airport", + "iataCode": "TXL", + "name": "Berlin" + }, + "boardingTime": "2018-03-06T17:05:00.000+01:00", + "departureAirport": { + "@type": "Airport", + "iataCode": "MUC", + "name": "Munich" + }, + "departureTime": "2018-03-06", + "flightNumber": "2724" + }, + "reservationNumber": "XXX007", + "showDepartureTimeOnBoardingPass": "false" + } +] diff --git a/src/jsonldimportfilter.cpp b/src/jsonldimportfilter.cpp index 2a71b50..1529b7f 100644 --- a/src/jsonldimportfilter.cpp +++ b/src/jsonldimportfilter.cpp @@ -1,86 +1,100 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "jsonldimportfilter.h" #include #include using namespace KItinerary; static void renameProperty(QJsonObject &obj, const char *oldName, const char *newName) { const auto value = obj.value(QLatin1String(oldName)); if (!value.isNull() && !obj.contains(QLatin1String(newName))) { obj.insert(QLatin1String(newName), value); obj.remove(QLatin1String(oldName)); } } static void filterTrainTrip(QJsonObject &trip) { if (trip.value(QLatin1String("@type")).toString() != QLatin1String("TrainTrip")) { return; } // move TrainTrip::trainCompany to TrainTrip::provider (as defined by schema.org) renameProperty(trip, "trainCompany", "provider"); } static void filterLodgingReservation(QJsonObject &res) { // check[in|out]Date -> check[in|out]Time (legacy Google format) renameProperty(res, "checkinDate", "checkinTime"); renameProperty(res, "checkoutDate", "checkoutTime"); } +static void filterFlight(QJsonObject &res) +{ + // move incomplete departureTime (ie. just ISO date, no time) to departureDay + if (res.value(QLatin1String("departureTime")).toString().size() == 10) { + renameProperty(res, "departureTime", "departureDay"); + } +} + static void filterReservation(QJsonObject &res) { // move ticketToken to Ticket (Google vs. schema.org difference) const auto token = res.value(QLatin1String("ticketToken")).toString(); if (!token.isEmpty()) { auto ticket = res.value(QLatin1String("reservedTicket")).toObject(); if (ticket.isEmpty()) { ticket.insert(QLatin1String("@type"), QLatin1String("Ticket")); } if (!ticket.contains(QLatin1String("ticketToken"))) { ticket.insert(QLatin1String("ticketToken"), token); res.insert(QLatin1String("reservedTicket"), ticket); } } } QJsonObject JsonLdImportFilter::filterObject(const QJsonObject& obj) { QJsonObject res(obj); const auto type = obj.value(QLatin1String("@type")).toString(); if (type.endsWith(QLatin1String("Reservation"))) { filterReservation(res); } if (type == QLatin1String("TrainReservation")) { auto train = obj.value(QLatin1String("reservationFor")).toObject(); filterTrainTrip(train); if (!train.isEmpty()) { res.insert(QLatin1String("reservationFor"), train); } } else if (type == QLatin1String("LodgingReservation")) { filterLodgingReservation(res); + } else if (type == QLatin1String("FlightReservation")) { + auto flight = obj.value(QLatin1String("reservationFor")).toObject(); + filterFlight(flight); + if (!flight.isEmpty()) { + res.insert(QLatin1String("reservationFor"), flight); + } } return res; }