@@ -245,7 +245,79 @@ const createClient = (profile, userAgent, opt = {}) => {
245
245
}
246
246
}
247
247
248
- const refreshJourney = async ( refreshToken , opt = { } ) => {
248
+ const bestPrices = async ( from , to , opt = { } ) => {
249
+ from = profile . formatLocation ( profile , from , 'from' )
250
+ to = profile . formatLocation ( profile , to , 'to' )
251
+
252
+ opt = Object . assign ( {
253
+ via : null , // let journeys pass this station?
254
+ transfers : - 1 , // maximum nr of transfers
255
+ bike : false , // only bike-friendly journeys
256
+ tickets : false , // return tickets?
257
+ polylines : false , // return leg shapes?
258
+ subStops : false , // parse & expose sub-stops of stations?
259
+ entrances : false , // parse & expose entrances of stops/stations?
260
+ remarks : true , // parse & expose hints & warnings?
261
+ scheduledDays : false , // parse & expose dates each journey is valid on?
262
+ } , opt )
263
+ if ( opt . via ) opt . via = profile . formatLocation ( profile , opt . via , 'opt.via' )
264
+
265
+ let when = new Date ( ) ;
266
+ if ( opt . departure !== undefined && opt . departure !== null ) {
267
+ when = new Date ( opt . departure )
268
+ if ( Number . isNaN ( + when ) ) throw new TypeError ( 'opt.departure is invalid' )
269
+ const now = new Date ( ) ;
270
+ const today = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) )
271
+ if ( today > when ) {
272
+ throw new TypeError ( 'opt.departure date older than current date.' )
273
+ }
274
+ }
275
+
276
+ const filters = [
277
+ profile . formatProductsFilter ( { profile} , opt . products || { } )
278
+ ]
279
+ if (
280
+ opt . accessibility &&
281
+ profile . filters &&
282
+ profile . filters . accessibility &&
283
+ profile . filters . accessibility [ opt . accessibility ]
284
+ ) {
285
+ filters . push ( profile . filters . accessibility [ opt . accessibility ] )
286
+ }
287
+
288
+ const query = {
289
+ maxChg : opt . transfers ,
290
+ depLocL : [ from ] ,
291
+ viaLocL : opt . via ? [ { loc : opt . via } ] : [ ] ,
292
+ arrLocL : [ to ] ,
293
+ jnyFltrL : filters ,
294
+ getTariff : ! ! opt . tickets ,
295
+
296
+ getPolyline : ! ! opt . polylines
297
+ }
298
+ query . outDate = profile . formatDate ( profile , when )
299
+
300
+ const { res, common} = await profile . request ( { profile, opt} , userAgent , {
301
+ cfg : { polyEnc : 'GPA' } ,
302
+ meth : 'BestPriceSearch' ,
303
+ req : profile . transformJourneysQuery ( { profile, opt} , query )
304
+ } )
305
+ if ( ! Array . isArray ( res . outConL ) ) return { }
306
+ // todo: outConGrpL
307
+
308
+ const ctx = { profile, opt, common, res}
309
+ const journeys = res . outConL . map ( j => profile . parseJourney ( ctx , j ) )
310
+ const bestPrices = res . outDaySegL . map ( j => profile . parseBestPrice ( ctx , j , journeys ) )
311
+
312
+ return {
313
+ bestPrices,
314
+ realtimeDataUpdatedAt : res . planrtTS && res . planrtTS !== '0'
315
+ ? parseInt ( res . planrtTS )
316
+ : null ,
317
+ }
318
+ }
319
+
320
+ const refreshJourney = async ( refreshToken , opt = { } ) => {
249
321
if ( 'string' !== typeof refreshToken || ! refreshToken ) {
250
322
throw new TypeError ( 'refreshToken must be a non-empty string.' )
251
323
}
@@ -791,6 +863,7 @@ const createClient = (profile, userAgent, opt = {}) => {
791
863
nearby,
792
864
serverInfo,
793
865
}
866
+ if ( profile . bestPrices ) client . bestPrices = bestPrices
794
867
if ( profile . trip ) client . trip = trip
795
868
if ( profile . radar ) client . radar = radar
796
869
if ( profile . refreshJourney ) client . refreshJourney = refreshJourney
0 commit comments