ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

commit ecca984731df1d1ee1cae80fc84486016071840d
parent 16ad4d29769de079ff6b06eb224abdd5b71b7730
Author: Leah Thein <leah@toaster.fritz.box>
Date: Fri, 4 Dec 2020 17:33:45 +0100

update backend
5 files changed, 45 insertions(+), 4 deletions(-)
diff --git a/src/backend/hafas/parse/journey.nim b/src/backend/hafas/parse/journey.nim
@@ -8,6 +8,12 @@ proc mkParseJourney*(common: CommonData): proc =
     var common = common
     common.dateStr = j{"date"}.getStr()
 
+    if j{"trfRes"}{"statusCode"}.getStr == "OK":
+      result.price = some(Price(
+        amount: j["trfRes"]["fareSetL"][0]["fareL"][0]["prc"].getInt / 100,
+        currency: some("Euro"),
+      ))
+
     result.refreshToken = j{"ctxRecon"}.getStr()
     result.legs = j{"secL"}.getElems().map(mkParseLeg(common))
     result.lastUpdated = common.timeStamp
diff --git a/src/backend/hafas/parse/journeys_response.nim b/src/backend/hafas/parse/journeys_response.nim
@@ -20,5 +20,8 @@ proc parseJourneysResponse*(data: JsonNode, isRefresh: bool = false): JourneysRe
 
   result.journeys = data["res"]["outConL"].getElems().map(mkParseJourney(common))
   if not isRefresh:
-    result.earlierRef = data["res"]["outCtxScrB"].getStr()
-    result.laterRef = data["res"]["outCtxScrF"].getStr()
+    if data["res"].hasKey("outCtxScrB"):
+      result.earlierRef = data["res"]["outCtxScrB"].getStr()
+    
+    if data["res"].hasKey("outCtxScrF"):
+      result.laterRef = data["res"]["outCtxScrF"].getStr()
diff --git a/src/backend/hafas/parse/line.nim b/src/backend/hafas/parse/line.nim
@@ -3,6 +3,26 @@ import ../util
 import ./products
 import json
 import options
+import tables
+import httpClient
+import asyncdispatch
+
+
+var trainTypes = initTable[string, string]()
+var trainTypesShort = initTable[string, string]()
+
+proc fetchTrainTypes() {.async.} =
+  var client = newAsyncHttpClient()
+  let resp = await client.getContent("https://lib.finalrewind.org/dbdb/ice_type.json")
+  let data = parseJson(resp)
+  for key, info in pairs(data):
+    if info{"type"}.getStr != "" and info{"type"}.getStr != "EC" and info{"type"}.getStr != "IC":
+      trainTypes[key] = info{"type"}.getStr
+    if info{"short"}.getStr != "":
+      trainTypesShort[key] = info{"short"}.getStr
+
+asyncCheck fetchTrainTypes()
+
 
 proc parseLine*(common: CommonData, i: int): Option[Line] =
   let l = common.lines[i]

@@ -17,7 +37,12 @@ proc parseLine*(common: CommonData, i: int): Option[Line] =
   res.name = line.name
   res.product = parseProduct(line.cls)
   res.tripNum = line.prodCtx.num
-  res.productName = line.prodCtx.catOut
+
+  if not isNone(line.prodCtx.catOut):
+    res.productName = get(line.prodCtx.catOut)
+  else:
+    res.productName = "?"
+
   res.fullProductName = line.prodCtx.catOutL
   res.id = slug(line.prodCtx.lineId.get(line.name))
 

@@ -26,6 +51,12 @@ proc parseLine*(common: CommonData, i: int): Option[Line] =
 
   # DB
 
+  if res.productName == "IC" or res.productName == "ICE" or res.productName == "EC" or res.productName == "ECE":
+    if trainTypes.contains(res.tripNum) and trainTypes[res.tripNum] != res.productName:
+      res.trainType = some(trainTypes[res.tripNum])
+    if trainTypesShort.contains(res.tripNum):
+      res.trainTypeShort = some(trainTypesShort[res.tripNum])
+
   if line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
     res.name = line.nameS.get
 
diff --git a/src/backend/hafas/types.nim b/src/backend/hafas/types.nim
@@ -31,7 +31,7 @@ type
   HafasProdCtx* = object
     name*:     string
     num*:      string
-    catOut*:   string
+    catOut*:   Option[string]
     catOutL*:  string
     lineId*:   Option[string]
 
diff --git a/src/backend/hafas/util.nim b/src/backend/hafas/util.nim
@@ -48,6 +48,7 @@ proc request*(req: JsonNode): Future[JsonNode] {.async.} =
   #echo pretty body
   let req = await client.request(url, httpMethod = HttpPost, body = $body)
   let resp = await req.body
+  echo resp
   let data = parseJson(resp)
 
   let error = data{"svcResL"}{0}{"err"}.getStr()