Commit db677859 authored by Volker Krause's avatar Volker Krause
Browse files

Add train <-> bus type transformation functions for JS

This replaces an ad hoc implementation of this in the DB extractor, and
fixes Flix[Bus|Train] using the wrong type for their train tickets.
parent c47f77ef
......@@ -6,6 +6,8 @@
#include "jsonld.h"
#include "json/jsonldfilterengine.h"
#include <KItinerary/JsonLdDocument>
#include <QDebug>
......@@ -171,6 +173,54 @@ QJSValue JsApi::JsonLd::newRentalCarReservation() const
return res;
}
static constexpr const JsonLdFilterEngine::TypeMapping train_to_bus_type_map[] = {
{ "TrainReservation", "BusReservation" },
{ "TrainStation", "BusStation" },
{ "TrainTrip", "BusTrip" },
};
static constexpr const JsonLdFilterEngine::PropertyMapping train_to_bus_property_map[] = {
{ "BusTrip", "arrivalStation", "arrivalBusStop" },
{ "BusTrip", "departureStation", "departureBusStop" },
{ "BusTrip", "trainName", "busName" },
{ "BusTrip", "trainNumber", "busNumber" },
};
QJSValue JsApi::JsonLd::trainToBusReservation(const QJSValue &trainRes) const
{
QJsonObject obj = QJsonValue::fromVariant(trainRes.toVariant()).toObject();
JsonLdFilterEngine filterEngine;
filterEngine.setTypeMappings(train_to_bus_type_map);
filterEngine.setPropertyMappings(train_to_bus_property_map);
filterEngine.filterRecursive(obj);
return m_engine->toScriptValue(obj);
}
static constexpr const JsonLdFilterEngine::TypeMapping bus_to_train_type_map[] = {
{ "BusReservation", "TrainReservation" },
{ "BusStation", "TrainStation" },
{ "BusStop", "TrainStation" },
{ "BusTrip", "TrainTrip" },
};
static constexpr const JsonLdFilterEngine::PropertyMapping bus_to_train_property_map[] = {
{ "TrainTrip", "arrivalBusStop", "arrivalStation" },
{ "TrainTrip", "departureBusStop", "departureStation" },
{ "TrainTrip", "busCompany", "provider" },
{ "TrainTrip", "busName", "trainName" },
{ "TrainTrip", "busNumber", "trainNumber" },
};
QJSValue JsApi::JsonLd::busToTrainReservation(const QJSValue &busRes) const
{
QJsonObject obj = QJsonValue::fromVariant(busRes.toVariant()).toObject();
JsonLdFilterEngine filterEngine;
filterEngine.setTypeMappings(bus_to_train_type_map);
filterEngine.setPropertyMappings(bus_to_train_property_map);
filterEngine.filterRecursive(obj);
return m_engine->toScriptValue(obj);
}
QDateTime JsApi::JsonLd::toDateTime(const QString &dtStr, const QString &format, const QString &localeName) const
{
QLocale locale(localeName);
......
......@@ -57,6 +57,11 @@ public:
*/
Q_INVOKABLE QJSValue newRentalCarReservation() const;
/** Convert a train reservation to a bus reservation. */
Q_INVOKABLE QJSValue trainToBusReservation(const QJSValue &trainRes) const;
/** Convert a bus reservation to a train reservation. */
Q_INVOKABLE QJSValue busToTrainReservation(const QJSValue &busRes) const;
/** Convert a date/time string to a date/time value.
* @param dtStr The input string containing a date/time value.
* @param format The format of the input string. Same format specification as
......
......@@ -7,6 +7,8 @@
#ifndef KITINERARY_JSONLDFILTERENGINE_H
#define KITINERARY_JSONLDFILTERENGINE_H
#include "kitinerary_export.h"
#include <cstddef>
class QJsonArray;
......@@ -19,8 +21,10 @@ namespace JsonLd {
void renameProperty(QJsonObject &obj, const char *oldName, const char *newName);
}
/** JSON-LD filtering for input normalization or type transforms. */
class JsonLdFilterEngine
/** JSON-LD filtering for input normalization or type transforms.
* @internal only exported for unit tests
*/
class KITINERARY_EXPORT JsonLdFilterEngine
{
public:
explicit JsonLdFilterEngine();
......
......@@ -55,18 +55,6 @@ function parseArrival(res, line, year) {
return true;
}
function convertToBus(res) {
res['@type'] = 'BusReservation';
res.reservationFor['@type'] = 'BusTrip';
res.reservationFor.departureBusStop = res.reservationFor.departureStation;
res.reservationFor.departureBusStop['@type'] = 'BusStop';
res.reservationFor.arrivalBusStop = res.reservationFor.arrivalStation;
res.reservationFor.arrivalBusStop['@type'] = 'BusStop';
res.reservationFor.busName = res.reservationFor.trainName;
res.reservationFor.busNumber = res.reservationFor.trainNumber;
res.reservedTicket.ticketedSeat = undefined;
}
function parseLegs(text, year, compact) {
var reservations = new Array();
var lines = text.split('\n');
......@@ -180,7 +168,8 @@ function parseTicket(text, uic918ticket) {
reservations[i].reservationNumber = bookingRef[1];
if (reservations[i].reservationFor.trainNumber && reservations[i].reservationFor.trainNumber.startsWith("Bus")) {
convertToBus(reservations[i]);
reservations[i] = JsonLd.trainToBusReservation(reservations[i]);
reservations[i].reservedTicket.ticketedSeat = undefined;
}
}
return reservations;
......
......@@ -10,6 +10,13 @@ function main(content, node) {
for (var i = 0; i < res.length; ++i) {
var ticketToken = res[i].reservedTicket.ticketToken;
res[i].reservedTicket.ticketToken = ticketToken.replace(/^https?:\/\/api\.meinfernbus\.(..)\/qrcode\/..\//, "qrCode:https://shop.flixbus.$1/pdfqr/");
// their schema.org annotations also claim train trips are bus trips, fix that
if (res[i].reservationFor.departureBusStop.name.endsWith(" (FlixTrain)") && res[i].reservationFor.arrivalBusStop.name.endsWith(" (FlixTrain)")) {
res[i] = JsonLd.busToTrainReservation(res[i]);
res[i].reservationFor.departureStation.name = res[i].reservationFor.departureStation.name.substr(0, res[i].reservationFor.departureStation.name.length - 11);
res[i].reservationFor.arrivalStation.name = res[i].reservationFor.arrivalStation.name.substr(0, res[i].reservationFor.arrivalStation.name.length - 11);
}
}
return res;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment