public-transport / hafas-client

JavaScript client for HAFAS public transport APIs.
ISC License
263 stars 52 forks source link

Destination sometimes missing in departure result #273

Closed bergmannjg closed 1 year ago

bergmannjg commented 1 year ago

Destination field sometimes is missing in departure result, even if there is a valid value.

The following condition succeeds only if tLocX is greater 0:

https://github.com/public-transport/hafas-client/blob/4189ce437bde4b33c0268baf4b94a0acf7b1d6a6/parse/arrival-or-departure.js#L40

I would suggest to replace it with

if (prefix === DEPARTURE && Array.isArray(d.prodL) && d.prodL.length > 0 {

Same with origin field in arrival result.

Thanks

derhuerst commented 1 year ago

Good catch!

I think changing the condition to … && d.prodL[0] && d.prodL[d.prodL[0].tLocX] is the most robust solution. What do you think?

bergmannjg commented 1 year ago

What is d.prodL[d.prodL[0].tLocX], do you mean ctx.res.common.locL[d.prodL[0].tLocX]?

That's good.

derhuerst commented 1 year ago

Yes, I got a little confused. 😄

derhuerst commented 1 year ago

Do you happen to have a test case where it doesn't parse the destination? I can add it as a test case then.

You can obtain raw HAFAS responses with DEBUG=hafas-client.

bergmannjg commented 1 year ago
const createClient = require('hafas-client')
const dbProfile = require('hafas-client/p/db')
let client = createClient(dbProfile, 'agent');

const options = { duration: 4800, results: 1, direction: '930200' };
client.departures('930200', options)
    .then(result => { console.log(result); });

the result is

[
  {
    tripId: '1|1616073|0|80|15102022',
    stop: {
      type: 'stop',
      id: '930200',
      name: 'Hauptbahnhof Bushst.1-3, Bielefeld',
      location: [Object],
      products: [Object],
      station: [Object]
    },
    when: '2022-10-15T15:46:00+02:00',
    plannedWhen: '2022-10-15T15:46:00+02:00',
    delay: null,
    platform: null,
    plannedPlatform: null,
    prognosisType: null,
    direction: 'Hauptbahnhof Bushst.1-3, Bielefeld',
    provenance: null,
    line: {
      type: 'line',
      id: '5-owl032-87',
      fahrtNr: '73374',
      name: 'Bus 87',
      public: true,
      adminCode: 'owl032',
      productName: 'Bus',
      mode: 'bus',
      product: 'bus',
      operator: [Object]
    },
    remarks: [],
    origin: null,
    destination: null
  }
]

and the raw hafas response is

{
  "ver": "1.34",
  "ext": "DB.R21.12.a",
  "lang": "eng",
  "id": "hsiwni6imgktg4wx",
  "err": "OK",
  "cInfo": {
    "code": "OK"
  },
  "graph": {
    "id": "standard",
    "index": 0
  },
  "subGraph": {
    "id": "global",
    "index": 0
  },
  "view": {
    "id": "standard",
    "index": 0,
    "type": "WGS84"
  },
  "svcResL": [
    {
      "meth": "StationBoard",
      "err": "OK",
      "res": {
        "common": {
          "locL": [
            {
              "lid": "A=1@O=Hauptbahnhof Bushst.1-3, Bielefeld@X=8532669@Y=52028585@U=80@L=930200@",
              "type": "S",
              "name": "Hauptbahnhof Bushst.1-3, Bielefeld",
              "icoX": 0,
              "extId": "930200",
              "state": "F",
              "crd": {
                "x": 8532669,
                "y": 52028585,
                "floor": 0
              },
              "pCls": 303,
              "pRefL": [
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8
              ],
              "mMastLocX": 1
            },
            {
              "lid": "A=1@O=Bielefeld Hbf@X=8532723@Y=52029259@U=80@L=8000036@",
              "type": "S",
              "name": "Bielefeld Hbf",
              "icoX": 2,
              "extId": "8000036",
              "state": "F",
              "crd": {
                "x": 8532777,
                "y": 52029421,
                "z": 0,
                "floor": 0
              },
              "pCls": 303,
              "pRefL": [
                9,
                10,
                11,
                12,
                13,
                14,
                15,
                16,
                17,
                18,
                19,
                20,
                21,
                22,
                23,
                1,
                2,
                24,
                4,
                5,
                25,
                6,
                26,
                27,
                28,
                29,
                7,
                8,
                30,
                31,
                32,
                33
              ]
            }
          ],
          "prodL": [
            {
              "name": "Bus 87",
              "nameS": "87",
              "number": "87",
              "icoX": 0,
              "cls": 32,
              "oprX": 0,
              "prodCtx": {
                "name": "Bus   87",
                "num": "73374",
                "line": "87",
                "lineId": "5_owl032_87",
                "matchId": "87",
                "catOut": "Bus",
                "catOutS": "Bus",
                "catOutL": "Bus",
                "catIn": "Bus",
                "catCode": "5",
                "admin": "owl032"
              }
            },
            {
              "name": "Bus 52",
              "nameS": "52",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   52",
                "line": "52",
                "lineId": "5_owl032_52",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 54",
              "nameS": "54",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   54",
                "line": "54",
                "lineId": "5_owl032_54",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 61",
              "nameS": "61",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   61",
                "line": "61",
                "lineId": "5_owl051_61",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 62",
              "nameS": "62",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   62",
                "line": "62",
                "lineId": "5_owl051_62",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 87",
              "nameS": "87",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   87",
                "line": "87",
                "lineId": "5_owl032_87",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 95",
              "nameS": "95",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   95",
                "line": "95",
                "lineId": "5_owl032_95",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 369",
              "nameS": "369",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus  369",
                "line": "369",
                "lineId": "5_owl032_369",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 80.2",
              "nameS": "80.2",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus 80.2",
                "line": "80.2",
                "lineId": "5_owl050_80.2",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "ICE",
              "icoX": 2,
              "cls": 1,
              "prodCtx": {
                "name": "ICE     ",
                "line": "",
                "catOut": "ICE     ",
                "catOutS": "ICE",
                "catOutL": "Intercity-Express"
              }
            },
            {
              "name": "EC",
              "icoX": 3,
              "cls": 2,
              "prodCtx": {
                "name": "EC      ",
                "line": "",
                "catOut": "EC      ",
                "catOutS": "EC",
                "catOutL": "Eurocity"
              }
            },
            {
              "name": "IC",
              "icoX": 4,
              "cls": 2,
              "prodCtx": {
                "name": "IC      ",
                "line": "",
                "catOut": "IC      ",
                "catOutS": "IC",
                "catOutL": "Intercity"
              }
            },
            {
              "name": "FLX",
              "icoX": 5,
              "cls": 4,
              "prodCtx": {
                "name": "FLX",
                "line": "",
                "catOut": "DPF     ",
                "catOutS": "DPF",
                "catOutL": "Fernreisezug externer EU"
              }
            },
            {
              "name": "Bus 61",
              "nameS": "61",
              "icoX": 0,
              "cls": 8,
              "prodCtx": {
                "name": "Bus   61",
                "line": "61",
                "lineId": "3_R2_____61!!949218!!5787966",
                "catOut": "Bus     ",
                "catOutS": "Bsv",
                "catOutL": "SEV-Bus"
              }
            },
            {
              "name": "Bus 69",
              "nameS": "69",
              "icoX": 0,
              "cls": 8,
              "prodCtx": {
                "name": "Bus   69",
                "line": "69",
                "lineId": "3_R2_____69!!949218!!5787966",
                "catOut": "Bus     ",
                "catOutS": "Bsv",
                "catOutL": "SEV-Bus"
              }
            },
            {
              "name": "Bus 78",
              "nameS": "78",
              "icoX": 0,
              "cls": 8,
              "prodCtx": {
                "name": "Bus   78",
                "line": "78",
                "lineId": "3_R2_____78!!949218!!5787966",
                "catOut": "Bus     ",
                "catOutS": "Bsv",
                "catOutL": "SEV-Bus"
              }
            },
            {
              "name": "Bus 82",
              "nameS": "82",
              "icoX": 0,
              "cls": 8,
              "prodCtx": {
                "name": "Bus   82",
                "line": "82",
                "lineId": "3_R2_____82",
                "catOut": "Bus     ",
                "catOutS": "Bsv",
                "catOutL": "SEV-Bus"
              }
            },
            {
              "name": "Bus SEV",
              "nameS": "SEV",
              "icoX": 0,
              "cls": 8,
              "prodCtx": {
                "name": "Bus  SEV",
                "line": "SEV",
                "lineId": "3_NWBus__SEV!!896828!!5815064",
                "catOut": "Bus     ",
                "catOutS": "Bsv",
                "catOutL": "SEV-Bus"
              }
            },
            {
              "name": "NWB",
              "icoX": 6,
              "cls": 8,
              "prodCtx": {
                "name": "NWB",
                "line": "",
                "catOut": "DPN     ",
                "catOutS": "DPN",
                "catOutL": "Nahreisezug"
              }
            },
            {
              "name": "RB",
              "icoX": 6,
              "cls": 8,
              "prodCtx": {
                "name": "RB ",
                "line": "",
                "catOut": "DPN     ",
                "catOutS": "DPN",
                "catOutL": "Nahreisezug"
              }
            },
            {
              "name": "RE",
              "icoX": 6,
              "cls": 8,
              "prodCtx": {
                "name": "RE ",
                "line": "",
                "catOut": "DPN     ",
                "catOutS": "DPN",
                "catOutL": "Nahreisezug"
              }
            },
            {
              "name": "TRI",
              "icoX": 6,
              "cls": 8,
              "prodCtx": {
                "name": "TRI",
                "line": "",
                "catOut": "DPN     ",
                "catOutS": "DPN",
                "catOutL": "Nahreisezug"
              }
            },
            {
              "name": "WFB",
              "icoX": 6,
              "cls": 8,
              "prodCtx": {
                "name": "WFB",
                "line": "",
                "catOut": "DPN     ",
                "catOutS": "DPN",
                "catOutL": "Nahreisezug"
              }
            },
            {
              "name": "Bus 48",
              "nameS": "48",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   48",
                "line": "48",
                "lineId": "5_owl051_48",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 59",
              "nameS": "59",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   59",
                "line": "59",
                "lineId": "5_owl051_59",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 88",
              "nameS": "88",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   88",
                "line": "88",
                "lineId": "5_owl051_88",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus N8",
              "nameS": "N8",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus   N8",
                "line": "N8",
                "lineId": "5_owl032_N8",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 196",
              "nameS": "196",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus  196",
                "line": "196",
                "lineId": "5_owl032_196",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 350",
              "nameS": "350",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus  350",
                "line": "350",
                "lineId": "5_owl021_350",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "Bus 351",
              "nameS": "351",
              "icoX": 0,
              "cls": 32,
              "prodCtx": {
                "name": "Bus  351",
                "line": "351",
                "lineId": "5_owl021_351",
                "catOut": "Bus     ",
                "catOutS": "Bus",
                "catOutL": "Bus"
              }
            },
            {
              "name": "STB 1",
              "nameS": "1",
              "icoX": 7,
              "cls": 256,
              "prodCtx": {
                "name": "STB    1",
                "line": "1",
                "lineId": "8_owl031_1",
                "catOut": "STB     ",
                "catOutS": "stb",
                "catOutL": "Stadtbahn"
              }
            },
            {
              "name": "STB 2",
              "nameS": "2",
              "icoX": 7,
              "cls": 256,
              "prodCtx": {
                "name": "STB    2",
                "line": "2",
                "lineId": "8_owl031_2",
                "catOut": "STB     ",
                "catOutS": "stb",
                "catOutL": "Stadtbahn"
              }
            },
            {
              "name": "STB 3",
              "nameS": "3",
              "icoX": 7,
              "cls": 256,
              "prodCtx": {
                "name": "STB    3",
                "line": "3",
                "lineId": "8_owl031_3",
                "catOut": "STB     ",
                "catOutS": "stb",
                "catOutL": "Stadtbahn"
              }
            },
            {
              "name": "STB 4",
              "nameS": "4",
              "icoX": 7,
              "cls": 256,
              "prodCtx": {
                "name": "STB    4",
                "line": "4",
                "lineId": "8_owl031_4",
                "catOut": "STB     ",
                "catOutS": "stb",
                "catOutL": "Stadtbahn"
              }
            }
          ],
          "opL": [
            {
              "name": "Nahreisezug",
              "icoX": 1
            }
          ],
          "icoL": [
            {
              "res": "Bus"
            },
            {
              "res": "DPN",
              "txt": "Nahreisezug"
            },
            {
              "res": "ICE"
            },
            {
              "res": "EC"
            },
            {
              "res": "IC"
            },
            {
              "res": "DPF"
            },
            {
              "res": "DPN"
            },
            {
              "res": "STB"
            }
          ],
          "lDrawStyleL": [
            {
              "sIcoX": 0,
              "type": "SOLID"
            },
            {
              "type": "SOLID"
            }
          ]
        },
        "type": "DEP",
        "jnyL": [
          {
            "jid": "1|1616073|0|80|15102022",
            "date": "20221015",
            "prodX": 0,
            "dirTxt": "Hauptbahnhof Bushst.1-3, Bielefeld",
            "dirFlg": "2",
            "status": "P",
            "isRchbl": true,
            "stbStop": {
              "locX": 0,
              "idx": 0,
              "dProdX": 0,
              "dTimeS": "154600",
              "dTZOffset": 120,
              "type": "N"
            },
            "subscr": "F",
            "prodL": [
              {
                "prodX": 0,
                "fLocX": 0,
                "tLocX": 0,
                "fIdx": 0,
                "tIdx": 37
              }
            ],
            "sumLDrawStyleX": 0,
            "resLDrawStyleX": 1,
            "trainStartDate": "20221015"
          }
        ],
        "fpB": "20211212",
        "fpE": "20231209",
        "planrtTS": "1665819982",
        "sD": "20221015",
        "sT": "094752",
        "locRefL": [
          0
        ]
      }
    }
  ]
}
derhuerst commented 1 year ago

Fix published as hafas-client@5.26.1. 🎉

bergmannjg commented 1 year ago

Thanks for the quick response and for mentioning typing and libs.