Restructured a bit again

This commit is contained in:
Johannes Loher 2016-12-29 18:39:10 +01:00
parent 960b4d3f6f
commit caab61ddb8
2 changed files with 15 additions and 11 deletions

View file

@ -1,5 +1,10 @@
import std.getopt : defaultGetoptPrinter, getopt; import std.getopt : defaultGetoptPrinter, getopt;
import std.stdio : File, stdout, writeln; import std.stdio : File, stdout, writeln;
import std.datetime : Clock;
import std.json : JSONValue;
import std.format : format;
import std.array: array, replace;
import requests : postContent; import requests : postContent;
@ -32,7 +37,10 @@ void main(string[] args)
loadSubstitutionFile(substitutionFileName); loadSubstitutionFile(substitutionFileName);
auto output = (cast(string) content.data).parseFahrplan; 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("\\/", "/");
if (fileName !is null) if (fileName !is null)
{ {
auto outfile = File(fileName, "w"); auto outfile = File(fileName, "w");

View file

@ -1,11 +1,11 @@
module fahrplanparser; module fahrplanparser;
private:
import std.algorithm : filter, map, startsWith; import std.algorithm : filter, map, startsWith;
import std.array : array, empty, front, replace; import std.array : empty, front, replace;
import std.conv : to; import std.conv : to;
import std.datetime : dur, TimeOfDay, Clock; import std.datetime : dur, TimeOfDay, Clock;
import std.format : format;
import std.json : JSONValue;
import std.regex : ctRegex, matchAll; import std.regex : ctRegex, matchAll;
import std.string : strip; import std.string : strip;
import std.typecons : tuple; import std.typecons : tuple;
@ -16,20 +16,16 @@ import substitution;
public: public:
auto parseFahrplan(in string data) auto parsedFahrplan(in string data)
{ {
auto currentTime = Clock.currTime; return data.readDocument
JSONValue j = ["time" : "%02s:%02s".format(currentTime.hour, currentTime.minute)];
j.object["departures"] = data.readDocument
.parseXPath(`//table[@id="departureMonitor"]/tbody/tr`)[1 .. $] .parseXPath(`//table[@id="departureMonitor"]/tbody/tr`)[1 .. $]
.getRowContents .getRowContents
.filter!(row => !row.empty) .filter!(row => !row.empty)
.map!(a => ["departure" : a[0].parseTime[0].to!string[0 .. $ - 3], .map!(a => ["departure" : a[0].parseTime[0].to!string[0 .. $ - 3],
"delay" : a[0].parseTime[1].total!"minutes".to!string, "delay" : a[0].parseTime[1].total!"minutes".to!string,
"line" : a[1], "line" : a[1],
"direction" : a[2].substitute]) "direction" : a[2].substitute]);
.array.JSONValue;
return j.toPrettyString.replace("\\/", "/");
} }
private: private: