-
Notifications
You must be signed in to change notification settings - Fork 8
/
swatcher.py
executable file
·311 lines (241 loc) · 10.3 KB
/
swatcher.py
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
#!/usr/bin/python
import argparse
import time
import selenium
import datetime
import os
import swa
import configuration
DEFAULT_CONFIGURATION_FILE = "swatcher.ini"
class state(object):
def __init__(self):
self.errorCount = 0
self.currentLowestFare = None
self.blockQuery = False
self.firstQuery = True
self.notificationHistory = ''
self.dailyAlertDate = datetime.datetime.now().date()
class swatcher(object):
def __init__(self):
self.state = []
self.config = None
def now(self):
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def parseArguments(self):
parser = argparse.ArgumentParser(description = "swatcher.py: Utility to monitor SWA for fare price changes")
parser.add_argument('-f', '--file',
dest = 'configurationFile',
help = "Configuration file to use. If unspecified, will be '" + DEFAULT_CONFIGURATION_FILE + "'",
default = DEFAULT_CONFIGURATION_FILE)
args = parser.parse_args()
return args
def initializeHistory(self, index):
tripHistory = os.linesep + "Trip Details:"
ignoreKeys = ['index', 'description']
for key in self.config.trips[index].__dict__:
if(any(x in key for x in ignoreKeys)):
continue
tripHistory += os.linesep + " " + key + ": " + str(self.config.trips[index].__dict__[key])
if(self.config.historyFileBase):
try:
historyFileName = self.config.historyFileBase + "-" + str(index) + ".history"
with open(historyFileName) as historyFile:
for line in historyFile:
tripHistory = line + tripHistory
except IOError as e:
pass
return tripHistory
def appendHistoryFile(self, index, message):
if(self.config.historyFileBase):
try:
historyFileName = self.config.historyFileBase + "-" + str(index) + ".history"
with open(historyFileName, 'a') as historyFile:
historyFile.write(message + os.linesep)
except IOError as e:
pass
def sendNotification(self, index, message):
if(index is None):
return
subject = self.config.trips[index].description + ": " + message
print(self.now() + ": SENDING NOTIFICATION!!! '" + subject + "'")
if(not self.state[index].notificationHistory):
# If in here, this is the first notification, so add details to notification and see if history is enabled
self.state[index].notificationHistory = self.initializeHistory(index)
self.appendHistoryFile(index, self.now() + ": Monitoring started")
self.state[index].notificationHistory = self.now() + ": Monitoring started" + os.linesep + self.state[index].notificationHistory
shortMessage = self.now() + ": " + message
self.state[index].notificationHistory = shortMessage + os.linesep + self.state[index].notificationHistory
self.appendHistoryFile(index, shortMessage)
if(self.config.notification.type == 'smtp'):
try:
# importing this way keeps people who aren't interested in smtplib from installing it..
smtplib = __import__('smtplib')
if(self.config.notification.useAuth):
server = smtplib.SMTP(self.config.notification.host, self.config.notification.port)
server.ehlo()
server.starttls()
server.login(self.config.notification.username, self.config.notification.password)
else:
server = smtplib.SMTP(self.config.notification.host, self.config.notification.port)
mailMessage = """From: %s\nTo: %s\nX-Priority: 2\nSubject: %s\n\n""" % (self.config.notification.sender, self.config.notification.recipient, subject)
mailMessage += self.state[index].notificationHistory
server.sendmail(self.config.notification.sender, self.config.notification.recipient, mailMessage)
server.quit()
except Exception as e:
print(self.now() + ": UNABLE TO SEND NOTIFICATION DUE TO ERROR - " + str(e))
return
elif(self.config.notification.type == 'twilio'):
try:
# importing this way keeps people who aren't interested in Twilio from installing it..
twilio = __import__('twilio.rest')
client = twilio.rest.Client(self.config.notification.accountSid, self.config.notification.authToken)
client.messages.create(to = self.config.notification.recipient, from_ = self.config.notification.sender, body = subject)
except Exception as e:
print(self.now() + ": UNABLE TO SEND NOTIFICATION DUE TO ERROR - " + str(e))
return
def findLowestFareInSegment(self,trip, segment):
lowestCurrentFare = None
specificFlights = []
if(trip.specificFlights):
specificFlights = [x.strip() for x in trip.specificFlights.split(',')]
for flight in segment:
# If flight is sold-out or otherwise unavailable, no reason to process further
if(flight['fare'] is None):
continue
# Now, see if looking for specificFlights - if this is set, all other rules do not matter...
if(len(specificFlights) and (flight['flight'] not in specificFlights)):
continue
if(trip.maxStops < flight['stops']):
continue
if((trip.maxDuration > 0.0) and (trip.maxDuration < flight['duration'])):
continue
if(lowestCurrentFare is None):
lowestCurrentFare = flight['fare']
elif(flight['fare'] < lowestCurrentFare):
lowestCurrentFare = flight['fare']
return lowestCurrentFare
def processTrip(self, trip, driver):
if(self.state[trip.index].blockQuery):
return True;
print(self.now() + ": Querying flight '" + trip.description + "'");
try:
segments = swa.scrape(
driver = driver,
originationAirportCode = trip.originationAirportCode,
destinationAirportCode = trip.destinationAirportCode,
departureDate = trip.departureDate,
departureTimeOfDay = trip.departureTimeOfDay,
returnDate = trip.returnDate,
returnTimeOfDay = trip.returnTimeOfDay,
tripType = trip.type,
adultPassengersCount = trip.adultPassengersCount,
debug = self.config.debug
)
except swa.scrapeValidation as e:
print(e)
print("\nValidation errors are not retryable, so swatcher is exiting")
return False
except swa.scrapeDatesNotOpen as e:
if(self.state[trip.index].firstQuery):
self.sendNotification(trip.index, "Dates do not appear open")
self.state[trip.index].firstQuery = False
return True
except swa.scrapeDatePast as e:
self.sendNotification(trip.index, "Stopping trip monitoring as date has (or is about to) pass")
self.state[trip.index].blockQuery = True;
return True
except swa.scrapeTimeout as e:
# This could be a few things - internet or SWA website is down.
# it could also mean my WebDriverWait conditional is incorrect/changed. Don't know
# what to do about this, so for now, just print to screen and try again at next loop
print(self.now() + ": Timeout waiting for results, will retry next loop")
return True
except Exception as e:
print(e)
self.state[trip.index].errorCount += 1
if(self.state[trip.index].errorCount == 10):
self.state[trip.index].blockQuery = True;
self.sendNotification(trip.index, "Ceasing queries due to frequent errors")
return True
# If here, successfully scraped, so reset errorCount
self.state[trip.index].errorCount = 0
lowestFare = None
priceCount = 0
for segment in segments:
lowestSegmentFare = self.findLowestFareInSegment(trip, segment)
if(lowestSegmentFare is None):
break;
lowestFare = lowestSegmentFare if (lowestFare is None) else lowestFare + lowestSegmentFare
priceCount += 1
if(((lowestFare is not None) and (trip.maxPrice > 0) and (lowestFare > trip.maxPrice)) or (priceCount != len(segments))):
lowestFare = None
if(self.state[trip.index].firstQuery):
if(lowestFare is None):
self.sendNotification(trip.index, "Fare that meets criteria is UNAVAILABLE")
else:
self.sendNotification(trip.index, "Fare now $" + str(lowestFare))
self.state[trip.index].currentLowestFare = lowestFare
self.state[trip.index].firstQuery = False
elif(self.state[trip.index].currentLowestFare is None):
if(lowestFare is not None):
self.sendNotification(trip.index, "Fare now $" + str(lowestFare))
self.state[trip.index].currentLowestFare = lowestFare
else:
if(lowestFare is None):
self.sendNotification(trip.index, "Fare that meets criteria is UNAVAILABLE")
self.state[trip.index].currentLowestFare = None
elif(lowestFare != self.state[trip.index].currentLowestFare):
self.sendNotification(trip.index, "Fare now $" + str(lowestFare))
self.state[trip.index].currentLowestFare = lowestFare
if(self.config.dailyAlerts):
if(self.state[trip.index].dailyAlertDate != datetime.datetime.now().date()):
if(lowestFare is None):
self.sendNotification(trip.index, "Daily alert fare that meets criteria is UNAVAILABLE")
else:
self.sendNotification(trip.index, "Daily alert fare is $" + str(lowestFare))
self.state[trip.index].dailyAlertDate = datetime.datetime.now().date()
return True
def processTrips(self, driver):
for trip in self.config.trips:
if(not self.processTrip(trip, driver)):
return False
allBlocked = True
for state in self.state:
if(not state.blockQuery):
allBlocked = False
break
if(allBlocked):
print(self.now() + ": Stopping swatcher as there are no remaining trips to monitor")
return False
return True
def main(self):
args = self.parseArguments();
print(self.now() + ": Parsing configuration file '" + args.configurationFile +"'")
try:
self.config = configuration.configuration(args.configurationFile)
except Exception as e:
print("Error in processing configuration file: " + str(e))
quit()
self.state = [state() for i in xrange(len(self.config.trips))]
if(self.config.browser.type == 'chrome'): # Or Chromium
options = selenium.webdriver.ChromeOptions()
options.binary_location = self.config.browser.binaryLocation
options.add_argument('headless')
options.add_argument("log-level=" + str(self.config.browser.logLevel))
driver = selenium.webdriver.Chrome(chrome_options=options)
elif(self.config.browser.type == 'firefox'): # Or Iceweasel
options = selenium.webdriver.firefox.options.Options()
options.binary_location = self.config.browser.binaryLocation
options.add_argument('--headless')
driver = selenium.webdriver.Firefox(firefox_options = options)
else:
print("Unsupported web browser '" + browser.type + "' specified")
quit()
while True:
if(not self.processTrips(driver)):
break
time.sleep(self.config.pollInterval * 60)
return
if __name__ == "__main__":
swatcher = swatcher()
swatcher.main()