oeffisearch/src/backend/hafas/api/journeys.nim
2020-02-07 14:09:42 +01:00

80 lines
2.1 KiB
Nim

import ../parse/products
import ../parse/point
import ../parse/accessibility
import ../parse/journeys_response
import ../types
import ../util
import json
import times
import asyncdispatch
import options
proc journeys*(params: JourneysParams): Future[JourneysResponse] {.async.} =
var `when` = now().toTime()
var isDeparture = true
if params.departure.isSome:
`when` = params.departure.get.fromUnix
elif params.arrival.isSome:
`when` = params.arrival.get.fromUnix
isDeparture = false
let req = %* {
"cfg": {
"polyEnc": "GPA"
},
"meth": "TripSearch",
"req": {
"ctxScr": nil,
"getPasslist": params.stopovers.get(false),
"maxChg": params.transfers.get(-1),
"minChgTime": params.transferTime.get(0),
"numF": params.results.get(5),
"depLocL": [ params.fromPoint.formatPoint() ],
"viaLocL": [],
"arrLocL": [ params.toPoint.formatPoint() ],
"jnyFltrL": [
{
"type": "PROD",
"mode": "INC",
"value": $formatProducts(params.products.get(parseProducts(1023))),
},
{
"type": "META",
"mode": "INC",
"meta": formatAccessibility(params.accessibility.get(Accessibility.none)),
}
],
"gisFltrL": [],
"getTariff": params.tickets.get(true),
"ushrp": params.startWithWalking.get(true),
"getPT": true,
"getIV": false,
"getPolyline": params.polylines.get(false),
"outFrwd": isDeparture,
"outDate": `when`.format("yyyyMMdd"),
"outTime": `when`.format("HHmmss"),
"trfReq": {
"jnyCl": 2,
"tvlrProf": [
{
"type": "E",
"redtnCard": nil
}
],
"cType": "PK"
}
}
}
if params.laterRef.isSome:
req["req"]["ctxScr"] = %* params.laterRef.get
elif params.earlierRef.isSome:
req["req"]["ctxScr"] = %* params.earlierRef.get
if params.viaPoint.isSome:
let viaPoint = params.viaPoint.get
req["req"]["viaLocL"] = %* [{ "loc": viaPoint.formatPoint() }]
let data = await request(req)
return parseJourneysResponse(data)