ctucx.git: kvg-kiel-station-departures

get bus-departures in the kiel-region as a simple json

commit 38c2dba0879635cede2e29051831dfe2c71c0f86
Author: Isa <hi@f2k1.de>
Date: Thu, 27 Jan 2022 15:49:45 +0100

initial commit
4 files changed, 44 insertions(+), 0 deletions(-)
A
.gitignore
|
1
+
A
kvg_kiel_station_departures.nimble
|
13
+++++++++++++
A
nim.cfg
|
1
+
A
src/kvg_kiel_station_departures.nim
|
29
+++++++++++++++++++++++++++++
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+kvg_kiel_station_departures
diff --git a/kvg_kiel_station_departures.nimble b/kvg_kiel_station_departures.nimble
@@ -0,0 +1,13 @@
+# Package
+
+version       = "0.1.0"
+author        = "Isa"
+description   = "renders simple json containing departures based on given station-ids"
+license       = "MIT"
+srcDir        = "src"
+bin           = @["kvg_kiel_station_departures"]
+
+
+# Dependencies
+
+requires "nim >= 1.4.8"
diff --git a/nim.cfg b/nim.cfg
@@ -0,0 +1 @@
+-d:ssl
diff --git a/src/kvg_kiel_station_departures.nim b/src/kvg_kiel_station_departures.nim
@@ -0,0 +1,29 @@
+import std/httpclient
+import std/[strutils, strtabs]
+import std/json
+import std/cgi
+
+when not defined(release):
+  setTestData("stations", "1505,2946,2187")
+
+let client  = newHttpClient()
+let getData = readData()
+var result  = newJObject()
+
+validateData(getData, "stations")
+
+for id in getData["stations"].split(","): 
+  let jsonData   = parseJson(client.getContent("https://www.kvg-kiel.de/internetservice/services/passageInfo/stopPassages/stop?stop=" & $id & "&mode=departure&language=en"))
+  var departures = newJArray()
+
+  for entry in jsonData{"actual"}:
+    departures.add(%* {
+      "line":         entry{"patternText"},
+      "direction":    entry{"direction"},
+      "departure_in": entry{"mixedTime"}.getStr.replace(" %UNIT_MIN%", "")
+    })
+
+  result.add(jsonData{"stopName"}.getStr, departures)
+
+write(stdout, "Content-Type: application/json\n\n")
+write(stdout, $result)