2017-01-11 14:42:04 +01:00
|
|
|
import std.array : array, replace;
|
2016-12-29 18:39:10 +01:00
|
|
|
import std.datetime : Clock;
|
2017-04-10 21:07:53 +02:00
|
|
|
import std.file : exists, isFile;
|
2016-12-29 18:39:10 +01:00
|
|
|
import std.format : format;
|
2016-12-29 18:43:54 +01:00
|
|
|
import std.getopt : defaultGetoptPrinter, getopt;
|
|
|
|
import std.json : JSONValue;
|
|
|
|
import std.stdio : File, writeln;
|
2016-12-02 12:46:39 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
import requests : getContent;
|
2016-12-02 12:46:39 +01:00
|
|
|
|
2016-12-29 18:20:08 +01:00
|
|
|
import fahrplanparser;
|
2016-12-02 12:46:39 +01:00
|
|
|
|
2016-12-29 18:20:08 +01:00
|
|
|
import substitution;
|
2016-12-02 12:46:39 +01:00
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
// find stops: http://mobile.defas-fgi.de/beg/XML_STOPFINDER_REQUEST?outputFormat=XML&stateless=1&locationServerActive=1®ionID_sf=1&type_sf=stop&name_sf=Regensburg%20Universit%C3%A4t
|
|
|
|
|
|
|
|
enum baseURL = "http://mobile.defas-fgi.de/beg/";
|
|
|
|
enum departureMonitorRequest = "XML_DM_REQUEST";
|
|
|
|
enum stopFinderRequest = "XML_STOPFINDER_REQUEST";
|
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
void main(string[] args)
|
|
|
|
{
|
|
|
|
string fileName;
|
2017-05-08 22:38:31 +02:00
|
|
|
string busStop = "Regensburg Universität";
|
2016-12-02 12:46:39 +01:00
|
|
|
string substitutionFileName = "replacement.txt";
|
2017-04-11 18:57:10 +02:00
|
|
|
// dfmt off
|
2016-12-02 12:46:39 +01:00
|
|
|
auto helpInformation = getopt(args,
|
2017-04-11 18:57:10 +02:00
|
|
|
"file|f", "The file that the data is written to.", &fileName,
|
|
|
|
"stop|s", "The bus stop for which to fetch data.", &busStop,
|
|
|
|
"replacement-file|r", "The file that contais the direction name replacement info.", &substitutionFileName);
|
|
|
|
// dfmt on
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
if (helpInformation.helpWanted)
|
|
|
|
{
|
2017-05-14 11:54:09 +02:00
|
|
|
defaultGetoptPrinter("Usage: bayernfahrplan [options]\n\n Options:", helpInformation.options);
|
2016-12-02 12:46:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:57:10 +02:00
|
|
|
// dfmt off
|
|
|
|
auto content = getContent(baseURL ~ departureMonitorRequest,
|
|
|
|
["outputFormat" : "XML",
|
|
|
|
"language" : "de",
|
|
|
|
"stateless" : "1",
|
|
|
|
"type_dm" : "stop",
|
|
|
|
"name_dm" : busStop,
|
|
|
|
"useRealtime" : "1",
|
|
|
|
"mode" : "direct",
|
|
|
|
"ptOptionActive" : "1",
|
|
|
|
"mergeDep" : "1",
|
|
|
|
"limit" : "20",
|
|
|
|
"deleteAssignedStops_dm" : "1"]);
|
|
|
|
// dfmt on
|
2017-04-10 22:08:46 +02:00
|
|
|
|
2017-04-10 21:07:53 +02:00
|
|
|
if (substitutionFileName.exists && substitutionFileName.isFile)
|
|
|
|
{
|
|
|
|
loadSubstitutionFile(substitutionFileName);
|
|
|
|
}
|
2016-12-02 12:46:39 +01:00
|
|
|
|
2016-12-29 18:39:10 +01:00
|
|
|
auto currentTime = Clock.currTime;
|
|
|
|
JSONValue j = ["time" : "%02s:%02s".format(currentTime.hour, currentTime.minute)];
|
2017-04-11 18:57:10 +02:00
|
|
|
|
2016-12-29 18:39:10 +01:00
|
|
|
j.object["departures"] = (cast(string) content.data).parsedFahrplan.array.JSONValue;
|
|
|
|
auto output = j.toPrettyString.replace("\\/", "/");
|
2016-12-02 12:46:39 +01:00
|
|
|
if (fileName !is null)
|
|
|
|
{
|
2016-12-29 18:20:08 +01:00
|
|
|
auto outfile = File(fileName, "w");
|
2017-01-11 14:42:04 +01:00
|
|
|
scope (exit)
|
|
|
|
outfile.close;
|
2016-12-29 18:20:08 +01:00
|
|
|
outfile.writeln(output);
|
2016-12-02 12:46:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-29 18:20:08 +01:00
|
|
|
output.writeln;
|
2016-12-02 12:46:39 +01:00
|
|
|
}
|
|
|
|
}
|