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 ../types
import ../parse/remark import ../parse/journeys_response
import ../parse/point
import ../parse/operator
import ../parse/journey
import ../parse/line
import ../util import ../util
import json import json
import asyncdispatch import asyncdispatch
@ -25,10 +21,4 @@ proc refreshJourney*(params: RefreshJourneyParams): Future[Journey] {.async.} =
} }
let data = await request(req) let data = await request(req)
let points = map(data["res"]["common"]["locL"].getElems(), parsePoint) return parseJourneysResponse(data, true).journeys[0]
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])

View file

@ -8,14 +8,15 @@ import json
import sequtils import sequtils
import strutils 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 points = map(data["res"]["common"]["locL"].getElems(), parsePoint)
let operators = map(data["res"]["common"]["opL"].getElems(), parseOperator) let operators = map(data["res"]["common"]["opL"].getElems(), parseOperator)
let remarks = map(data["res"]["common"]["remL"].getElems(), parseRemark) 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 timestamp = parseInt(data["res"]["planrtTS"].getStr())
let common = CommonData(points: points, operators: operators, remarks: remarks, lines: lines, timestamp: timestamp) let common = CommonData(points: points, operators: operators, remarks: remarks, lines: lines, timestamp: timestamp)
result.journeys = data["res"]["outConL"].getElems().map(mkParseJourney(common)) result.journeys = data["res"]["outConL"].getElems().map(mkParseJourney(common))
result.earlierRef = data["res"]["outCtxScrB"].getStr() if not isRefresh:
result.laterRef = data["res"]["outCtxScrF"].getStr() result.earlierRef = data["res"]["outCtxScrB"].getStr()
result.laterRef = data["res"]["outCtxScrF"].getStr()

View file

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

View file

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

View file

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