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 
import ../types
import ../util
import ./products
import json
import options
import tables
import httpClient
import asyncdispatch


var trainTypes = initTable[string, string]()
var trainTypesShort = initTable[string, string]()

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

  if not isNone(line.prodCtx.catOut):
    res.productName = get(line.prodCtx.catOut)
  else:
    res.productName = "?"

  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 trainTypesShort.contains(res.tripNum):
      res.trainTypeShort = some(trainTypesShort[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)