bayernfahrplan/source/app.d

76 lines
2.3 KiB
D
Raw Normal View History

import std.array : array, replace;
2016-12-29 18:39:10 +01:00
import std.datetime : Clock;
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 : getContent;
2016-12-02 12:46:39 +01:00
import fahrplanparser;
2016-12-02 12:46:39 +01:00
import substitution;
2016-12-02 12:46:39 +01:00
// find stops: http://mobile.defas-fgi.de/beg/XML_STOPFINDER_REQUEST?outputFormat=XML&stateless=1&locationServerActive=1&regionID_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";
// dfmt off
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);
// dfmt on
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;
}
// 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
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)];
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)
{
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
}
}