diff --git a/docs/journeys.md b/docs/journeys.md index 4afdaa04..84615f81 100644 --- a/docs/journeys.md +++ b/docs/journeys.md @@ -69,7 +69,8 @@ With `opt`, you can override the default options, which look like this: remarks: true, // parse & expose hints & warnings? // Consider walking to nearby stations at the beginning of a journey? startWithWalking: true, - language: 'en' // language to get results in + language: 'en', // language to get results in + scheduledDays: false // parse which days each journey is valid on } ``` @@ -300,3 +301,18 @@ departure of first (later) journey 2017-12-17T19:19:00.000+01:00 ``` If you pass `polylines: true`, each journey leg will have a `polyline` field. Refer to [the section in the `trip()` docs](trip.md#polyline-option) for details. + +If you pass `scheduledDays: true`, each journey will have a `scheduledDays` field looking like this: + +```js +{ + '2018-01-01': true, + '2018-01-02': false, + // … + '2018-10-12': true, + '2018-10-13': true, + // … + '2019-01-02': false, + '2019-01-03': false +} +``` diff --git a/index.js b/index.js index c1dbb27b..66535db4 100644 --- a/index.js +++ b/index.js @@ -140,7 +140,8 @@ const createClient = (profile, userAgent, request = _request) => { polylines: false, // return leg shapes? remarks: true, // parse & expose hints & warnings? // Consider walking to nearby stations at the beginning of a journey? - startWithWalking: true + startWithWalking: true, + scheduledDays: false }, opt) if (opt.via) opt.via = profile.formatLocation(profile, opt.via, 'opt.via') diff --git a/parse/journey-leg.js b/parse/journey-leg.js index 3c08cfc5..9225f0fe 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -43,7 +43,6 @@ const createParseJourneyLeg = (profile, opt, data) => { const {locations, lines, hints, warnings, polylines} = data // todo: pt.status, pt.isPartCncl // todo: pt.isRchbl, pt.chRatingRT, pt.chgDurR, pt.minChg - // todo: pt.sDays // todo: pt.dep.dProgType, pt.arr.dProgType // todo: what is pt.jny.dirFlg? diff --git a/parse/journey.js b/parse/journey.js index 0ebe1001..3e88b7a6 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -1,12 +1,31 @@ 'use strict' +const {DateTime} = require('luxon') const findRemark = require('./find-remark') +const parseScheduledDays = (sDaysB, profile) => { + sDaysB = Buffer.from(sDaysB, 'hex') + const res = Object.create(null) + + let d = DateTime.fromObject({ + zone: profile.timezone, locale: profile.locale, + year: new Date().getFullYear(), + month: 1, day: 1, + hour: 0, minute: 0, second: 0, millisecond: 0 + }) + for (let b = 0; b < sDaysB.length; b++) { + for (let i = 0; i < 8; i++) { + res[d.toISODate()] = (sDaysB[b] & Math.pow(2, 7 - i)) > 0 + d = d.plus({days: 1}) + } + } + return res +} + const createParseJourney = (profile, opt, data) => { const parseLeg = profile.parseJourneyLeg(profile, opt, data) const {hints, warnings} = data - // todo: c.sDays // todo: c.conSubscr // todo: c.trfRes x vbb-parse-ticket // todo: c.sotRating, c.isSotCon, c.sotCtxt @@ -32,6 +51,10 @@ const createParseJourney = (profile, opt, data) => { } } + if (opt.scheduledDays) { + res.scheduledDays = parseScheduledDays(j.sDays.sDaysB, profile) + } + return res }