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
|
|
|
|
|
|
|
import requests : postContent;
|
|
|
|
|
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
|
|
|
|
|
|
|
void main(string[] args)
|
|
|
|
{
|
|
|
|
string fileName;
|
2017-04-10 20:35:41 +02:00
|
|
|
string busStop = "4014080";
|
2016-12-02 12:46:39 +01:00
|
|
|
string substitutionFileName = "replacement.txt";
|
2017-01-11 14:42:04 +01:00
|
|
|
|
2016-12-02 12:46:39 +01:00
|
|
|
auto helpInformation = getopt(args,
|
|
|
|
"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);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto content = postContent("http://txt.bayern-fahrplan.de/textversion/bcl_abfahrtstafel",
|
|
|
|
["limit" : "20",
|
|
|
|
"useRealtime" : "1",
|
|
|
|
"name_dm" : busStop,
|
|
|
|
"mode" : "direct",
|
|
|
|
"type_dm" : "any",
|
|
|
|
"itdLPxx_bcl" : "true"]);
|
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)];
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|