Skip to content

Commit

Permalink
journeys: remove collection of results
Browse files Browse the repository at this point in the history
Reverts 7fd574f.
See also #23 (comment) .
  • Loading branch information
derhuerst committed Jul 19, 2020
1 parent 5c05375 commit 51f4a66
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 69 deletions.
111 changes: 45 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const createClient = (profile, userAgent, opt = {}) => {
return _stationBoard(station, 'ARR', profile.parseArrival, opt)
}

const journeys = (from, to, opt = {}) => {
const journeys = async (from, to, opt = {}) => {
from = profile.formatLocation(profile, from, 'from')
to = profile.formatLocation(profile, to, 'to')

Expand Down Expand Up @@ -173,75 +173,54 @@ const createClient = (profile, userAgent, opt = {}) => {
})
}

// 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.
// todo: revert this change, see https://github.com/public-transport/hafas-client/issues/76#issuecomment-424448449
const journeys = []
let earlierRef = null, laterRef = null
const more = (when, journeysRef) => {
const query = {
getPasslist: !!opt.stopovers,
maxChg: opt.transfers,
minChgTime: opt.transferTime,
depLocL: [from],
viaLocL: opt.via ? [{loc: opt.via}] : null,
arrLocL: [to],
jnyFltrL: filters,
gisFltrL,
getTariff: !!opt.tickets,
// 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?
getPolyline: !!opt.polylines
// todo: `getConGroups: false` what is this?
// todo: what is getEco, fwrd?
}
if (journeysRef) query.ctxScr = journeysRef
else {
query.outDate = profile.formatDate(profile, when)
query.outTime = profile.formatTime(profile, when)
}
if (profile.journeysNumF && opt.results !== null) query.numF = opt.results
if (profile.journeysOutFrwd) query.outFrwd = outFrwd

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 []
// todo: outConGrpL

const ctx = {profile, opt, common, res}

if (!earlierRef) earlierRef = res.outCtxScrB
const query = {
getPasslist: !!opt.stopovers,
maxChg: opt.transfers,
minChgTime: opt.transferTime,
depLocL: [from],
viaLocL: opt.via ? [{loc: opt.via}] : null,
arrLocL: [to],
jnyFltrL: filters,
gisFltrL,
getTariff: !!opt.tickets,
// 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?
getPolyline: !!opt.polylines
// todo: `getConGroups: false` what is this?
// todo: what is getEco, fwrd?
}
if (journeysRef) query.ctxScr = journeysRef
else {
query.outDate = profile.formatDate(profile, when)
query.outTime = profile.formatTime(profile, when)
}
if (profile.journeysNumF && opt.results !== null) query.numF = opt.results
if (profile.journeysOutFrwd) query.outFrwd = outFrwd

const {
res, common,
} = await profile.request({profile, opt}, userAgent, {
cfg: {polyEnc: 'GPA'},
meth: 'TripSearch',
req: profile.transformJourneysQuery({profile, opt}, query)
})

let latestDep = -Infinity
for (const rawJourney of res.outConL) {
const journey = profile.parseJourney(ctx, rawJourney)
journeys.push(journey)
if (!Array.isArray(res.outConL)) return []
// todo: outConGrpL

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
if (dep > latestDep) latestDep = dep
}
const ctx = {profile, opt, common, res}
const journeys = res.outConL
.map(j => profile.parseJourney(ctx, j))

if (opt.results === null) return {earlierRef, laterRef, journeys}
const when = new Date(latestDep)
return more(when, res.outCtxScrF) // otherwise continue
})
return {
earlierRef: res.outCtxScrB,
laterRef: res.outCtxScrF,
journeys,
}

return more(when, journeysRef)
}

const refreshJourney = (refreshToken, opt = {}) => {
Expand Down
1 change: 0 additions & 1 deletion lib/default-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const defaultProfile = {
formatRectangle,
filters,

journeysNumF: true, // `journeys()` method: support for `numF` field?
journeysOutFrwd: true, // `journeys()` method: support for `outFrwd` field?
departuresGetPasslist: true, // `departures()` method: support for `getPasslist` field?
departuresStbFltrEquiv: true, // `departures()` method: support for `stbFltrEquiv` field?
Expand Down
1 change: 0 additions & 1 deletion p/oebb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const oebbProfile = {
parseLocation: parseHook(_parseLocation, fixWeirdPOIs),
parseMovement: parseHook(_parseMovement, fixMovement),

journeysNumF: false,
trip: true,
radar: true,
reachableFrom: true
Expand Down
1 change: 0 additions & 1 deletion p/vbb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ const vbbProfile = {

formatStation,

journeysNumF: true,
journeysWalkingSpeed: true,
trip: true,
radar: true,
Expand Down

0 comments on commit 51f4a66

Please sign in to comment.