Commit 8bbf97b7 authored by Volker Krause's avatar Volker Krause
Browse files

Add convenience API to create the entire reservation object skeleton

So far only for hotel reservations, still needs to be done for other types
too. Reduces error-prone boilerplate code in extractor scripts.
parent 6f8f218c
......@@ -18,7 +18,7 @@
*/
function main(text) {
var res = JsonLd.newObject("LodgingReservation");
var res = JsonLd.newLodgingReservation();
var bookingRef = text.match(/(?:Booking no\.|Buchungs-Nr\.)\s+([A-Z0-9-]+)\s+/);
if (!bookingRef)
......@@ -38,11 +38,9 @@ function main(text) {
res.checkoutTime = JsonLd.toDateTime(departureDate[1] + " 10:00", "dd.MM.yyyy hh:mm", "en");
idx += departureDate.index + departureDate[0].length;
res.reservationFor = JsonLd.newObject("LodgingBusiness");
var geo = text.substr(idx).match(/<(http[^>]*google.com\/maps[^>]*)>/);
res.reservationFor.geo = JsonLd.toGeoCoordinates(geo[1]);
res.underName = JsonLd.newObject("Person");
var name = text.substr(idx).match(/\*(?:First name|Vorname)\*\s+([^\s{2}]+)\s{2,}/);
if (!name)
return null;
......@@ -60,7 +58,6 @@ function main(text) {
idx += hotel.index + hotel[0].length;
hotel = text.substr(idx).split(/\s{2,}/);
res.reservationFor.name = hotel[0];
res.reservationFor.address = JsonLd.newObject("PostalAddress");
res.reservationFor.address.streetAddress = hotel[1];
var city = hotel[2].match(/(\d+)\s(.*)/);
if (city) {
......
......@@ -24,15 +24,13 @@ function parseHtml(doc) {
if (!bookingRef)
return null;
var res = JsonLd.newObject("LodgingReservation");
var res = JsonLd.newLodgingReservation();
res.reservationNumber = bookingRef[1];
var hotelInfo = elems[3].eval(".//table//table");
var row = hotelInfo[0].firstChild;
var addr = row.recursiveContent.match(/([^\n]+)\n\s+([^\n]+)\n\s+([^\n]+)/);
res.reservationFor = JsonLd.newObject("LodgingBusiness");
res.reservationFor.name = addr[1];
res.reservationFor.address = JsonLd.newObject("PostalAddress");
res.reservationFor.address.streetAddress = addr[2];
res.reservationFor.address.addressLocality = addr[3];
row = row.nextSibling;
......@@ -51,7 +49,6 @@ function parseHtml(doc) {
var booking = elems[5].firstChild.firstChild.firstChild;
row = booking.firstChild;
res.underName = JsonLd.newObject("Person");
res.underName.name = row.firstChild.nextSibling.content;
row = row.nextSibling.nextSibling.nextSibling;
......
......@@ -49,28 +49,24 @@ regExMap['de_DE']['person'] = /Name des Gastes +(.*) Name des Gastes bearbeiten/
regExMap['de_DE']['dateFormat'] = "dddd, d. MMMM yyyy hh:mm";
function main(text) {
var res = JsonLd.newObject("LodgingReservation");
var res = JsonLd.newLodgingReservation();
for (var locale in regExMap) {
var bookingRef = text.match(regExMap[locale]['bookingRef']);
// If no booking reference go to the next locale
if (!bookingRef)
continue;
res.reservationNumber = bookingRef[1];
var hotelName = text.match(regExMap[locale]['hotelName']);
if (!hotelName)
return null;
res.reservationFor = JsonLd.newObject("LodgingBusiness");
res.reservationFor.name = hotelName[1];
var hotel = text.match(regExMap[locale]['hotelInformation']);
if (!hotel)
return null;
res.reservationFor.address = JsonLd.newObject("PostalAddress");
res.reservationFor.address.streetAddress = hotel[1];
res.reservationFor.address.postalCode = hotel[3];
res.reservationFor.address.addressLocality = hotel[2];
......@@ -93,14 +89,11 @@ function main(text) {
res.checkoutTime = JsonLd.toDateTime(departureDate[1] + " " + departureDate[2], regExMap[locale]['dateFormat'], locale);
idx += departureDate.index + departureDate[0].length;
res.underName = JsonLd.newObject("Person");
var name = text.substr(idx).match(regExMap[locale]['person']);
if (!name)
return null;
res.underName.name = name[1];
return res;
}
}
......@@ -45,6 +45,23 @@ QJSValue JsApi::JsonLd::newObject(const QString &typeName) const
return v;
}
QJSValue JsApi::JsonLd::newLodgingReservation() const
{
const auto addr = newObject(QStringLiteral("PostalAddress"));
const auto geo = newObject(QStringLiteral("GeoCoordinates"));
const auto person = newObject(QStringLiteral("Person"));
auto resFor = newObject(QStringLiteral("LodgingBusiness"));
resFor.setProperty(QStringLiteral("address"), addr);
resFor.setProperty(QStringLiteral("geo"), geo);
auto res = newObject(QStringLiteral("LodgingReservation"));
res.setProperty(QStringLiteral("reservationFor"), resFor);
res.setProperty(QStringLiteral("underName"), person);
return res;
}
QDateTime JsApi::JsonLd::toDateTime(const QString &dtStr, const QString &format, const QString &localeName) const
{
QLocale locale(localeName);
......
......@@ -39,6 +39,13 @@ public:
/** Create a new JSON-LD object of type @p typeName. */
Q_INVOKABLE QJSValue newObject(const QString &typeName) const;
/** Convenience method that generates a full LodgingReservation JS object.
* This can be used by extractor scripts to fill in the extracted information.
*/
Q_INVOKABLE QJSValue newLodgingReservation() const;
// TODO add the same for other reservation types
/** 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
......
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