From 4529a93175db240dfcb19758166257f28b9faa71 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 9 Jul 2018 19:26:15 +0200 Subject: [PATCH] parse DB-specific hints inside DB profile --- p/db/index.js | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++ parse/hint.js | 40 +-------- 2 files changed, 222 insertions(+), 36 deletions(-) diff --git a/p/db/index.js b/p/db/index.js index 345f94f6..3d4dbb72 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -1,6 +1,9 @@ 'use strict' +const trim = require('lodash/trim') + const _createParseJourney = require('../../parse/journey') +const _parseHint = require('../../parse/hint') const _formatStation = require('../../format/station') const {bike} = require('../../format/filters') @@ -76,6 +79,220 @@ const createParseJourney = (profile, opt, data) => { return parseJourneyWithPrice } +const hintsByCode = Object.assign(Object.create(null), { + fb: { + type: 'hint', + code: 'bicycle-conveyance', + summary: 'bicycles conveyed' + }, + fr: { + type: 'hint', + code: 'bicycle-conveyance-reservation', + summary: 'bicycles conveyed, subject to reservation' + }, + nf: { + type: 'hint', + code: 'no-bicycle-conveyance', + summary: 'bicycles not conveyed' + }, + k2: { + type: 'hint', + code: '2nd-class-only', + summary: '2. class only' + }, + eh: { + type: 'hint', + code: 'boarding-ramp', + summary: 'vehicle-mounted boarding ramp available' + }, + ro: { + type: 'hint', + code: 'wheelchairs-space', + summary: 'space for wheelchairs' + }, + oa: { + type: 'hint', + code: 'wheelchairs-space-reservation', + summary: 'space for wheelchairs, subject to reservation' + }, + wv: { + type: 'hint', + code: 'wifi', + summary: 'WiFi available' + }, + wi: { + type: 'hint', + code: 'wifi', + summary: 'WiFi available' + }, + sn: { + type: 'hint', + code: 'snacks', + summary: 'snacks available for purchase' + }, + mb: { + type: 'hint', + code: 'snacks', + summary: 'snacks available for purchase' + }, + mp: { + type: 'hint', + code: 'snacks', + summary: 'snacks available for purchase at the seat' + }, + bf: { + type: 'hint', + code: 'barrier-free', + summary: 'barrier-free' + }, + rg: { + type: 'hint', + code: 'barrier-free-vehicle', + summary: 'barrier-free vehicle' + }, + bt: { + type: 'hint', + code: 'on-board-bistro', + summary: 'Bordbistro available' + }, + br: { + type: 'hint', + code: 'on-board-restaurant', + summary: 'Bordrestaurant available' + }, + ki: { + type: 'hint', + code: 'childrens-area', + summary: `children's area available` + }, + kk: { + type: 'hint', + code: 'parents-childrens-compartment', + summary: `parent-and-children compartment available` + }, + kr: { + type: 'hint', + code: 'kids-service', + summary: 'DB Kids Service available' + }, + ls: { + type: 'hint', + code: 'power-sockets', + summary: 'power sockets available' + }, + ev: { + type: 'hint', + code: 'replacement-service', + summary: 'replacement service' + }, + kl: { + type: 'hint', + code: 'air-conditioned', + summary: 'air-conditioned vehicle' + }, + r0: { + type: 'hint', + code: 'upward-escalator', + summary: 'upward escalator' + }, + au: { + type: 'hint', + code: 'elevator', + summary: 'elevator available' + }, + ck: { + type: 'hint', + code: 'komfort-checkin', + summary: 'Komfort-Checkin available' + }, + it: { + type: 'hint', + code: 'ice-sprinter', + summary: 'ICE Sprinter service' + }, + rp: { + type: 'hint', + code: 'compulsory-reservation', + summary: 'compulsory seat reservation' + }, + rm: { + type: 'hint', + code: 'optional-reservation', + summary: 'optional seat reservation' + }, + scl: { + type: 'hint', + code: 'all-2nd-class-seats-reserved', + summary: 'all 2nd class seats reserved' + }, + acl: { + type: 'hint', + code: 'all-seats-reserved', + summary: 'all seats reserved' + }, + sk: { + type: 'hint', + code: 'oversize-luggage-forbidden', + summary: 'oversize luggage not allowed' + }, + hu: { + type: 'hint', + code: 'animals-forbidden', + summary: 'animals not allowed, except guide dogs' + }, + ik: { + type: 'hint', + code: 'baby-cot-required', + summary: 'baby cot/child seat required' + }, + ee: { + type: 'hint', + code: 'on-board-entertainment', + summary: 'on-board entertainment available' + }, + toilet: { + type: 'hint', + code: 'toilet', + summary: 'toilet available' + }, + oc: { + type: 'hint', + code: 'wheelchair-accessible-toilet', + summary: 'wheelchair-accessible toilet available' + }, + iz: { + type: 'hint', + code: 'intercity-2', + summary: 'Intercity 2' + } +}) + +const codesByText = Object.assign(Object.create(null), { + 'journey cancelled': 'journey-cancelled', // todo: German variant + 'stop cancelled': 'stop-cancelled', // todo: change to `stopover-cancelled`, German variant + 'signal failure': 'signal-failure', + 'signalstörung': 'signal-failure', + 'additional stop': 'additional-stopover', // todo: German variant + 'platform change': 'changed platform', // todo: use dash, German variant +}) + +const parseHint = (profile, h, icons) => { + if (h.type === 'A') { + const hint = hintsByCode[h.code && h.code.trim().toLowerCase()] + if (hint) { + return Object.assign({text: h.txtN}, hint) + } + } + + const res = _parseHint(profile, h, icons) + if (h.txtN) { + console.error(h) + const text = trim(h.txtN.toLowerCase(), ' ()') + if (codesByText[text]) res.code = codesByText[text] + } + return res +} + const isIBNR = /^\d{6,}$/ const formatStation = (id) => { if (!isIBNR.test(id)) throw new Error('station ID must be an IBNR.') @@ -99,6 +316,7 @@ const dbProfile = { // todo: parseLocation parseJourney: createParseJourney, + parseHint, formatStation, diff --git a/parse/hint.js b/parse/hint.js index eebcad90..7dd6f045 100644 --- a/parse/hint.js +++ b/parse/hint.js @@ -1,7 +1,5 @@ 'use strict' -const trim = require('lodash/trim') - const hints = Object.assign(Object.create(null), { fb: { type: 'hint', @@ -218,36 +216,6 @@ const parseHint = (profile, h, icons) => { } } - // todo: find sth more reliable than this - if (h.type === 'P' && text.toLowerCase() === 'journey cancelled') { - return { - type: 'status', - code: 'journey-cancelled', - text - // todo: `h.sIdx` - } - } - if (h.type === 'U' && text.toLowerCase() === 'stop cancelled') { - return { - type: 'status', - code: 'stop-cancelled', // todo: change to stopover-cancelled - text - } - } - if (h.type === 'U' && trim(text.toLowerCase(), ' ()') === 'additional stop') { - return { - type: 'status', - code: 'additional-stopover', - text - } - } - if (h.type === 'G' && text.toLowerCase() === 'platform change') { - return { - type: 'status', - code: 'changed platform', - text - } - } if (h.type === 'L') { return { type: 'status', @@ -257,11 +225,11 @@ const parseHint = (profile, h, icons) => { } } if (h.type === 'A') { - const hint = hints[h.code && h.code.trim().toLowerCase()] - if (hint) { - return Object.assign({text: h.txtN}, hint) + return { + type: 'hint', + code: h.code || null, + text: h.txtN || null } - return null } if (h.type === 'D' || h.type === 'U' || h.type === 'R' || h.type === 'N') {