2016-12-29 18:20:08 +01:00
|
|
|
module fahrplanparser;
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
import std.algorithm : map;
|
2017-07-12 15:42:23 +02:00
|
|
|
import std.array : empty, front;
|
2016-12-29 18:20:08 +01:00
|
|
|
import std.conv : to;
|
2017-04-11 18:57:10 +02:00
|
|
|
import std.datetime : dur, TimeOfDay, DateTimeException;
|
|
|
|
import std.string : format;
|
2016-12-29 18:20:08 +01:00
|
|
|
|
|
|
|
import kxml.xml : readDocument, XmlNode;
|
|
|
|
|
|
|
|
import substitution;
|
|
|
|
|
2017-01-11 14:42:04 +01:00
|
|
|
private:
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
enum departureNodeName = "dp";
|
|
|
|
enum timeNodeName = "t";
|
|
|
|
enum realTimeNodeName = "rt";
|
|
|
|
|
|
|
|
enum departuresXPath = "/efa/dps/" ~ departureNodeName;
|
|
|
|
template timeXPath(string _timeNodeName = timeNodeName)
|
2017-01-11 14:42:04 +01:00
|
|
|
{
|
2017-04-11 18:57:10 +02:00
|
|
|
enum timeXPath = "/st/" ~ _timeNodeName;
|
2017-01-11 14:42:04 +01:00
|
|
|
}
|
2017-05-14 11:29:19 +02:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
enum useRealTimeXPath = "/realtime";
|
|
|
|
enum lineXPath = "/m/nu";
|
|
|
|
enum directionXPath = "/m/des";
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2016-12-29 18:20:08 +01:00
|
|
|
public:
|
|
|
|
|
2017-05-16 14:34:42 +02:00
|
|
|
/***********************************
|
|
|
|
* Parses the departure monitor data and returns it as an associative array.
|
|
|
|
* data is expected to contain valid XML as returned by queries sent to http://mobile.defas-fgi.de/beg/.
|
|
|
|
*/
|
|
|
|
|
2016-12-29 18:39:10 +01:00
|
|
|
auto parsedFahrplan(in string data)
|
2016-12-29 18:20:08 +01:00
|
|
|
{
|
2017-01-11 14:42:04 +01:00
|
|
|
// dfmt off
|
2016-12-29 18:39:10 +01:00
|
|
|
return data.readDocument
|
2017-04-11 18:57:10 +02:00
|
|
|
.parseXPath(departuresXPath)
|
|
|
|
.map!(dp => ["departure" : "%02s:%02s".format(dp.departureTime.hour, dp.departureTime.minute),
|
|
|
|
"delay" : dp.delay.total!"minutes".to!string,
|
|
|
|
"line": dp.parseXPath(lineXPath).front.getCData,
|
|
|
|
"direction": dp.parseXPath(directionXPath).front.getCData.substitute]);
|
2017-01-11 14:42:04 +01:00
|
|
|
// dfmt on
|
2016-12-29 18:20:08 +01:00
|
|
|
}
|
|
|
|
|
2017-05-16 14:34:42 +02:00
|
|
|
///
|
2017-04-11 18:57:10 +02:00
|
|
|
@system unittest
|
|
|
|
{
|
2017-05-14 11:29:19 +02:00
|
|
|
import std.array : array;
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
auto xml = "";
|
|
|
|
assert(xml.parsedFahrplan.array == []);
|
|
|
|
|
|
|
|
xml = "<efa><dps></dps></efa>";
|
|
|
|
assert(xml.parsedFahrplan.array == []);
|
|
|
|
|
|
|
|
xml = "<efa><dps><dp><realtime>1</realtime><st><t>1224</t><rt>1242</rt></st><m><nu>6</nu><des>Wernerwerkstraße</des></m></dp></dps></efa>";
|
2017-05-14 11:29:19 +02:00
|
|
|
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße",
|
|
|
|
"line" : "6", "departure" : "12:24", "delay" : "18"]]);
|
2017-04-11 18:57:10 +02:00
|
|
|
|
|
|
|
xml = "<efa><dps><dp><realtime>0</realtime><st><t>1224</t></st><m><nu>6</nu><des>Wernerwerkstraße</des></m></dp></dps></efa>";
|
2017-05-14 11:29:19 +02:00
|
|
|
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße",
|
|
|
|
"line" : "6", "departure" : "12:24", "delay" : "0"]]);
|
2017-04-11 18:57:10 +02:00
|
|
|
|
|
|
|
xml = "<efa><dps><dp><realtime>0</realtime><st><t>1224</t></st><m><nu>6</nu><des>Wernerwerkstraße</des></m></dp><dp><realtime>1</realtime><st><t>1353</t><rt>1356</rt></st><m><nu>11</nu><des>Burgweinting</des></m></dp></dps></efa>";
|
2017-05-14 11:29:19 +02:00
|
|
|
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", "line" : "6",
|
|
|
|
"departure" : "12:24", "delay" : "0"], ["direction" : "Burgweinting",
|
|
|
|
"line" : "11", "departure" : "13:53", "delay" : "3"]]);
|
2017-04-11 18:57:10 +02:00
|
|
|
}
|
|
|
|
|
2016-12-29 18:20:08 +01:00
|
|
|
private:
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
class UnexpectedValueException(T) : Exception
|
2016-12-29 18:20:08 +01:00
|
|
|
{
|
2017-04-11 18:57:10 +02:00
|
|
|
this(T t, string node) @safe pure
|
2016-12-29 18:20:08 +01:00
|
|
|
{
|
2017-04-11 18:57:10 +02:00
|
|
|
super(`Unexpected value "%s" for node "%s"`.format(t, node));
|
2016-12-29 18:20:08 +01:00
|
|
|
}
|
2017-04-11 18:57:10 +02:00
|
|
|
}
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-07-12 15:42:23 +02:00
|
|
|
class CouldNotFindeNodeException : Exception
|
|
|
|
{
|
|
|
|
this(string node) @safe pure
|
|
|
|
{
|
|
|
|
super(`Could not find node "%s"`.format(node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
auto departureTime(string _timeNodeName = timeNodeName)(XmlNode dp)
|
|
|
|
in
|
|
|
|
{
|
|
|
|
assert(dp.getName == departureNodeName);
|
|
|
|
}
|
|
|
|
body
|
|
|
|
{
|
2017-07-12 15:42:23 +02:00
|
|
|
auto timeNodes = dp.parseXPath(timeXPath!_timeNodeName);
|
|
|
|
if(timeNodes.empty)
|
|
|
|
throw new CouldNotFindeNodeException(_timeNodeName);
|
|
|
|
|
|
|
|
return TimeOfDay.fromISOString(timeNodes.front.getCData ~ "00");
|
2016-12-29 18:20:08 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
@system unittest
|
2016-12-29 18:20:08 +01:00
|
|
|
{
|
2017-04-11 18:57:10 +02:00
|
|
|
import std.exception : assertThrown;
|
|
|
|
|
|
|
|
auto xml = "<dp><st><t>0000</t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.departureTime == TimeOfDay(0, 0));
|
|
|
|
|
|
|
|
xml = "<dp><st><t>0013</t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.departureTime == TimeOfDay(0, 13));
|
|
|
|
|
|
|
|
xml = "<dp><st><t>1100</t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.departureTime == TimeOfDay(11, 00));
|
|
|
|
|
|
|
|
xml = "<dp><st><t>1242</t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.departureTime == TimeOfDay(12, 42));
|
|
|
|
|
|
|
|
xml = "<dp><st><t>2359</t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.departureTime == TimeOfDay(23, 59));
|
|
|
|
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>2400</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>0061</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>2567</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t></t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>0</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>00</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>000000</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>00:00</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
assertThrown!DateTimeException("<dp><st><t>abcd</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front.departureTime);
|
|
|
|
}
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
auto delay(XmlNode dp)
|
|
|
|
in
|
|
|
|
{
|
|
|
|
assert(dp.getName == departureNodeName);
|
|
|
|
}
|
|
|
|
body
|
|
|
|
{
|
2017-05-14 11:29:19 +02:00
|
|
|
immutable useRealtimeString = dp.parseXPath(useRealTimeXPath).front.getCData;
|
2017-04-11 18:57:10 +02:00
|
|
|
if (useRealtimeString == "0")
|
|
|
|
return dur!"minutes"(0);
|
|
|
|
else if (useRealtimeString == "1")
|
|
|
|
{
|
2017-07-12 15:42:23 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
immutable expectedTime = dp.departureTime;
|
|
|
|
immutable realTime = dp.departureTime!realTimeNodeName;
|
|
|
|
auto timeDiff = realTime - expectedTime;
|
|
|
|
if (timeDiff < dur!"minutes"(0))
|
|
|
|
timeDiff = dur!"hours"(24) + timeDiff;
|
|
|
|
return timeDiff;
|
|
|
|
}
|
|
|
|
catch (CouldNotFindeNodeException e)
|
|
|
|
{
|
|
|
|
return dur!"minutes"(0);
|
|
|
|
}
|
2016-12-29 18:20:08 +01:00
|
|
|
}
|
2017-04-11 18:57:10 +02:00
|
|
|
else
|
|
|
|
throw new UnexpectedValueException!string(useRealtimeString, "realtime");
|
2016-12-29 18:20:08 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
@system unittest
|
2016-12-29 18:20:08 +01:00
|
|
|
{
|
|
|
|
import std.exception : assertThrown;
|
2017-04-11 18:57:10 +02:00
|
|
|
import core.exception : AssertError;
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
auto xml = "<dp><realtime>0</realtime></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(0));
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime></realtime></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!(UnexpectedValueException!string)(xml.delay);
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>2</realtime></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!(UnexpectedValueException!string)(xml.delay);
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>a</realtime></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!(UnexpectedValueException!string)(xml.delay);
|
2016-12-29 18:27:30 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!AssertError(xml.delay);
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><t></t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!DateTimeException(xml.delay);
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt></rt></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!AssertError(xml.delay);
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><st><rt></rt><t></t></st></dp>".readDocument.parseXPath("/dp").front;
|
|
|
|
assertThrown!AssertError(xml.delay);
|
2016-12-29 18:20:08 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt></rt><t></t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front;
|
|
|
|
assertThrown!DateTimeException(xml.delay);
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t></t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front;
|
|
|
|
assertThrown!DateTimeException(xml.delay);
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt></rt><t>0000</t></st></dp>".readDocument.parseXPath("/dp")
|
|
|
|
.front;
|
|
|
|
assertThrown!DateTimeException(xml.delay);
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t>0000</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(0));
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>0001</rt><t>0000</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(1));
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>1753</rt><t>1751</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(2));
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>1010</rt><t>1000</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(10));
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>1301</rt><t>1242</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(19));
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t>1242</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(678));
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t>2359</t></st></dp>"
|
|
|
|
.readDocument.parseXPath("/dp").front;
|
|
|
|
assert(xml.delay == dur!"minutes"(1));
|
2016-12-29 18:20:08 +01:00
|
|
|
}
|