forked from polastre/united
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunited.js
executable file
·214 lines (200 loc) · 7.07 KB
/
united.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
#!/usr/bin/env node
const puppeteer = require('puppeteer')
const colors = require('colors')
const args = process.argv.slice(2)
const _ = require('lodash')
const fs = require('fs')
/**
* Convert the date into a string format that can be used in requests
*/
function getDateString(date, separator = '/') {
var d = ('0' + date.getDate()).slice(-2)
var m = ('0' + (date.getMonth() + 1)).slice(-2)
var y = date.getFullYear()
return m + separator + d + separator + y
}
/**
* Parse the resulting data from United
*/
function parseResults(data) {
let upgrades = []
let flights = data.data['Trips'][0]['Flights']
if (flights == undefined) {
return []
}
for (let i = 0; i < flights.length; i++) {
let upgradeAvailable = false
let products = flights[i]['Products']
let productIndex = 1
if (products[3]) productIndex = 3 // use 3 on Premium Economy routes
if (products[productIndex]['InstrumentFlightBlockUpgrade'] &&
products[productIndex]['InstrumentFlightBlockUpgrade']['Available'] === true &&
products[productIndex]['InstrumentFlightBlockUpgrade']['Waitlisted'] === false) {
flights[i]['Upgrade'] = true
if (flights[i]['TravelMinutes'] >= config.minTime) upgradeAvailable = true
}
if (flights[i]['Connections'] != null) {
for (let x = 0; x < flights[i]['Connections'].length; x++) {
if (flights[i]['Connections'][x]['Products'][productIndex]['InstrumentFlightBlockUpgrade'] &&
flights[i]['Connections'][x]['Products'][productIndex]['InstrumentFlightBlockUpgrade']['Available'] === true &&
flights[i]['Connections'][x]['Products'][productIndex]['InstrumentFlightBlockUpgrade']['Waitlisted'] === false) {
flights[i]['Connections'][x]['Upgrade'] = true;
if (flights[i]['Connections'][x]['TravelMinutes'] >= config.minTime) upgradeAvailable = true
}
}
}
if (upgradeAvailable === true) upgrades.push(flights[i])
}
return upgrades
}
/**
* Pretty print the results so that they are human readable
*/
function printResults(result) {
console.log('==============================')
if (result.upgrades.length > 0) {
console.log(getDateString(result.date).green.bold)
} else {
console.log(getDateString(result.date).red.bold)
}
let templateFile = fs.readFileSync('united_template.ejs')
let template = _.template(templateFile)
for (let i = 0; i < result.upgrades.length; i++) {
console.log(template({ data: result.upgrades[i] }))
}
}
async function processDate(date) {
let haveNonstop = false
let haveLayovers = false
let retrievalTimeout = 120
let retries = 0
const browser = await puppeteer.launch()
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const page = await browser.newPage()
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36')
let results = null
let xhrRequests = 0
let ready = false
await page.goto('https://www.united.com/ual/en/us/flight-search/book-a-flight', {waitUntil: 'networkidle0'})
page.on('response', async msg => {
if (msg.request().resourceType == 'xhr') {
xhrRequests++
if (msg.request().url.startsWith('https://www.united.com/ual/en/us/default/autocomplete/affinityseach')) {
ready = true
}
if (msg.request().url == 'https://www.united.com/ual/en/us/flight-search/book-a-flight/flightshopping/getflightresults/rev') {
try {
let data = await msg.json()
if (results == null) results = parseResults(data)
else results = results.concat(parseResults(data))
// Try until we get both nonstop and layover, or until 15 seconds past the last result.
// We can't tell if we're going to get two or one sometimes.
if (data.data['SearchFilters']['HideNoneStop'] === false) {
haveNonstop = true
retrievalTimeout = retries + 30
}
if (data.data['SearchFilters']['HideLayover'] === false) {
haveLayovers = true
retrievalTimeout = retries + 30
}
}
catch (error) {}
}
}
})
await page.evaluate(() => {
const ow = document.querySelector("#TripTypes_ow")
ow.click()
document.querySelector("#Trips_0__NonStop").click()
document.querySelector("#Trips_0__OneStop").click()
document.querySelector("#Trips_0__TwoPlusStop").click()
})
await page.click('#TripTypes_ow')
await timeout(100)
await page.evaluate(function() {
document.querySelector('#Trips_0__Origin').value = ''
document.querySelector('#Trips_0__Destination').value = ''
document.querySelector('#Trips_0__DepartDate').value = ''
})
await timeout(100)
await page.type('#Trips_0__Origin', config.origin, {delay: 100})
await timeout(100)
await page.click('#fare-preference')
await timeout(100)
await page.type('#Trips_0__Destination', config.destination, {delay: 100})
await timeout(100)
await page.click('#fare-preference')
await timeout(100)
await page.type('#Trips_0__DepartDate', getDateString(date), {delay: 100})
await timeout(100)
await page.click('#fare-preference')
await timeout(100)
await page.select('#select-upgrade-type', 'MUA')
await timeout(100)
await page.click('#fare-preference')
await timeout(100)
await page.focus('#ClassofService')
await timeout(100)
// check if date is valid
let valid = await page.$eval('#Trips_0__DepartDate', el => el.getAttribute('aria-invalid'))
if (valid == 'true') {
console.log('INVALID DATE')
return []
}
while ((xhrRequests < 3) || (ready === false)) {
await timeout(500)
}
await timeout(1000)
await page.evaluate(() => {
const btn = document.querySelector("#btn-search")
btn.click()
})
while ((results === null || haveNonstop === false || haveLayovers === false) && retries < retrievalTimeout) {
process.stdout.write('.')
await timeout(500)
retries++
}
if (retries == 120) {
console.log('TIMEOUT! ', getDateString(date))
}
await page.close()
await browser.close()
if (results === null) {
return []
}
// await timeout(2000)
return results
}
// set data based on args
if (args.length < 4) {
console.log('Not enough arguments. Format: [ORG] [DST] [FRM] [TO] [OPTIONAL:minimum flight length]')
process.exit(1)
}
const config = {
origin: args[0],
destination: args[1],
start: new Date(args[2]),
end: new Date(args[3]),
minTime: Number(args[4] > 0) ? Number(args[4]) : 0
}
async function runDates() {
let dates = []
let promises = []
for (let d = new Date(config.start); d <= config.end; d.setDate(d.getDate() + 1)) {
let newDate = new Date(d)
dates.push(newDate)
promises.push(processDate(newDate))
}
Promise.all(promises).then(results => {
console.log('')
for (let i = 0; i < results.length; i++) {
printResults({
date: dates[i],
upgrades: results[i]
})
}
})
}
runDates()