bayernfahrplan/source/app.d

56 lines
1.7 KiB
D
Raw Normal View History

2016-12-02 12:46:39 +01:00
import std.getopt : defaultGetoptPrinter, getopt;
import std.stdio : File, stdout, writeln;
2016-12-29 18:39:10 +01:00
import std.datetime : Clock;
import std.json : JSONValue;
import std.format : format;
import std.array: array, replace;
2016-12-02 12:46:39 +01:00
import requests : postContent;
import fahrplanparser;
2016-12-02 12:46:39 +01:00
import substitution;
2016-12-02 12:46:39 +01:00
void main(string[] args)
{
string fileName;
string busStop = "Universität Regensburg";
string substitutionFileName = "replacement.txt";
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);
if (helpInformation.helpWanted)
{
defaultGetoptPrinter("Some information about the program.", helpInformation.options);
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"]);
loadSubstitutionFile(substitutionFileName);
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)
{
auto outfile = File(fileName, "w");
scope(exit) outfile.close;
outfile.writeln(output);
2016-12-02 12:46:39 +01:00
}
else
{
output.writeln;
2016-12-02 12:46:39 +01:00
}
}