backend/hafas: only parse lines that are used

This commit is contained in:
Milan Pässler 2020-02-07 18:29:03 +01:00
parent b065c3695b
commit d4aae8d34f
5 changed files with 36 additions and 44 deletions

View file

@ -1,9 +1,5 @@
import ../types
import ../parse/remark
import ../parse/point
import ../parse/operator
import ../parse/journey
import ../parse/line
import ../parse/journeys_response
import ../util
import json
import asyncdispatch
@ -25,10 +21,4 @@ proc refreshJourney*(params: RefreshJourneyParams): Future[Journey] {.async.} =
}
let data = await request(req)
let points = map(data["res"]["common"]["locL"].getElems(), parsePoint)
let operators = map(data["res"]["common"]["opL"].getElems(), parseOperator)
let remarks = map(data["res"]["common"]["remL"].getElems(), parseRemark)
let lines = map(data["res"]["common"]["prodL"].getElems(), mkParseLine(operators))
let common = CommonData(points: points, operators: operators, remarks: remarks, lines: lines)
return common.mkParseJourney()(data["res"]["outConL"][0])
return parseJourneysResponse(data, true).journeys[0]

View file

@ -8,14 +8,15 @@ import json
import sequtils
import strutils
proc parseJourneysResponse*(data: JsonNode): JourneysResponse =
proc parseJourneysResponse*(data: JsonNode, isRefresh: bool = false): JourneysResponse =
let points = map(data["res"]["common"]["locL"].getElems(), parsePoint)
let operators = map(data["res"]["common"]["opL"].getElems(), parseOperator)
let remarks = map(data["res"]["common"]["remL"].getElems(), parseRemark)
let lines = map(data["res"]["common"]["prodL"].getElems(), mkParseLine(operators))
let lines = data["res"]["common"]["prodL"]
let timestamp = parseInt(data["res"]["planrtTS"].getStr())
let common = CommonData(points: points, operators: operators, remarks: remarks, lines: lines, timestamp: timestamp)
result.journeys = data["res"]["outConL"].getElems().map(mkParseJourney(common))
result.earlierRef = data["res"]["outCtxScrB"].getStr()
result.laterRef = data["res"]["outCtxScrF"].getStr()
if not isRefresh:
result.earlierRef = data["res"]["outCtxScrB"].getStr()
result.laterRef = data["res"]["outCtxScrF"].getStr()

View file

@ -2,6 +2,7 @@ import ../types
import ./stopover
import ./msg
import ./date
import ./line
import json
import options
@ -30,7 +31,7 @@ proc mkParseLeg*(common: CommonData): proc =
if typeStr == "JNY":
result.direction = some(l{"jny"}{"dirTxt"}.getStr())
result.tripId = some(l{"jny"}{"jid"}.getStr())
result.line = common.lines[l{"jny"}{"prodX"}.getInt()]
result.line = common.parseLine(l["jny"]["prodX"].getInt())
let stopovers = l{"jny"}{"stopL"}.getElems()
if stopovers.len > 0:

View file

@ -4,38 +4,37 @@ import ./products
import json
import options
proc mkParseLine*(commonOperators: seq[Operator]): proc =
proc parseLine(l: JsonNode): Option[Line] =
# unparsable
if l{"cls"}.getInt == 0:
return options.none(Line)
proc parseLine*(common: CommonData, i: int): Option[Line] =
let l = common.lines[i]
let line = l.to(HafasProd)
var res = Line()
# unparsable
if l{"cls"}.getInt == 0:
return options.none(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))
let line = l.to(HafasProd)
var res = Line()
if line.opX.isSome:
res.operator = some(commonOperators[line.opX.get])
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))
# DB
if line.opX.isSome:
res.operator = some(common.operators[line.opX.get])
if line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
res.name = line.nameS.get
# DB
if line.addName.isSome:
# swap name and addName
res.additionalName = some(res.name)
res.name = line.addName.get
if line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
res.name = line.nameS.get
# End DB
if line.addName.isSome:
# swap name and addName
res.additionalName = some(res.name)
res.name = line.addName.get
res.mode = MODES[int(res.product)]
return some(res)
# End DB
return parseLine
res.mode = MODES[int(res.product)]
return some(res)

View file

@ -1,10 +1,11 @@
import ../../types
import options
export types
import json
type
CommonData* = object
lines*: seq[Option[Line]]
lines*: JsonNode
remarks*: seq[Remark]
operators*: seq[Operator]
points*: seq[Point]