diff --git a/src/backends/deutschebahnbackend.cpp b/src/backends/deutschebahnbackend.cpp index 851f6f1..5e06a9d 100644 --- a/src/backends/deutschebahnbackend.cpp +++ b/src/backends/deutschebahnbackend.cpp @@ -1,89 +1,95 @@ /* Copyright (C) 2019 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 "deutschebahnbackend.h" #include "deutschebahnvehiclelayoutparser.h" #include #include #include #include #include #include #include #include #include using namespace KPublicTransport; AbstractBackend::Capabilities DeutscheBahnBackend::capabilities() const { return Secure; // hardcoded API endpoint below } static QString extractTrainNumber(const Line &line) { qDebug() << line.modeString() << line.name(); QRegularExpression regex(QStringLiteral("(?:ICE|IC|EC)\\s*(\\d+)")); const auto match = regex.match(line.modeString() + line.name()); if (match.hasMatch()) { return match.captured(1); } return {}; } bool DeutscheBahnBackend::queryVehicleLayout(const VehicleLayoutRequest &request, VehicleLayoutReply *reply, QNetworkAccessManager *nam) const { // we need two parameters for the online API: the train number (numeric only), and the departure time // note: data is only available withing the upcoming 24h // checking this early is useful as the error response from the online service is extremely verbose... - const auto dt = request.departure().scheduledDepartureTime(); + auto dt = request.departure().scheduledDepartureTime(); const auto trainNum = extractTrainNumber(request.departure().route().line()); - qDebug() << dt << trainNum; - if (!dt.isValid() || trainNum.isEmpty()) { // TODO check dt in valid range + if (!dt.isValid() || trainNum.isEmpty()) { return false; } + // there are only valid results for a 24h time window, so try to adjust the date accordingly + const auto now = QDateTime::currentDateTime(); + if (dt.daysTo(now) > 1 || dt.daysTo(now) < -1) { + qDebug() << "adjusting departure time to today:" << dt; + dt.setDate(QDate::currentDate()); + } + QUrl url; url.setScheme(QStringLiteral("https")); url.setHost(QStringLiteral("www.apps-bahn.de")); url.setPath(QLatin1String("/wr/wagenreihung/1.0/") + trainNum + QLatin1Char('/') + dt.toString(QStringLiteral("yyyyMMddhhmm"))); QNetworkRequest netReq(url); logRequest(request, netReq); auto netReply = nam->get(netReq); QObject::connect(netReply, &QNetworkReply::finished, reply, [this, reply, netReply] { const auto data = netReply->readAll(); logReply(reply, netReply, data); if (netReply->error() == QNetworkReply::NoError) { DeutscheBahnVehicleLayoutParser p; if (p.parse(data)) { addResult(reply, p.vehicle, p.platform, p.departure); } else { addError(reply, this, p.error, p.errorMessage); } } else { addError(reply, this, Reply::NetworkError, reply->errorString()); } netReply->deleteLater(); }); return true; }