forked from gilby125/swa-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·564 lines (504 loc) · 13.8 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
#!/usr/bin/env node
"use strict"
const osmosis = require("osmosis")
const chalk = require("chalk")
const rainbow = require("chalk-rainbow")
const twilio = require("twilio")
const blessed = require("blessed")
const contrib = require("blessed-contrib")
const format = require("date-format")
const pretty = require("pretty-ms")
const airports = require("airports")
// Time constants
const TIME_MS = 1
const TIME_SEC = TIME_MS * 1000
const TIME_MIN = TIME_SEC * 60
const TIME_HOUR = TIME_MIN * 60
// Fares
var prevLowestOutboundFare
var prevLowestReturnFare
const fares = {
outbound: [],
return: []
}
// Flight times
const flightTimes = {
"anytime": "ANYTIME",
"morning": "BEFORE_NOON",
"afternoon": "NOON_TO_6PM",
"evening": "AFTER_6PM"
}
// Command line options
var originAirport
var destinationAirport
var outboundDateString
var outboundTimeOfDay = flightTimes["anytime"]
var returnDateString
var returnTimeOfDay = flightTimes["anytime"]
var adultPassengerCount
var individualDealPrice
var totalDealPrice
var interval = 30 // In minutes
var fareType = "DOLLARS"
var isOneWay = false
// Parse command line options (no validation, sorry!)
process.argv.forEach((arg, i, argv) => {
switch (arg) {
case "--from":
originAirport = argv[i + 1]
break
case "--to":
destinationAirport = argv[i + 1]
break
case "--leave-date":
outboundDateString = argv[i + 1]
break
case "--leave-time":
outboundTimeOfDay = (flightTimes[argv[i + 1 ]] === undefined) ? flightTimes["anytime"] : flightTimes[argv[i + 1 ]]
break
case "--return-date":
returnDateString = argv[i + 1]
break
case "--return-time":
returnTimeOfDay = (flightTimes[argv[i + 1 ]] === undefined) ? flightTimes["anytime"] : flightTimes[argv[i + 1 ]]
break
case "--fare-type":
fareType = argv[i + 1].toUpperCase()
break
case "--passengers":
adultPassengerCount = argv[i + 1]
break
case "--individual-deal-price":
individualDealPrice = parseInt(argv[i + 1])
break
case "--total-deal-price":
totalDealPrice = parseInt(argv[i + 1])
break
case "--interval":
interval = parseFloat(argv[i + 1])
break
case "--one-way":
isOneWay = true
break
}
})
// Remove invalid fields for a one-way flight
// Doing this after all flags are parsed in the event
// flags are out of order
if (isOneWay) {
returnDateString = ""
returnTimeOfDay = ""
totalDealPrice = undefined;
}
// Check if Twilio env vars are set
const isTwilioConfigured = process.env.TWILIO_ACCOUNT_SID &&
process.env.TWILIO_AUTH_TOKEN &&
process.env.TWILIO_PHONE_FROM &&
process.env.TWILIO_PHONE_TO
/**
* Dashboard renderer
*/
class Dashboard {
constructor() {
this.markers = []
this.widgets = {}
// Configure blessed
this.screen = blessed.screen({
title: "SWA Dashboard",
autoPadding: true,
dockBorders: true,
fullUnicode: true,
smartCSR: true
})
this.screen.key(["escape", "q", "C-c"], (ch, key) => process.exit(0))
// Grid settings
this.grid = new contrib.grid({
screen: this.screen,
rows: 12,
cols: 12
})
// Graphs
this.graphs = {
outbound: {
title: "Origin/Outbound",
x: [],
y: [],
style: {
line: "red"
}
},
}
// Graph return flight if one-way is not selected
if (!isOneWay) {
this.graphs.return = {
title: "Destination/Return",
x: [],
y: [],
style: {
line: "yellow"
}
}
}
// Shared settings
const shared = {
border: {
type: "line"
},
style: {
fg: "blue",
text: "blue",
border: {
fg: "green"
}
}
}
// Widgets
const widgets = {
map: {
type: contrib.map,
size: {
width: 9,
height: 5,
top: 0,
left: 0
},
options: Object.assign({}, shared, {
label: "Map",
startLon: 54,
endLon: 110,
startLat: 112,
endLat: 140,
region: "us"
})
},
settings: {
type: contrib.log,
size: {
width: 3,
height: 5,
top: 0,
left: 9
},
options: Object.assign({}, shared, {
label: "Settings",
padding: {
left: 1
}
})
},
graph: {
type: contrib.line,
size: {
width: 12,
height: 4,
top: 5,
left: 0
},
options: Object.assign({}, shared, {
label: "Prices",
showLegend: true,
legend: {
width: 20
}
})
},
log: {
type: contrib.log,
size: {
width: 12,
height: 3,
top: 9,
left: 0
},
options: Object.assign({}, shared, {
label: "Log",
padding: {
left: 1
}
})
}
}
// For each widget, create a new contrib widget object and replace
for (let name in widgets) {
let widget = widgets[name]
this.widgets[name] = this.grid.set(
widget.size.top,
widget.size.left,
widget.size.height,
widget.size.width,
widget.type,
widget.options
)
}
}
/**
* Render screen
*
* @return {Void}
*/
render() {
this.screen.render()
}
/**
* Plot graph data
*
* @param {Arr} prices
*
* @return {Void}
*/
plot(prices) {
const now = format("MM/dd/yy-hh:mm:ss", new Date())
const data = []
Object.assign(this.graphs.outbound, {
x: [...this.graphs.outbound.x, now],
y: [...this.graphs.outbound.y, prices.outbound]
})
data.push(this.graphs.outbound)
// Add data point if one-way is not selected
if (!isOneWay) {
Object.assign(this.graphs.return, {
x: [...this.graphs.return.x, now],
y: [...this.graphs.return.y, prices.return]
})
data.push(this.graphs.return)
}
this.widgets.graph.setData(data)
}
/**
* Add waypoint marker to map
*
* @param {Obj} data
*
* @return {Void}
*/
waypoint(data) {
this.markers.push(data)
if (this.blink) {
return
}
// Blink effect
var visible = true
this.blink = setInterval(() => {
if (visible) {
this.markers.forEach((m) => this.widgets.map.addMarker(m))
} else {
this.widgets.map.clearMarkers()
}
visible = !visible
this.render()
}, 1 * TIME_SEC)
}
/**
* Log data
*
* @param {Arr} messages
*
* @return {Void}
*/
log(messages) {
const now = format("MM/dd/yy-hh:mm:ss", new Date())
messages.forEach((m) => this.widgets.log.log(`${now}: ${m}`))
}
/**
* Display settings
*
* @param {Arr} config
*
* @return {Void}
*/
settings(config) {
// At this stage, this.widgets.settings is a contrib Log widget that has an `add(line)` function
config.forEach((c) => this.widgets.settings.add(c))
}
}
const dashboard = new Dashboard()
/**
* Send a text message using Twilio
*
* @param {Str} message
*
* @return {Void}
*/
const sendTextMessage = (message) => {
try {
const twilioClient = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN)
twilioClient.sendMessage({
from: process.env.TWILIO_PHONE_FROM,
to: process.env.TWILIO_PHONE_TO,
body: message
}, function(err, data) {
if (!dashboard) return
if (err) {
dashboard.log([
chalk.red(`Error: failed to send SMS to ${process.env.TWILIO_PHONE_TO} from ${process.env.TWILIO_PHONE_FROM}`)
])
} else {
dashboard.log([
chalk.green(`Successfully sent SMS to ${process.env.TWILIO_PHONE_TO} from ${process.env.TWILIO_PHONE_FROM}`)
])
}
})
} catch(e) {}
}
/**
* Format fare type price
*
* @param {Int} price
*
* @return {Str}
*/
const formatPrice = (price) => {
if (fareType === 'POINTS') {
return `${price} pts`
} else {
return `\$${price}`
}
}
/**
* Parse and return pricing from HTML markup
*
* @param {Str} priceMarkup
*
* @return {Int}
*/
const parsePriceMarkup = (priceMarkup) => {
if (fareType === 'POINTS') {
const matches = priceMarkup.text().split(',').join('')
return parseInt(matches)
} else {
const matches = priceMarkup.toString().match(/\$.*?(\d+)/)
return parseInt(matches[1])
}
}
/**
* Fetch latest Southwest prices
*
* @return {Void}
*/
const fetch = () => {
const formData = {
}
osmosis
.get("https://www.southwest.com")
.submit(".booking-form--form", {
twoWayTrip: !isOneWay,
airTranRedirect: "",
returnAirport: isOneWay ? "" : "RoundTrip",
outboundTimeOfDay,
returnTimeOfDay,
seniorPassengerCount: 0,
fareType,
originAirport,
destinationAirport,
outboundDateString,
returnDateString,
adultPassengerCount
})
.find("#faresOutbound .product_price, #b0Table span.var.h5")
.then((priceMarkup) => {
const price = parsePriceMarkup(priceMarkup)
fares.outbound.push(price)
})
.find("#faresReturn .product_price, #b1Table span.var.h5")
.then((priceMarkup) => {
if (isOneWay) return // Only record return prices if it's a two-way flight
const price = parsePriceMarkup(priceMarkup)
fares.return.push(price)
})
.done(() => {
const lowestOutboundFare = Math.min(...fares.outbound)
const lowestReturnFare = Math.min(...fares.return)
var faresAreValid = true
// Clear previous fares
fares.outbound = []
fares.return = []
// Get difference from previous fares
const outboundFareDiff = prevLowestOutboundFare - lowestOutboundFare
const returnFareDiff = prevLowestReturnFare - lowestReturnFare
var outboundFareDiffString = ""
var returnFareDiffString = ""
// Create a string to show the difference
if (!isNaN(outboundFareDiff) && !isNaN(returnFareDiff)) {
// Usually this is because of a scraping error
if (!isFinite(outboundFareDiff) || !isFinite(returnFareDiff)) {
faresAreValid = false
}
if (outboundFareDiff > 0) {
outboundFareDiffString = chalk.green(`(down ${formatPrice(Math.abs(outboundFareDiff))})`)
} else if (outboundFareDiff < 0) {
outboundFareDiffString = chalk.red(`(up ${formatPrice(Math.abs(outboundFareDiff))})`)
} else if (outboundFareDiff === 0) {
outboundFareDiffString = chalk.blue(`(no change)`)
}
if (returnFareDiff > 0) {
returnFareDiffString = chalk.green(`(down ${formatPrice(Math.abs(returnFareDiff))})`)
} else if (returnFareDiff < 0) {
returnFareDiffString = chalk.red(`(up ${formatPrice(Math.abs(returnFareDiff))})`)
} else if (returnFareDiff === 0) {
returnFareDiffString = chalk.blue(`(no change)`)
}
}
if (faresAreValid) {
// Store current fares for next time
prevLowestOutboundFare = lowestOutboundFare
prevLowestReturnFare = lowestReturnFare
// Do some Twilio magic (SMS alerts for awesome deals)
const awesomeDealIsAwesome = (
totalDealPrice && (lowestOutboundFare + lowestReturnFare <= totalDealPrice)
) || (
individualDealPrice && (lowestOutboundFare <= individualDealPrice || lowestReturnFare <= individualDealPrice)
)
if (awesomeDealIsAwesome) {
const message = `Deal alert! Combined total has hit ${formatPrice(lowestOutboundFare + lowestReturnFare)}. Individual fares are ${formatPrice(lowestOutboundFare)} (outbound) and ${formatPrice(lowestReturnFare)} (return).`
// Party time
dashboard.log([
rainbow(message)
])
if (isTwilioConfigured) {
sendTextMessage(message)
}
}
dashboard.log([
`Lowest fares for an outbound flight is currently ${formatPrice([lowestOutboundFare, outboundFareDiffString].filter(i => i).join(" "))}`,
])
if (!isOneWay) {
dashboard.log([
`Lowest fares for a return flight is currently ${formatPrice([lowestReturnFare, returnFareDiffString].filter(i => i).join(" "))}`
])
}
dashboard.plot({
outbound: lowestOutboundFare,
return: lowestReturnFare
})
}
dashboard.render()
setTimeout(fetch, interval * TIME_MIN)
})
}
// Get lat/lon for airports (no validation on non-existent airports)
airports.forEach((airport) => {
switch (airport.iata) {
case originAirport:
dashboard.waypoint({ lat: airport.lat, lon: airport.lon, color: "red", char: "X" })
break
case destinationAirport:
dashboard.waypoint({ lat: airport.lat, lon: airport.lon, color: "yellow", char: "X" })
break
}
})
dashboard.settings([
`Origin airport: ${originAirport}`,
`Destination airport: ${destinationAirport}`,
`Outbound date: ${outboundDateString}`,
`Outbound time: ${outboundTimeOfDay.toLowerCase()}`,
!isOneWay && `Return date: ${returnDateString}`,
!isOneWay && `Return time: ${returnTimeOfDay.toLowerCase()}`,
`Trip type: ${isOneWay ? "one-way" : "two-way"}`,
`Fare type: ${fareType.toLowerCase()}`,
`Passengers: ${adultPassengerCount}`,
`Interval: ${pretty(interval * TIME_MIN)}`,
!isOneWay && `Individual deal price: ${individualDealPrice ? `<= ${formatPrice(individualDealPrice)}` : "disabled"}`,
`Total deal price: ${totalDealPrice ? `<= ${formatPrice(totalDealPrice)}` : "disabled"}`,
`SMS alerts: ${isTwilioConfigured ? process.env.TWILIO_PHONE_TO : "disabled"}`
].filter(s => s))
fetch()