ctucx.git: nimhafas

[nimlang] hafas-client library

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
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)