-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex-inquirer.js
211 lines (188 loc) · 5.33 KB
/
index-inquirer.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
const chalk = require('chalk');
const utils = require('./utils/utils');
const Browser = require('./browser/browser');
const SkyscannerScraper = require('./skyscanner/skyscanner');
const inquirer = require('inquirer');
/*language
currency*/
const questions = [
{
type: 'confirm',
name: 'directOnly',
message: 'Direct flights only?',
default: false
},
{
type: 'confirm',
name: 'oneWay',
message: "Is a one way trip?",
default: false
},
{
type: 'input',
name: 'origin',
message: 'Type a city/airport/IATA code of depart:',
validate: function (value) {
let valid = (value !== '' && value !== undefined);
return valid || 'Please type a city/airport/IATA Code';
}
},
{
type: 'input',
name: 'destination',
message: 'Type a city/airport/IATA code of destination:',
default: 'Everywhere',
validate: function (value) {
let valid = (value !== '' && value !== undefined);
return valid || 'Please type a city/airport/IATA Code';
}
},
{
type: 'input',
name: 'yearStart',
message: 'If different, type the depart year:',
default: new Date().getFullYear()
},
{
type: 'list',
name: 'monthStart',
message: 'Select a month of depart:',
choices: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
},
{
type: 'confirm',
name: 'wholeMonthStart',
message: 'Would you like to list the prices for the whole month?',
default: false
},
{
type: 'input',
name: 'dayStart',
message: 'Type a day of depart:',
validate: function (value) {
var valid = !isNaN(parseInt(value));
return valid || 'Please enter a number';
},
when: function (answers) {
return answers.wholeMonthStart === false;
},
filter: Number
},
{
type: 'input',
name: 'yearEnd',
message: 'If different, type the return year:',
default: new Date().getFullYear(),
when: function (answers) {
return answers.oneWay === false;
}
},
{
type: 'list',
name: 'monthEnd',
message: 'Select a month of return:',
choices: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
default: function (answers) {
return answers.monthStart;
},
when: function (answers) {
return answers.oneWay === false;
}
},
{
type: 'confirm',
name: 'wholeMonthEnd',
message: 'Would you like to list the prices for the whole month?',
default: false,
when: function (answers) {
return answers.wholeMonthStart === true && answers.oneWay === false;
}
},
{
type: 'input',
name: 'dayEnd',
message: 'Type a day of return:',
validate: function (value) {
var valid = !isNaN(parseInt(value));
return valid || 'Please enter a number';
},
when: function (answers) {
return answers.oneWay === false && answers.wholeMonthStart === false;
},
filter: Number
}
];
var args = null;
inquirer.prompt(questions).then(answers => {
const defaultArgs = utils.getInputParameters();
args = answers;
args['ua'] = defaultArgs['ua'];
args['wholeMonthEnd'] = args['wholeMonthEnd'] || false;
if (Object.keys(args).indexOf('h') !== -1) {
utils.showHelp();
return false;
}
utils.validateInputArguments(args);
(async () => {
try{
const browser = new Browser(args);
await browser.init();
const skyscannerScraperInstance = new SkyscannerScraper();
skyscannerScraperInstance.attachBrowser(browser);
await skyscannerScraperInstance.init({
fakingUserInteraction: true,
ua: args['ua'],
'intercept-request': true
});
console.log(chalk.bgCyan('Faking user interaction..'));
await utils.fakingUserInteraction(skyscannerScraperInstance.page);
if (args['username'] !== undefined && args['password'] !== undefined) {
await skyscannerScraperInstance.signIn(args['username'], args['password']);
}
// await skyscannerScraperInstance.page.click('#fsc-trip-type-selector-return'); // enabled by default
console.log(chalk.yellow("Is oneWay: " + chalk.underline.bold(args['oneWay'])));
if (args['oneWay'] === true)
await skyscannerScraperInstance.setOneWay();
console.log(chalk.yellow("Is directOnly: " + chalk.underline.bold(args['directOnly'])));
if (args['directOnly'] === true)
await skyscannerScraperInstance.setDirectOnly();
await skyscannerScraperInstance.setOriginAirport(args['origin']);
if (args['destination'] != 'Everywhere') {
await skyscannerScraperInstance.setDestinationAirport(args['destination']);
}
await skyscannerScraperInstance.setDepartureDate(
args['wholeMonthStart'],
args['dayStart'],
args['monthStart'],
args['yearStart']
);
if (args['oneWay'] !== true) {
await skyscannerScraperInstance.setReturnDate(
args['wholeMonthEnd'],
args['dayEnd'],
args['monthEnd'],
args['yearEnd']
);
}
if(args['adults'] !== undefined || args['children'] !== undefined)
await skyscannerScraperInstance.setPassengersData(args['adults'], args['children']);
await skyscannerScraperInstance.submitSearch();
/*
// in case of G recaptcha
await utils.clickOnRecaptcha(page);
*/
if(await skyscannerScraperInstance.loadResultPage()) {
console.log(await skyscannerScraperInstance.page.url());
console.log('Wait for the results..');
var pageParser = await skyscannerScraperInstance.createPageParser();
var results = await pageParser.getData(skyscannerScraperInstance);
console.log(results);
}
console.log('Window close');
await browser.close();
} catch(e) {
console.log(e);
return false;
}
})();
});