forked from gilby125/swa-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·183 lines (161 loc) · 5.82 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
#!/usr/bin/env node
"use strict"
const chalk = require("chalk")
const rainbow = require("chalk-rainbow")
const twilio = require("twilio")
const airports = require("airports")
const request = require("request")
const JSONStream = require('JSONStream')
const es = require('event-stream');
// 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: []
}
// Command line options
var originAirport
var destinationAirport
var outboundDateString
var returnDateString
var adultPassengerCount
var individualDealPrice
var totalDealPrice
var interval = 30 // In minutes
// 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 "--return-date":
returnDateString = argv[i + 1]
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
}
})
// 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
/**
* 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 (err) {
console.log([
chalk.red(`Error: failed to send SMS to ${process.env.TWILIO_PHONE_TO} from ${process.env.TWILIO_PHONE_FROM}`)
])
} else {
console.log([
chalk.green(`Successfully sent SMS to ${process.env.TWILIO_PHONE_TO} from ${process.env.TWILIO_PHONE_FROM}`)
])
}
})
} catch(e) {}
}
/**
* Fetch latest Ryanair prices
*
* @return {Void}
*/
const fetch = () => {
let roundTrip = returnDateString ? true : false
let reqURL = 'https://desktopapps.ryanair.com/en-ie/availability?ADT=' + adultPassengerCount + '&CHD=0&DateIn=' + returnDateString + '&DateOut=' + outboundDateString + '&Destination=' + destinationAirport + '&FlexDaysIn=0&FlexDaysOut=0&INF=0&Origin=' + originAirport + '&RoundTrip=' + roundTrip + '&TEEN=0&exists=false'
request({url: reqURL})
.pipe(JSONStream.parse())
.pipe(es.mapSync((data) => {
// TODO get lowest from list of fares
const lowestOutboundFare = data.trips[0].dates[0].flights[0].regularFare.fares[0].amount
const lowestReturnFare = data.trips[1].dates[0].flights[0].regularFare.fares[0].amount
var faresAreValid = true
// Get difference from previous fares
const outboundFareDiff = prevLowestOutboundFare - lowestOutboundFare
const returnFareDiff = prevLowestReturnFare - lowestReturnFare
var outboundFareDiffString = ""
var returnFareDiffString = ""
if (faresAreValid) {
// Store current fares for next time
prevLowestOutboundFare = lowestOutboundFare
prevLowestReturnFare = lowestReturnFare
// 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 \$${Math.abs(outboundFareDiff)})`)
} else if (outboundFareDiff < 0) {
outboundFareDiffString = chalk.red(`(up \$${Math.abs(outboundFareDiff)})`)
} else if (outboundFareDiff === 0) {
outboundFareDiffString = chalk.blue(`(no change)`)
}
if (returnFareDiff > 0) {
returnFareDiffString = chalk.green(`(down \$${Math.abs(returnFareDiff)})`)
} else if (returnFareDiff < 0) {
returnFareDiffString = chalk.red(`(up \$${Math.abs(returnFareDiff)})`)
} else if (returnFareDiff === 0) {
returnFareDiffString = chalk.blue(`(no change)`)
}
// 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 \$${lowestOutboundFare + lowestReturnFare}. Individual fares are \$${lowestOutboundFare} (outbound) and \$${lowestReturnFare} (return).`
console.log(message);
if (isTwilioConfigured) {
sendTextMessage(message)
}
}
[
`Lowest fares for an outbound flight is currently €${[lowestOutboundFare, outboundFareDiffString].filter(i => i).join(" ")}`,
`Lowest fares for a return flight is currently €${[lowestReturnFare, returnFareDiffString].filter(i => i).join(" ")}`
].forEach((price) => {
console.log(price)
})
console.log()
}
setTimeout(fetch, interval * TIME_MIN)
}
}))
}
fetch()