-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·642 lines (584 loc) · 17.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/usr/bin/env node
'use strict';
const trainline = require('./trainline.js');
const program = require('commander');
const storage = require('node-persist');
const colors = require('colors');
const moment = require('moment');
const Table = require('cli-table2');
const fuzzy = require('fuzzy');
const Spinner = require('cli-spinner').Spinner;
const inquirer = require('inquirer');
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
// Connected user infos
var uinfos;
// storage configuration
storage.init({
stringify: JSON.stringify,
parse: JSON.parse,
encoding: 'utf8',
ttl: false
}).then(() => {
storage.getItem('uinfos').then(infos => {
uinfos = infos;
loadUserInfos();
main();
});
});
program
.version('1.0.0')
.option('-l, --login [email]', 'Log in to your Trainline account')
.option('-L, --logout', 'Logout of your Trainline account')
.option('-s, --search', 'Search for a trip')
.option('-t, --trips', 'List of your trips')
.option('-a, --buy', 'Buy trips in your basket')
.option('-b, --basket', 'List of your options')
.parse(process.argv);
function menu() {
inquirer.prompt([
{
type: 'list',
name: 'menu',
message: 'What do you want to do:',
choices: [
{ name: 'Search for a trip', value: searchForTrips },
{ name: 'Pay for a trip in my basket', value: buyFromBasket },
{ name: 'Consult my booked trips', value: consultBookedTrips },
{ name: 'Logout', value: logout },
{ name: 'Exit', value: () => { return Promise.reject(); } },
],
pageSize: 20
}
]).then(choice => {
let selection = choice.menu;
return selection();
}).then(() => {
if (uinfos) {
return menu();
}
}).catch(() => {});
}
let firstTime = true;
function main() {
let firstTimeo = firstTime;
firstTime = false;
// Login
if (program.login) {
if (uinfos) {
displayError('You are already connected.');
return;
}
login(program.login);
return;
}
// The following actions need a user to be connected
if (!uinfos) {
if (firstTimeo) {
displayInfo('First you need to login to your Trainline account:');
}
return login().then(main);
}
// Logout
if (program.logout) {
logout();
}
if (program.trips) {
consultBookedTrips();
}
if (program.basket) {
trainline.basket().then(trips => {
console.log(tripsToTable(trips));
});
}
if (program.buy) {
buyFromBasket();
}
if (program.search) {
searchForTrips().then(menu);
}
// If no specific action specified, display the menu
menu();
}
function loadUserInfos() {
if (uinfos && uinfos.meta && uinfos.meta.token) {
trainline.TOKEN = uinfos.meta.token;
trainline.USER_ID = uinfos.user.id;
displayInfo('Welcome ' + uinfos.user.first_name + ' ' + uinfos.user.last_name + '!');
}
}
function login(email) {
let questions = [];
if (!email) {
questions.push({
type: 'input',
name: 'email',
message: 'Email address:'
});
}
questions.push({
type: 'password',
name: 'password',
message: 'Trainline password:'
});
return inquirer.prompt(questions).then(answers => {
let login = email || answers.email;
return trainline.connexion(login, answers.password);
}).then(infos => {
uinfos = infos;
return storage.setItem('uinfos', infos);
}).then(() => {
loadUserInfos();
}).catch(err => {
displayError('Wrong password or wrong email address.');
});
}
function logout() {
return storage.removeItem('uinfos').then(() => {
uinfos = null;
displayInfo('You are now disconnected.');
});
}
/**
* Consult booked trips
*/
function consultBookedTrips() {
let spinner = startSpinner();
return trainline.trips().then(trips => {
spinner.stop(true);
console.log(tripsToTable(trips.slice(0, 7)));
});
}
/**
* Interactive session for searching for train tickets
*/
function searchForTrips() {
let dates = getNextDays(90);
let spinner;
return inquirer.prompt([
{
type: 'autocomplete',
name: 'from',
suggestOnly: false,
message: 'From:',
source: searchStation,
pageSize: 5
},
{
type: 'autocomplete',
name: 'to',
suggestOnly: false,
message: 'To:',
source: searchStation,
pageSize: 5
},
{
type: 'autocomplete',
name: 'departure_date',
suggestOnly: false,
message: 'Departure date:',
source: (answers, input) => {
return Promise.resolve(fuzzy.filter(input || '', dates).map(e => { return e.string }));
},
pageSize: 5
},
{
type: 'list',
name: 'hour',
message: 'Time:',
choices: ['14h', '16h', '18h', '20h', '22h', '6h', '8h', '10h', '12h']
},
{
type: 'checkbox',
name: 'passengers',
message: 'Passengers:',
choices: uinfos.passengers.map(passenger => {
return {
checked: passenger.is_selected,
name: passenger.first_name + ' ' + passenger.last_name,
value: {
id: passenger.id,
card_ids: passenger.card_ids
}
}
}).sort((a, b) => {
return a.checked;
})
}
]).then(answers => {
spinner = startSpinner();
// We need to find the ids of the selected stations
let sq1 = trainline.searchStation(answers.from);
let sq2 = trainline.searchStation(answers.to);
return Promise.all([answers, sq1, sq2]);
}).then(queries => {
let answers = queries[0];
let departure_station_id = queries[1][0].id;
let arrival_station_id = queries[2][0].id;
let departure_date = moment(colors.strip(answers.departure_date) + ' ' + answers.hour, 'dddd, MMMM D H[h]').format();
let passengers = answers.passengers;
let passenger_ids = passengers.map(p => { return p.id });
let card_ids = passengers.reduce((acc, p) => { return acc.concat(p.card_ids) }, []);
return trainline.searchTrips(departure_station_id, arrival_station_id, passenger_ids, card_ids, departure_date);
}).then(trips => {
trips = humanifyTrips(trips);
let choices = [];
choices.push(new inquirer.Separator());
trips.forEach(trip => {
let table = compactTable({ colWidths: [20, 60] });
let duration = colors.white(formatDuration(moment(trip.arrival_date) - moment(trip.departure_date)));
let departure_time = colors.green(moment(trip.departure_date).format('HH:mm'));
let arrival_time = colors.green(moment(trip.arrival_date).format('HH:mm'));
let price = trip.travel_classes.economy.cents/100;
if (trip.travel_classes.first) {
price += ' / ' + trip.travel_classes.first.cents/100;
}
price += ' ' + trip.travel_classes.economy.currency;
table.push([duration, departure_time + ' ' + colors.bold(trip.departure_station)]);
trip.stops.forEach(stop => {
table.push([' ', ' ' + colors.magenta(formatDuration(stop.duration) + ' ' + stop.station)]);
});
table.push([' ' + price, ' ' + arrival_time + ' ' + colors.bold(trip.arrival_station)]);
choices.push({
name: table.toString(),
value: trip.travel_classes,
short: trip.departure_station + ' ' + colors.green(departure_time) + ' > ' + colors.green(arrival_time) + ' ' + trip.arrival_station
});
choices.push(new inquirer.Separator());
});
spinner.stop(true);
return inquirer.prompt([
{
type: 'list',
name: 'trip',
message: 'Available trips:',
choices: choices,
pageSize: 20
}
]);
}).then(answers => {
let travel_classes = answers.trip;
if (Object.keys(travel_classes).length > 1) {
return inquirer.prompt([
{
type: 'list',
name: 'tobook',
message: 'Travel class:',
choices: [
{
name: 'Economy: ' + travel_classes.economy.cents/100 + ' ' + travel_classes.economy.currency,
value: travel_classes.economy.tobook
},
{
name: 'First: ' + travel_classes.first.cents/100 + ' ' + travel_classes.first.currency,
value: travel_classes.first.tobook
}
]
}
])
} else {
return Promise.resolve({tobook: travel_classes[Object.keys(travel_classes)[0]].tobook});
}
}).then(trip => {
spinner = startSpinner();
return trainline.bookTrip(trip.tobook.search_id, trip.tobook.folder_id);
}).then(result => {
spinner.stop(true);
displaySuccess('Your trip has been added to your basket!');
});
}
/**
* Interactive session from selecting trips to buy in the basket,
* to paying for them
*/
function buyFromBasket() {
let finalPnrs, trips;
let spinner = startSpinner();
return trainline.basket().then(tripso => {
trips = tripso;
let choices = [];
choices.push(new inquirer.Separator());
tripsToArrayOfTables(trips, true, ' ').forEach(trip => {
choices.push({
name: trip.forDisplay,
checked: trip.details.is_selected,
value: trip.details,
short: trip.short
});
choices.push(new inquirer.Separator());
});
spinner.stop(true);
return inquirer.prompt([
{
type: 'checkbox',
name: 'pnrs',
message: 'Select your trips:',
choices: choices,
pageSize: 20
}
])
}).then(answers => {
spinner = startSpinner();
finalPnrs = answers.pnrs;
// Now we will select the right pnrs and unselect the other
let pnrsToChange = [];
let selectedPnrs = answers.pnrs.map(pnr => { return pnr.pnr_id });
trips.forEach(trip => {
let isSelected = (selectedPnrs.indexOf(trip.pnr_id) > -1);
if ((trip.is_selected && !isSelected) || (!trip.is_selected && isSelected)) {
pnrsToChange.push({
pnr_id: trip.pnr_id,
is_selected: isSelected
});
}
});
let pnrsq = new Promise((resolve, reject) => {
let i = 0;
// Trainline seems not to accept changing too fast multiple pnrs
// So I do it sequentially
function selectNextPnr() {
if (i >= pnrsToChange.length) {
return resolve();
}
let pnr = pnrsToChange[i];
i++;
trainline.selectPnr(pnr.pnr_id, pnr.is_selected).then(selectNextPnr);
}
selectNextPnr();
});
return Promise.all([trainline.paymentCards(), pnrsq]);
}).then(qs => {
let payment_cards = qs[0].payment_cards.map(c => {
return {
name: c.label + ' (' + c.type + ' xxxx xxxx xxxx ' + c.last_digits + ')',
value: c.id
}
});
spinner.stop(true);
return inquirer.prompt([
{
type: 'list',
name: 'card',
message: 'Credit card:',
choices: payment_cards,
pageSize: 4
},
{
type: 'password',
name: 'cvv',
message: 'CVV:'
},
{
type: 'confirm',
name: 'confirm',
message: 'Do you confirm the payment of ' + finalPnrs.length + ' ticket' + s(finalPnrs.length) + ' for ' + finalPnrs.reduce((acc, pnr) => { return acc + pnr.cents }, 0)/100 + ' EUR?'
}
]);
}).then(answers => {
if (!answers.confirm) {
displayError('Aborting.');
return Promise.reject();
}
if (!answers.cvv || answers.cvv.length != 3) {
displayError('The CVV must have three characters.');
return Promise.reject();
}
spinner = startSpinner();
return trainline.payForPnrs(answers.card, answers.cvv, finalPnrs);
}).then(payment => {
spinner.stop(true);
if (payment.payment.status != 'success') {
displayError('An error occurred. Please check your card and CVV.');
return;
}
let m = 'The payment was successful, your ';
if (finalPnrs.length > 1) {
m += 'trips have';
} else {
m += 'trip has';
}
m += 'been booked! You should receive an email in a minute.';
displaySuccess(m);
}).catch((err) => {});
}
/**
* Adapt a list of trips from a search
* for an easy display. Compute the list of stops from the list of segments.
* @param {trips} array({})
* @return array({})
*/
function humanifyTrips(trips) {
trips.forEach(trip => {
trip.stops = [];
for (let i = 1; i < trip.segments.length; i++) {
let segment = trip.segments[i];
let psegment = trip.segments[i-1];
let stop = {
station: segment.departure_station,
train_name: segment.train_name,
duration: (moment(segment.departure_date) - moment(psegment.arrival_date))
};
trip.stops.push(stop);
}
});
return trips;
};
/**
* Format for a human the duration in seconds
* @param {duration} number The duration in ms
* @return string
*/
function formatDuration(duration) {
function fillz(n) {
if (n < 10) {
return '0' + n;
}
return n;
}
duration = duration/1000;
let o = '';
if ((duration % 3600) != 0) {
o = Math.ceil((duration%3600)/60);
}
if (duration >= 3600) {
o = Math.floor(duration/3600) + 'h' + fillz(o);
} else {
o += ' min';
}
return o;
}
/**
* Return the next `limit` days, to a human format
* @param {limit} number The number of days to return
* @return array(string)
*/
function getNextDays(limit) {
let dates = [];
let currentDate = moment();
for (let i = 0; i < limit; i++) {
let d = currentDate.format('dddd, MMMM D');
if (currentDate.isoWeekday() >= 6) {
d = colors.green(d);
}
if (i <= 1) {
d = colors.bold(d);
}
dates.push(d);
currentDate.add(1, 'days');
}
return dates;
}
/**
* Search for a station
* If no query, return the most popular stations of the user
* @param {answers} array The previous answers
* @param {input} string The query
* @return Promise([string])
*/
function searchStation(answers, input) {
return (function() {
if (input) {
return trainline.searchStation(input);
}
return Promise.resolve(uinfos.stations);
}()).then(stations => {
return stations.map(s => s.name);
});
}
/**
* Create an array ready to be displayed from the trips
* @param {trips} array List of trips
* @param {hideRef} boolean Hide the reference of the trip (useful for basket, where trips don't have references yet)
* @param {offset} string String which will be added at the beginning of new lines (useful for Inquirer)
* @return array({details, short, forDisplay})
*/
function tripsToArrayOfTables(trips, hideRef, offset) {
offset = offset || '';
let nl = '\n' + offset;
let o = [];
// We will create a complete table with cli-table2
// so everything will be correctly aligned
// And then we will split it
let table = compactTable({ chars: {'right-mid': ' '}});
trips.reverse();
trips.forEach(trip => {
let t = [];
if (!hideRef) {
let reference = trip.reference;
t.push(reference);
}
let departure_date = colors.green(moment(trip.departure_date).format('ddd, MMM D YYYY HH:mm'));
let arrival_date = colors.green(moment(trip.arrival_date).format('ddd, MMM D YYYY HH:mm'));
let date = departure_date;
if (departure_date != arrival_date) {
date += nl + arrival_date;
}
let stations = colors.bold(trip.departure_station.name + nl + trip.arrival_station.name);
let passenger = trip.passenger.first_name;
let price = {hAlign: 'right', content: colors.yellow(trip.cents/100 + ' ' + trip.currency)};
t = t.concat([date, stations, passenger, price]);
o.push({
details: trip,
short: trip.departure_station.name + ' > ' + trip.arrival_station.name + ' (' + passenger + ' - ' + price.content + ')'
});
table.push(t);
});
let stringTrips = table.toString().split(/\n\u001b\[90m \u001b\[39m\n/g);
stringTrips.forEach((s, i) => {
o[i].forDisplay = s;
});
return o;
}
/**
* Create a table for display from an array of trips
* @param {trips} array List of trips
* @return string The table to display
*/
function tripsToTable(trips) {
return tripsToArrayOfTables(trips).map(trip => { return trip.forDisplay }).join('\n');
}
/**
* Return a new Table object with custom chars, so it is more compact than default
* @param {params} objet The other parameters to consider
* @return Table
*/
function compactTable(params) {
params = params || {};
params.chars = params.chars || {};
let config = {
chars: {
'top': '', 'top-mid': '', 'top-left': '', 'top-right': '',
'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': '',
'left': '', 'left-mid': '', 'mid': '', 'mid-mid': '',
'right': '' , 'right-mid': '' , 'middle': ' '
},
style: { 'padding-left': 0, 'padding-right': 0 }
};
Object.assign(config.chars, params.chars);
delete params.chars;
Object.assign(config, params);
return new Table(config);
}
/**
* Helpers to display messages
*/
function displayInfo(message) {
console.log(colors.blue(colors.bold('i ') + message));
}
function displaySuccess(message) {
console.log(colors.yellow(colors.bold('✓ ') + message));
}
function displayError(message) {
console.log(colors.red(colors.bold('x ') + message));
}
function s(n) {
if (n >= 2) {
return 's';
}
return '';
}
function startSpinner() {
let spinner = new Spinner('%s');
spinner.setSpinnerString(0);
spinner.start();
return spinner;
}