db-vendo-client/index.js

455 lines
15 KiB
JavaScript
Raw Normal View History

2016-06-22 01:39:04 +02:00
'use strict'
2017-11-20 15:43:13 +01:00
const minBy = require('lodash/minBy')
const maxBy = require('lodash/maxBy')
2018-03-18 00:13:44 +01:00
const isObj = require('lodash/isObject')
2018-08-25 01:52:46 +02:00
const sortBy = require('lodash/sortBy')
2018-09-03 20:38:56 +02:00
const pRetry = require('p-retry')
2017-11-20 15:43:13 +01:00
const defaultProfile = require('./lib/default-profile')
const validateProfile = require('./lib/validate-profile')
2016-06-22 02:09:02 +02:00
const isNonEmptyString = str => 'string' === typeof str && str.length > 0
2018-02-28 16:09:23 +01:00
2018-08-25 01:52:46 +02:00
const validateLocation = (loc, name = 'location') => {
if (!isObj(loc)) {
2019-02-13 17:55:57 +01:00
throw new TypeError(name + ' must be an object.')
2018-08-25 01:52:46 +02:00
} else if (loc.type !== 'location') {
2019-02-13 17:55:57 +01:00
throw new TypeError('invalid location object.')
2018-08-25 01:52:46 +02:00
} else if ('number' !== typeof loc.latitude) {
2019-02-13 17:55:57 +01:00
throw new TypeError(name + '.latitude must be a number.')
2018-08-25 01:52:46 +02:00
} else if ('number' !== typeof loc.longitude) {
2019-02-13 17:55:57 +01:00
throw new TypeError(name + '.longitude must be a number.')
2018-08-25 01:52:46 +02:00
}
}
const createClient = (profile, userAgent, opt = {}) => {
2017-11-11 22:49:04 +01:00
profile = Object.assign({}, defaultProfile, profile)
2017-12-12 03:21:12 +01:00
validateProfile(profile)
2017-10-03 17:36:42 +02:00
2018-07-19 21:50:20 +02:00
if ('string' !== typeof userAgent) {
2019-02-13 17:55:57 +01:00
throw new TypeError('userAgent must be a string');
2018-07-19 21:50:20 +02:00
}
const _stationBoard = (station, type, parse, opt = {}) => {
2018-02-28 16:09:23 +01:00
if (isObj(station)) station = profile.formatStation(station.id)
else if ('string' === typeof station) station = profile.formatStation(station)
2019-02-13 17:55:57 +01:00
else throw new TypeError('station must be an object or a string.')
2017-11-12 18:06:16 +01:00
2018-06-26 15:49:50 +02:00
if ('string' !== typeof type || !type) {
2019-02-13 17:55:57 +01:00
throw new TypeError('type must be a non-empty string.')
2018-06-26 15:49:50 +02:00
}
if (!profile.departuresGetPasslist && ('stopovers' in opt)) {
throw new Error('opt.stopovers is not supported by this endpoint')
}
if (!profile.departuresStbFltrEquiv && ('includeRelatedStations' in opt)) {
throw new Error('opt.includeRelatedStations is not supported by this endpoint')
}
2017-11-12 18:06:16 +01:00
opt = Object.assign({
2019-10-28 17:42:33 +01:00
// todo: for arrivals(), this is actually a station it *has already* stopped by
direction: null, // only show departures stopping by this station
duration: 10, // show departures for the next n minutes
linesOfStops: false, // parse & expose lines at the stop/station?
remarks: true, // parse & expose hints & warnings?
stopovers: false, // fetch & parse previous/next stopovers?
// departures at related stations
// e.g. those that belong together on the metro map.
includeRelatedStations: true
2017-11-12 18:06:16 +01:00
}, opt)
2018-05-21 17:10:42 +02:00
opt.when = new Date(opt.when || Date.now())
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid')
2019-10-31 20:08:56 +01:00
const req = profile.formatStationBoardReq({profile, opt}, station, type)
return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => {
if (!Array.isArray(res.jnyL)) return []
const ctx = {profile, opt, common, res}
return res.jnyL.map(res => parse(ctx, res))
.sort((a, b) => new Date(a.when) - new Date(b.when)) // todo
2017-11-12 18:06:16 +01:00
})
}
2018-06-26 17:11:25 +02:00
const departures = (station, opt = {}) => {
return _stationBoard(station, 'DEP', profile.parseDeparture, opt)
}
const arrivals = (station, opt = {}) => {
return _stationBoard(station, 'ARR', profile.parseArrival, opt)
}
2017-11-12 20:02:32 +01:00
const journeys = (from, to, opt = {}) => {
from = profile.formatLocation(profile, from, 'from')
to = profile.formatLocation(profile, to, 'to')
2017-11-12 20:02:32 +01:00
if (('earlierThan' in opt) && ('laterThan' in opt)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('opt.earlierThan and opt.laterThan are mutually exclusive.')
}
if (('departure' in opt) && ('arrival' in opt)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('opt.departure and opt.arrival are mutually exclusive.')
}
let journeysRef = null
if ('earlierThan' in opt) {
if (!isNonEmptyString(opt.earlierThan)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('opt.earlierThan must be a non-empty string.')
}
if (('departure' in opt) || ('arrival' in opt)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('opt.earlierThan and opt.departure/opt.arrival are mutually exclusive.')
}
journeysRef = opt.earlierThan
}
if ('laterThan' in opt) {
if (!isNonEmptyString(opt.laterThan)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('opt.laterThan must be a non-empty string.')
}
if (('departure' in opt) || ('arrival' in opt)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('opt.laterThan and opt.departure/opt.arrival are mutually exclusive.')
}
journeysRef = opt.laterThan
}
2017-11-12 20:02:32 +01:00
opt = Object.assign({
results: null, // number of journeys  `null` means "whatever HAFAS returns"
2017-11-12 20:02:32 +01:00
via: null, // let journeys pass this station?
stopovers: false, // return stations on the way?
transfers: -1, // maximum of 5 transfers
2017-11-12 20:02:32 +01:00
transferTime: 0, // minimum time for a single transfer in minutes
// todo: does this work with every endpoint?
accessibility: 'none', // 'none', 'partial' or 'complete'
bike: false, // only bike-friendly journeys
tickets: false, // return tickets?
polylines: false, // return leg shapes?
2018-06-28 13:45:56 +02:00
remarks: true, // parse & expose hints & warnings?
2018-10-25 16:11:19 +02:00
walkingSpeed: 'normal', // 'slow', 'normal', 'fast'
// Consider walking to nearby stations at the beginning of a journey?
2018-07-23 20:42:22 +02:00
startWithWalking: true,
scheduledDays: false
2017-11-12 20:02:32 +01:00
}, opt)
if (opt.via) opt.via = profile.formatLocation(profile, opt.via, 'opt.via')
if (opt.when !== undefined) {
throw new Error('opt.when is not supported anymore. Use opt.departure/opt.arrival.')
}
let when = new Date(), outFrwd = true
if (opt.departure !== undefined && opt.departure !== null) {
when = new Date(opt.departure)
2019-02-13 17:55:57 +01:00
if (Number.isNaN(+when)) throw new TypeError('opt.departure is invalid')
} else if (opt.arrival !== undefined && opt.arrival !== null) {
2019-12-29 22:16:45 +01:00
if (!profile.journeysOutFrwd) {
throw new Error('opt.arrival is unsupported')
}
when = new Date(opt.arrival)
2019-02-13 17:55:57 +01:00
if (Number.isNaN(+when)) throw new TypeError('opt.arrival is invalid')
outFrwd = false
}
const filters = [
profile.formatProductsFilter({profile}, opt.products || {})
]
if (
opt.accessibility &&
profile.filters &&
profile.filters.accessibility &&
profile.filters.accessibility[opt.accessibility]
) {
filters.push(profile.filters.accessibility[opt.accessibility])
}
2017-11-12 20:02:32 +01:00
2018-10-25 16:11:19 +02:00
if (!['slow','normal','fast'].includes(opt.walkingSpeed)) {
throw new Error('opt.walkingSpeed must be one of these values: "slow", "normal", "fast".')
}
const gisFltrL = []
if (profile.journeysWalkingSpeed) {
gisFltrL.push({
meta: 'foot_speed_' + opt.walkingSpeed,
mode: 'FB',
type: 'M'
})
}
2018-10-25 16:11:19 +02:00
2018-09-25 20:19:06 +02:00
// With protocol version `1.16`, the VBB endpoint *used to* fail with
// `CGI_READ_FAILED` if you pass `numF`, the parameter for the number
// of results. To circumvent this, we loop here, collecting journeys
// until we have enough.
2018-09-25 20:19:06 +02:00
// todo: revert this change, see https://github.com/public-transport/hafas-client/issues/76#issuecomment-424448449
const journeys = []
let earlierRef, laterRef
const more = (when, journeysRef) => {
const query = {
2018-03-05 00:51:11 +01:00
outDate: profile.formatDate(profile, when),
outTime: profile.formatTime(profile, when),
ctxScr: journeysRef,
getPasslist: !!opt.stopovers,
maxChg: opt.transfers,
minChgTime: opt.transferTime,
depLocL: [from],
viaLocL: opt.via ? [{loc: opt.via}] : null,
arrLocL: [to],
jnyFltrL: filters,
2018-10-25 16:11:19 +02:00
gisFltrL,
getTariff: !!opt.tickets,
2019-10-28 17:42:33 +01:00
// todo: this is actually "take additional stations nearby the given start and destination station into account"
// see rest.exe docs
ushrp: !!opt.startWithWalking,
getPT: true, // todo: what is this?
getIV: false, // todo: walk & bike as alternatives?
2018-04-30 12:49:33 +02:00
getPolyline: !!opt.polylines
2018-10-28 16:36:49 +01:00
// todo: `getConGroups: false` what is this?
}
if (profile.journeysNumF && opt.results !== null) query.numF = opt.results
2019-12-29 22:16:45 +01:00
if (profile.journeysOutFrwd) query.outFrwd = outFrwd
2019-10-31 18:48:11 +01:00
return profile.request({profile, opt}, userAgent, {
cfg: {polyEnc: 'GPA'},
meth: 'TripSearch',
req: profile.transformJourneysQuery({profile, opt}, query)
})
.then(({res, common}) => {
if (!Array.isArray(res.outConL)) return []
2019-08-23 15:36:57 +02:00
// todo: outConGrpL
2018-04-30 12:49:58 +02:00
const ctx = {profile, opt, common, res}
2018-04-30 12:49:58 +02:00
if (!earlierRef) earlierRef = res.outCtxScrB
2018-03-05 00:51:11 +01:00
let latestDep = -Infinity
for (const rawJourney of res.outConL) {
const journey = profile.parseJourney(ctx, rawJourney)
journeys.push(journey)
2018-03-05 00:51:11 +01:00
if (opt.results !== null && journeys.length >= opt.results) { // collected enough
laterRef = res.outCtxScrF
return {earlierRef, laterRef, journeys}
}
const dep = +new Date(journey.legs[0].departure) // todo
2018-03-05 00:51:11 +01:00
if (dep > latestDep) latestDep = dep
}
2018-03-05 00:51:11 +01:00
if (opt.results === null) return {earlierRef, laterRef, journeys}
2018-03-05 00:51:11 +01:00
const when = new Date(latestDep)
return more(when, res.outCtxScrF) // otherwise continue
})
}
return more(when, journeysRef)
2017-11-12 20:02:32 +01:00
}
2018-07-24 17:37:36 +02:00
const refreshJourney = (refreshToken, opt = {}) => {
if ('string' !== typeof refreshToken || !refreshToken) {
2019-02-13 17:55:57 +01:00
new TypeError('refreshToken must be a non-empty string.')
2018-07-24 17:37:36 +02:00
}
opt = Object.assign({
stopovers: false, // return stations on the way?
tickets: false, // return tickets?
polylines: false, // return leg shapes?
remarks: true // parse & expose hints & warnings?
}, opt)
const req = profile.formatRefreshJourneyReq({profile, opt}, refreshToken)
return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => {
if (!Array.isArray(res.outConL) || !res.outConL[0]) {
2018-07-24 17:37:36 +02:00
throw new Error('invalid response')
}
const ctx = {profile, opt, common, res}
return profile.parseJourney(ctx, res.outConL[0])
2018-07-24 17:37:36 +02:00
})
2017-11-12 20:02:32 +01:00
}
2017-11-12 20:19:33 +01:00
const locations = (query, opt = {}) => {
if (!isNonEmptyString(query)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('query must be a non-empty string.')
2018-02-28 16:09:23 +01:00
}
2017-11-12 20:19:33 +01:00
opt = Object.assign({
fuzzy: true, // find only exact matches?
results: 5, // how many search results?
stops: true, // return stops/stations?
2017-11-12 20:19:33 +01:00
addresses: true,
poi: true, // points of interest
linesOfStops: false // parse & expose lines at each stop/station?
2017-11-12 20:19:33 +01:00
}, opt)
2019-10-31 20:08:56 +01:00
const req = profile.formatLocationsReq({profile, opt}, query)
return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => {
if (!res.match || !Array.isArray(res.match.locL)) return []
const ctx = {profile, opt, common, res}
return res.match.locL.map(loc => profile.parseLocation(ctx, loc))
2017-11-12 20:19:33 +01:00
})
}
2018-11-21 19:54:40 +01:00
const stop = (stop, opt = {}) => {
if ('object' === typeof stop) stop = profile.formatStation(stop.id)
else if ('string' === typeof stop) stop = profile.formatStation(stop)
2019-02-13 17:55:57 +01:00
else throw new TypeError('stop must be an object or a string.')
2018-01-26 17:08:07 +01:00
opt = Object.assign({
linesOfStops: false // parse & expose lines at the stop/station?
}, opt)
2019-10-31 20:08:56 +01:00
const req = profile.formatStopReq({profile, opt}, stop)
return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => {
if (!res || !Array.isArray(res.locL) || !res.locL[0]) {
2018-01-26 17:08:07 +01:00
// todo: proper stack trace?
throw new Error('invalid response')
}
const ctx = {profile, opt, res, common}
return profile.parseLocation(ctx, res.locL[0])
2018-01-26 17:08:07 +01:00
})
}
2018-01-05 14:53:03 +01:00
const nearby = (location, opt = {}) => {
2018-08-25 01:52:46 +02:00
validateLocation(location, 'location')
2018-01-05 14:53:03 +01:00
2017-11-12 20:29:57 +01:00
opt = Object.assign({
results: 8, // maximum number of results
distance: null, // maximum walking distance in meters
poi: false, // return points of interest?
stops: true, // return stops/stations?
linesOfStops: false // parse & expose lines at each stop/station?
2017-11-12 20:29:57 +01:00
}, opt)
2019-10-31 20:08:56 +01:00
const req = profile.formatNearbyReq({profile, opt}, location)
return profile.request({profile, opt}, userAgent, req)
.then(({common, res}) => {
if (!Array.isArray(res.locL)) return []
const ctx = {profile, opt, common, res}
return res.locL.map(loc => profile.parseNearby(ctx, loc))
2017-11-12 20:29:57 +01:00
})
}
const trip = (id, lineName, opt = {}) => {
if (!isNonEmptyString(id)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('id must be a non-empty string.')
2018-02-28 16:09:23 +01:00
}
if (!isNonEmptyString(lineName)) {
2019-02-13 17:55:57 +01:00
throw new TypeError('lineName must be a non-empty string.')
2018-02-28 16:09:23 +01:00
}
2017-12-17 20:33:04 +01:00
opt = Object.assign({
stopovers: true, // return stations on the way?
polyline: false, // return a track shape?
2018-06-28 13:45:56 +02:00
remarks: true // parse & expose hints & warnings?
2017-12-17 20:33:04 +01:00
}, opt)
2017-11-20 15:43:13 +01:00
2019-10-31 20:08:56 +01:00
const req = profile.formatTripReq({profile, opt}, id, lineName)
return profile.request({profile, opt}, userAgent, req)
.then(({common, res}) => {
const ctx = {profile, opt, common, res}
2017-11-20 15:43:13 +01:00
2018-11-21 23:42:13 +01:00
const rawLeg = { // pretend the leg is contained in a journey
2017-11-20 15:43:13 +01:00
type: 'JNY',
dep: minBy(res.journey.stopL, 'idx'),
arr: maxBy(res.journey.stopL, 'idx'),
jny: res.journey
2017-11-20 15:43:13 +01:00
}
const trip = profile.parseJourneyLeg(ctx, rawLeg, res.journey.date)
2018-11-21 23:42:13 +01:00
trip.id = trip.tripId
delete trip.tripId
return trip
2017-11-20 15:43:13 +01:00
})
}
2018-03-17 20:58:03 +01:00
const radar = ({north, west, south, east}, opt) => {
2019-02-13 17:55:57 +01:00
if ('number' !== typeof north) throw new TypeError('north must be a number.')
if ('number' !== typeof west) throw new TypeError('west must be a number.')
if ('number' !== typeof south) throw new TypeError('south must be a number.')
if ('number' !== typeof east) throw new TypeError('east must be a number.')
if (north <= south) throw new Error('north must be larger than south.')
if (east <= west) throw new Error('east must be larger than west.')
2017-11-20 17:37:08 +01:00
opt = Object.assign({
results: 256, // maximum number of vehicles
duration: 30, // compute frames for the next n seconds
// todo: what happens with `frames: 0`?
2017-12-12 03:28:54 +01:00
frames: 3, // nr of frames to compute
products: null, // optionally an object of booleans
polylines: true // return a track shape for each vehicle?
2017-11-20 17:37:08 +01:00
}, opt || {})
2018-05-21 17:10:42 +02:00
opt.when = new Date(opt.when || Date.now())
2019-02-13 17:55:57 +01:00
if (Number.isNaN(+opt.when)) throw new TypeError('opt.when is invalid')
2017-11-20 17:37:08 +01:00
2019-10-31 20:08:56 +01:00
const req = profile.formatRadarReq({profile, opt}, north, west, south, east)
return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => {
if (!Array.isArray(res.jnyL)) return []
2017-11-20 17:37:08 +01:00
const ctx = {profile, opt, common, res}
return res.jnyL.map(m => profile.parseMovement(ctx, m))
2017-11-20 17:37:08 +01:00
})
}
const reachableFrom = (address, opt = {}) => {
validateLocation(address, 'address')
2018-08-25 01:52:46 +02:00
opt = Object.assign({
when: Date.now(),
maxTransfers: 5, // maximum of 5 transfers
maxDuration: 20, // maximum travel duration in minutes, pass `null` for infinite
2018-08-25 01:52:46 +02:00
products: {}
}, opt)
2019-02-13 17:55:57 +01:00
if (Number.isNaN(+opt.when)) throw new TypeError('opt.when is invalid')
2018-08-25 01:52:46 +02:00
2019-10-31 20:08:56 +01:00
const req = profile.formatReachableFromReq({profile, opt}, address)
2018-09-03 20:38:56 +02:00
const refetch = () => {
2019-10-31 20:08:56 +01:00
return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => {
if (!Array.isArray(res.posL)) {
2018-09-03 20:38:56 +02:00
const err = new Error('invalid response')
err.shouldRetry = true
throw err
}
const byDuration = []
let i = 0, lastDuration = NaN
for (const pos of sortBy(res.posL, 'dur')) {
const loc = common.locations[pos.locX]
2018-09-03 20:38:56 +02:00
if (!loc) continue
if (pos.dur !== lastDuration) {
lastDuration = pos.dur
i = byDuration.length
byDuration.push({
duration: pos.dur,
stations: [loc]
})
} else {
byDuration[i].stations.push(loc)
}
}
return byDuration
})
}
return pRetry(refetch, {
retries: 3,
factor: 2,
minTimeout: 2 * 1000
2018-08-25 01:52:46 +02:00
})
}
2018-11-21 19:54:40 +01:00
const client = {departures, arrivals, journeys, locations, stop, nearby}
if (profile.trip) client.trip = trip
if (profile.radar) client.radar = radar
if (profile.refreshJourney) client.refreshJourney = refreshJourney
2018-08-25 01:52:46 +02:00
if (profile.reachableFrom) client.reachableFrom = reachableFrom
2017-12-16 07:54:39 +01:00
Object.defineProperty(client, 'profile', {value: profile})
return client
2016-06-22 01:39:59 +02:00
}
module.exports = createClient