From caab61ddb8827c6dc13de6e9b184129709b3adb0 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Thu, 29 Dec 2016 18:39:10 +0100 Subject: [PATCH] Restructured a bit again --- source/app.d | 10 +++++++++- source/fahrplanparser.d | 16 ++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/source/app.d b/source/app.d index 4865685..089c145 100644 --- a/source/app.d +++ b/source/app.d @@ -1,5 +1,10 @@ import std.getopt : defaultGetoptPrinter, getopt; 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; @@ -32,7 +37,10 @@ void main(string[] args) 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) { auto outfile = File(fileName, "w"); diff --git a/source/fahrplanparser.d b/source/fahrplanparser.d index b546f52..caae9bc 100644 --- a/source/fahrplanparser.d +++ b/source/fahrplanparser.d @@ -1,11 +1,11 @@ module fahrplanparser; +private: + 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.datetime : dur, TimeOfDay, Clock; -import std.format : format; -import std.json : JSONValue; import std.regex : ctRegex, matchAll; import std.string : strip; import std.typecons : tuple; @@ -16,20 +16,16 @@ import substitution; public: -auto parseFahrplan(in string data) +auto parsedFahrplan(in string data) { - auto currentTime = Clock.currTime; - JSONValue j = ["time" : "%02s:%02s".format(currentTime.hour, currentTime.minute)]; - j.object["departures"] = data.readDocument + return data.readDocument .parseXPath(`//table[@id="departureMonitor"]/tbody/tr`)[1 .. $] .getRowContents .filter!(row => !row.empty) .map!(a => ["departure" : a[0].parseTime[0].to!string[0 .. $ - 3], "delay" : a[0].parseTime[1].total!"minutes".to!string, "line" : a[1], - "direction" : a[2].substitute]) - .array.JSONValue; - return j.toPrettyString.replace("\\/", "/"); + "direction" : a[2].substitute]); } private: