oeffisearch/src/backend/hafas/parse/line.nim
2020-09-11 19:47:00 +02:00

62 lines
1.6 KiB
Nim

import ../types
import ../util
import ./products
import json
import options
import tables
import httpClient
import asyncdispatch
var trainTypes = 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
asyncCheck fetchTrainTypes()
proc parseLine*(common: CommonData, i: int): Option[Line] =
let l = common.lines[i]
# unparsable
if l{"cls"}.getInt == 0:
return options.none(Line)
let line = l.to(HafasProd)
var res = Line()
res.name = line.name
res.product = parseProduct(line.cls)
res.tripNum = line.prodCtx.num
res.productName = line.prodCtx.catOut
res.fullProductName = line.prodCtx.catOutL
res.id = slug(line.prodCtx.lineId.get(line.name))
if line.opX.isSome:
res.operator = some(common.operators[line.opX.get])
# 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 line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
res.name = line.nameS.get
if line.addName.isSome:
# swap name and addName
res.additionalName = some(res.name)
res.name = line.addName.get
# End DB
res.mode = MODES[int(res.product)]
return some(res)