-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
666 lines (555 loc) · 23 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
var BASE_URL = '/api/search.php';
var HOST = 'skiplagged.com';
var fs = require('fs');
var log = 'flights_log.txt';
var connection = null;
var transporter = null;
var flight_info = {};
var email_info = {};
module.exports = Flight;
function Flight(data) {
data.CONFIG = config = data.CONFIG || {};
data.RESULTS = data.RESULTS || 1;
data.SORT = data.SORT || 'cost';
data.RETURN_DATE = data.RETURN_DATE || '';
data.SKIP_HIDDEN_CITY = 'SKIP_HIDDEN_CITY' in data ? data.SKIP_HIDDEN_CITY : true;
data.SAVE_TO_DATABASE = 'SAVE_TO_DATABASE' in data ? data.SAVE_TO_DATABASE : false;
data.FLIGHT_TIME = data.FLIGHT_TIME || 0;
data.BEFORE_OR_AFTER = data.BEFORE_OR_AFTER || null;
var flightUrl = BASE_URL;
flightUrl += '?from=' +
data.FROM +
'&to=' +
data.TO +
'&depart=' +
data.DEPARTURE_DATE +
'&return' +
data.RETURN_DATE +
'&sort=' +
data.SORT;
this.flightUrl = flight_info.flightUrl = flightUrl;
this.return_count = flight_info.return_count = data.RESULTS;
this.departure = flight_info.departure = data.FROM;
this.destination = flight_info.destination = data.TO;
this.skip_hidden_city = flight_info.skip_hidden_city = data.SKIP_HIDDEN_CITY;
this.departure_date = flight_info.departure_date = data.DEPARTURE_DATE;
this.flight_time = flight_info.flight_time = data.FLIGHT_TIME;
this.before_or_after = flight_info.before_or_after = data.BEFORE_OR_AFTER;
if(data.SAVE_TO_DATABASE === true) {
data.MIN_PERCENT_CHANGE = data.MIN_PERCENT_CHANGE || 0;
data.CONFIG.EMAIL = data.CONFIG.EMAIL || {};
data.CONFIG.MYSQL = data.CONFIG.MYSQL || {};
flight_info.MIN_PERCENT_CHANGE = data.MIN_PERCENT_CHANGE;
email_info.USER = data.CONFIG.EMAIL.USER || '';
email_info.TO = data.CONFIG.EMAIL.TO || '';
email_info.NAME = data.CONFIG.EMAIL.NAME || 'User';
startMysqlConnection(data.CONFIG.MYSQL);
startEmailTransporter(data.CONFIG.EMAIL);
}
}
function startMysqlConnection(database_config) {
var error = false;
if('DATABASE' in database_config === false || database_config.DATABASE.length === 0) {
error = true;
appendLogFile("Missing Database Name in Config\n\n");
}
if('USERNAME' in database_config === false || database_config.USERNAME.length === 0) {
error = true;
appendLogFile(" Missing Username in Config\n\n");
}
if('PASSWORD' in database_config === false || database_config.PASSWORD.length === 0) {
error = true;
appendLogFile("Missing Database in Config\n\n");
}
if('HOST' in database_config === false || database_config.HOST.length === 0) {
error = true;
appendLogFile("Missing Host in Config\n\n");
}
if(error === false) {
var mysql = require('mysql');
connection = mysql.createConnection({
host: database_config.HOST,
user: database_config.USERNAME,
password: database_config.PASSWORD,
database: database_config.DATABASE
});
connection.connect(function(err) {
if(err) {
err.message = "mysql connection";
appendLogFile(JSON.stringify(err, undefined, 4) + "\n\n");
}
else {
startCron();
}
});
}
}
function startEmailTransporter(email_config) {
var error = false;
if(typeof email_config.SERVICE == 'undefined' || email_config.SERVICE.length === 0) {
error = true;
appendLogFile("Missing or Invalid Email Servivce Type\n\n");
}
if(typeof email_config.USER == 'undefined' || email_config.USER.length === 0) {
error = true;
appendLogFile("Missing or Invalid Email User");
}
if(typeof email_config.PASSWORD == 'undefined' || email_config.PASSWORD.length === 0) {
error = true;
appendLogFile("Missing or Invalid Email Password\n\n");
}
if(error === false) {
var nodemailer = require('nodemailer');
transporter = nodemailer.createTransport({
service: email_config.SERVICE,
auth: {
user: email_config.USER,
pass: email_config.PASSWORD
}
});
}
}
function startCron() {
setInterval(function() {
flightDataQuery();
}, 300000);
}
function flightDataQuery() {
var data = {
flightUrl: flight_info.flightUrl,
return_count: 1,
destination: flight_info.destination,
skip_hidden_city: flight_info.skip_hidden_city
};
var waterfall = require('async-waterfall');
getFlightData(data, function(error, body) {
waterfall([
function(callback) {
checkCurrentPrice(body.flightData);
callback(null);
},
function(callback) {
insertFlightData(body.flightData);
callback(null);
}
], function(err, result) {
if(err) {
err.message = "flightDataQuery getFlightData";
appendLogFile(JSON.stringify(err, undefined, 4) + "\n\n");
}
});
});
}
function checkCurrentPrice(flightData) {
var all_time_low_query = "SELECT MIN(price) as lowest_price from flights where from_iata = "+connection.escape(flight_info.departure)+" and to_iata = "+connection.escape(flight_info.destination) + " and date(departure_date) = "+connection.escape(flight_info.departure_date);
var current_low_query = "SELECT price from flights where from_iata = "+connection.escape(flight_info.departure)+" and to_iata = "+connection.escape(flight_info.destination) + " and date(departure_date) = "+connection.escape(flight_info.departure_date) + " and current_low = " + connection.escape("y");
var current_low = null;
var all_time_low = null;
var waterfall = require('async-waterfall');
waterfall([
function(callback) {
connection.query(current_low_query, function(err, rows) {
if(err) {
err.message = "select price from flights where from_iata and to_iata and dat(departure_date) and current_low = Y";
callback(err);
}
else if(rows.length === 0) {
appendLogFile("No row in the db for the current flight, don't check price for email\n\n");
callback(null);
}
else {
current_low = rows[0].price;
callback(null);
}
});
},
function(callback) {
connection.query(all_time_low_query, function(err, rows) {
if(err) {
err.message = "select min(price) as lowest_price from flights where from_iata and to_iata and date(departure_date)";
callback(err);
}
else {
if(rows.length !== 0) {
all_time_low = rows[0].lowest_price;
}
callback(null);
}
});
}
],
function(err, result) {
if(err) {
appendLogFile(JSON.stringify(err, undefined, 4) + "\n\n");
}
else if(current_low !== null) {
var subject_string = '';
var current_price_pennies = flightData[0].price_pennies;
if(current_price_pennies > (current_low * (1 + flight_info.MIN_PERCENT_CHANGE))) {
subject_string = 'Flight Price for ' + flight_info.departure + ' To ' + flight_info.destination + ' Increased ' + ((current_price_pennies / current_low - 1) * 100).toFixed() + '%';
if(all_time_low !== null) {
subject_string += ' Up ' + ((current_price_pennies / all_time_low - 1) * 100).toFixed() + '% From The All Time Low of $' + (all_time_low / 100).toFixed(1);
}
sendEmail(flightData, subject_string);
}
else if(current_price_pennies < (current_low * (1 - flight_info.MIN_PERCENT_CHANGE))) {
subject_string = 'Flight Price for ' + flight_info.departure + ' To ' + flight_info.destination + ' Dropped ' + ((1 - current_price_pennies / current_low) * 100).toFixed() + '%';
if(all_time_low !== null) {
subject_string += ' Down ' + ((current_price_pennies / all_time_low - 1) * 100).toFixed() + '% From The All Time Low of $' + (all_time_low / 100).toFixed(1);
}
sendEmail(flightData, subject_string);
}
}
});
}
function sendEmail(flightData, subject_string) {
var error = false;
if(email_info.USER.length === 0) {
error = true;
appendLogFile("Missing User Email\n\n");
}
if(email_info.TO.length === 0) {
error = true;
appendLogFile("Missing To Email\n\n");
}
if(error === false) {
var mailOptions = {
from: email_info.NAME + ' <' + email_info.USER + '>',
to: email_info.TO,
subject: subject_string,
html: '<pre>'+JSON.stringify(flightData, undefined, 4)+'</pre>'
};
transporter.sendMail(mailOptions, function(err, info){
if(err){
err.message = "transporter.sendMail";
appendLogFile(JSON.stringify(err, undefined, 4) + "\n\n");
}
else {
appendLogFile("Email Sent - " + subject_string + "\n\n");
}
});
}
}
function insertFlightData(flight_data) {
var waterfall = require('async-waterfall');
var flight_id = null;
var flight_exists = false;
var first_flight = true;
waterfall([
function(callback) {
var query = "SELECT * FROM flights";
query += " WHERE from_iata = " +connection.escape(flight_info.departure);
query += " AND to_iata = " + connection.escape(flight_info.destination);
query += " AND date(departure_date) = " + connection.escape(flight_info.departure_date);
connection.query(query, function(err, rows) {
if(err) {
err.message = "select * from flights where from_iata and to_iata and departure_date";
callback(err);
}
else if(rows.length > 0) {
first_flight = false;
callback(null);
}
else {
callback(null);
}
});
},
function(callback) {
var query = "SELECT * FROM flights";
query += " WHERE flight_key_long = " + connection.escape(flight_data[0].flight_key_long);
query += " AND price = " + connection.escape(flight_data[0].price_pennies);
connection.query(query, function(err, rows) {
if(err) {
err.message = "select * from flights where flight_key_long and price";
callback(err);
}
else if(rows.length > 0){
flight_exists = true;
callback(null);
}
else {
callback(null);
}
});
},
function(callback) {
if(flight_exists === false && first_flight === false) {
var query = "UPDATE flights";
query += " SET current_low = " + connection.escape("N");
query += " WHERE from_iata = " +connection.escape(flight_info.departure);
query += " AND to_iata = " + connection.escape(flight_info.destination);
query += " AND date(departure_date) = " + connection.escape(flight_info.departure_date);
connection.query(query, function(err, rows) {
if(err) {
err.message = "update flights set current_low = N where from_iata and to_iata and departure_date";
callback(err);
}
else {
callback(null);
}
});
}
else {
callback(null);
}
},
function(callback) {
if(flight_exists === false) {
var query = "INSERT INTO flights (flight_key, flight_key_long, price, duration, departure_date, from_iata, to_iata, current_low, hidden_city) VALUES (";
query += connection.escape(flight_data[0].flight_key)+",";
query += connection.escape(flight_data[0].flight_key_long)+",";
query += connection.escape(flight_data[0].price_pennies)+",";
query += connection.escape(flight_data[0].duration_seconds)+",";
query += connection.escape(flight_data[0].legs[0].departure_time_formatted)+",";
query += connection.escape(flight_info.departure)+",";
query += connection.escape(flight_info.destination)+",";
query += connection.escape("Y")+",";
query += flight_info.skip_hidden_city === true ? connection.escape("N") : connection.escape("Y");
query += ")";
connection.query(query, function(err, rows) {
if(err) {
err.message = "insert into flights";
callback(err);
}
else {
callback(null);
}
});
}
else {
callback(null);
}
},
function(callback) {
if(flight_exists === false) {
connection.query("SELECT flight_id FROM flights ORDER BY flight_id DESC LIMIT 1", function(err, rows) {
if(err) {
err.message = "select flight_id from flights order by flight_id desc limit 1";
callback(err);
}
else {
flight_id = rows[0].flight_id;
callback(null);
}
});
}
else {
callback(null);
}
}
], function(err, result) {
if(err) {
appendLogFile(JSON.stringify(err, undefined, 4) + "\n\n");
}
else {
if(flight_exists === false) {
var flight_string = "";
flight_string += "New Flight Found\n\n";
flight_string += "Price: " + flight_data[0].price + "\n";
flight_string += "Duration: " + flight_data[0].duration + "\n";
flight_string += "Departure DateTime: " + flight_data[0].departure_time + "\n";
flight_string += "Arrival DateTime: " + flight_data[0].arrival_time + "\n";
flight_string += "Flight Key: " + flight_data[0].flight_key + "\n";
flight_string += "Flight Key Long: " + flight_data[0].flight_key_long + "\n\n";
flight_string += "Flight Inserted\n\n";
appendLogFile(flight_string);
for(var i=0; i<flight_data[0].legs.length; i++) {
insertTripData(flight_id, flight_data, i);
}
}
else {
appendLogFile("Flight Already In Database\n\n");
}
}
});
}
function insertTripData(flight_id, flight_data, leg_number) {
var moment = require('moment-timezone');
var current_flight = flight_data[0].legs[leg_number];
var duration_seconds = current_flight.duration_seconds;
var departure_iata = current_flight.departing_from.split(',');
departure_iata = departure_iata[1].trim();
var departure_time = moment.tz(current_flight.departure_time_formatted, 'America/New_York').utc().format("YYYY-MM-DD HH:mm:ss");
var arrival_iata = current_flight.arriving_at.split(',');
arrival_iata = arrival_iata[1].trim();
var arrival_time = moment.tz(current_flight.arrival_time_formatted, 'America/New_York').utc().format("YYYY-MM-DD HH:mm:ss");
var query = "INSERT INTO trips (airline, duration, flight_number, departure_airport_iata, departure_time, arrival_airport_iata, arrival_time, flight_id)";
query += " VALUES (";
query += connection.escape(current_flight.airline)+",";
query += connection.escape(duration_seconds)+",";
query += connection.escape(current_flight.flight_number)+",";
query += connection.escape(departure_iata)+",";
query += connection.escape(departure_time)+",";
query += connection.escape(arrival_iata)+",";
query += connection.escape(arrival_time)+",";
query += connection.escape(flight_id);
query += ")";
connection.query(query, function(err, rows) {
if(err) {
err.message = "insert into trips";
appendLogFile(JSON.stringify(err, undefined, 4) + "\n\n");
}
else {
var trip_string = "";
trip_string += "New Trip\n\n";
trip_string += "Airline: " + current_flight.airline + "\n";
trip_string += "Flight Number: " + current_flight.flight_number + "\n";
trip_string += "Duration: " + current_flight.duration + "\n";
trip_string += "Departing From: " + current_flight.departing_from + "\n";
trip_string += "Departure DateTime: " + current_flight.departure_time + "\n";
trip_string += "Arriving At: " + current_flight.arriving_at + "\n";
trip_string += "Arrival DateTime: " + current_flight.arrival_time + "\n\n";
trip_string += "Trip Inserted\n\n";
appendLogFile(trip_string);
}
});
}
function parseDurationInt(duration) {
var minutes = Math.round(duration / 60);
var duration_string = '';
var minutes_string = minutes !== 0 ? (minutes + ' Minute' + (minutes > 1 ? 's' : '')) : '';
if(minutes >= 60) {
var minutes_r = minutes % 60;
var hours = (minutes - minutes_r) / 60;
var hours_string = hours !== 0 ? (hours + ' Hour' + (hours > 1 ? 's ' : ' ')) : '';
minutes_string = (minutes - hours * 60) !== 0 ? ((minutes - hours * 60) + ' Minute' + ((minutes - hours * 60) > 1 ? 's' : '')) : '';
if(hours >= 24) {
var hours_r = hours % 24;
var days = (hours - hours_r) / 24;
hours_string = (hours - days * 24) !== 0 ? ((hours - days * 24) + ' Hour' + ((hours - days * 24) > 1 ? 's ' : ' ')) : '';
duration_string = days + ' Day' + (days > 1 ? 's ' : ' ') + hours_string + minutes_string;
}
else {
duration_string = hours_string + minutes_string;
}
}
else {
duration_string = minutes_string;
}
return duration_string;
}
function findTimestampDifference(start_timestamp, end_timestamp) {
var moment = require('moment-timezone');
var zone = "America/Los_Angeles";
var start_timestamp_zoned = moment(moment.tz(start_timestamp, zone).format());
var end_timestamp_zoned = moment(moment.tz(end_timestamp, zone).format());
var difference = end_timestamp_zoned.diff(start_timestamp_zoned, 'seconds');
return difference;
}
function getFlightData(data, callback) {
var airports = require('airport-codes');
var http = require('http');
var moment = require('moment-timezone');
var flightUrl = data.flightUrl;
var return_count = data.return_count;
var destination = data.destination;
var skip_hidden_city = data.skip_hidden_city;
var flights = [];
var time_check = false;
var departure_date_time_preference = null;
if(flight_info.flight_time !== 0 && !isNaN(flight_info.flight_time) && flight_info.flight_time % 1 === 0 && flight_info.flight_time <= 24) {
if(flight_info.before_or_after === 'BEFORE') {
time_check = 1;
departure_date_time_preference = moment(flight_info.departure_date + 'T' + flight_info.flight_time);
}
else if(flight_info.before_or_after == 'AFTER') {
time_check = 2;
departure_date_time_preference = moment(flight_info.departure_date + 'T' + flight_info.flight_time);
}
}
return http.get({host: HOST, path: flightUrl}, function(response) {
var body = '';
response.on('data', function(data) {
body += data;
})
.on('end', function() {
var flight_data = JSON.parse(body);
for(var j=0; j<flight_data.depart.length; j++) {
var is_hidden_city = false;
var incorrect_departure_time = false;
var key = flight_data.depart[j][3];
var current_flight = {
price: '$' + (flight_data.depart[j][0][0] / 100).toFixed(2),
price_pennies: flight_data.depart[j][0][0],
duration: parseDurationInt(flight_data.flights[key][1]),
duration_seconds: flight_data.flights[key][1],
departure_time: '',
arrival_time: '',
legs: [],
flight_key: key,
flight_key_long: flight_data.depart[j][2]
};
for(var i=0; i<flight_data.flights[key][0].length; i++) {
var departure_zone = airports.findWhere({iata: flight_data.flights[key][0][i][1]}).get('tz');
if(i === (flight_data.flights[key][0].length - 1) && flight_data.flights[key][0][(flight_data.flights[key][0].length) - 1][3] != destination && skip_hidden_city === true) {
is_hidden_city = true;
break;
}
if(time_check !== false && i === 0) {
var departure_moment = moment.tz(flight_data.flights[key][0][i][2], departure_zone);
var flight_time_moment = moment.tz(flight_info.departure_date+'T'+flight_info.flight_time, departure_zone);
var difference = departure_moment.diff(flight_time_moment, 'minutes');
if(time_check === 1 && difference > 0) {
incorrect_departure_time = true;
break;
}
else if(time_check === 2 && difference < 0){
incorrect_departure_time = true;
break;
}
}
var arrival_zone = airports.findWhere({iata: flight_data.flights[key][0][i][3]}).get('tz');
var duration_seconds = findTimestampDifference(flight_data.flights[key][0][i][2], flight_data.flights[key][0][i][4]);
var duration_formatted = parseDurationInt(duration_seconds);
var airline = flight_data.airlines[flight_data.flights[key][0][i][0].substring(0, 2)];
var flight_number = flight_data.flights[key][0][i][0];
var departing_from = airports.findWhere({iata: flight_data.flights[key][0][i][1]}).get('name') + ', ' + flight_data.flights[key][0][i][1] + ', ' + airports.findWhere({iata: flight_data.flights[key][0][i][1]}).get('city') + ', ' + airports.findWhere({iata: flight_data.flights[key][0][i][1]}).get('country');
var arriving_at = airports.findWhere({iata: flight_data.flights[key][0][i][3]}).get('name') + ', ' + flight_data.flights[key][0][i][3] + ', ' + airports.findWhere({iata: flight_data.flights[key][0][i][3]}).get('city') + ', ' + airports.findWhere({iata: flight_data.flights[key][0][i][3]}).get('country');
var departure_time = moment.tz(flight_data.flights[key][0][i][2], departure_zone).format('dddd, MMMM Do YYYY, hh:mma');
var departure_time_formatted = flight_data.flights[key][0][i][2];
var arrival_time = moment.tz(flight_data.flights[key][0][i][4], arrival_zone).format('dddd, MMMM Do YYYY, hh:mma');
var arrival_time_formatted = flight_data.flights[key][0][i][4];
var current_leg = {airline: airline, flight_number: flight_number, duration: duration_formatted, duration_seconds: duration_seconds, departing_from: departing_from, departure_time: departure_time, departure_time_formatted: departure_time_formatted, arriving_at: arriving_at, arrival_time: arrival_time, arrival_time_formatted: arrival_time_formatted};
if(i === 0) {
current_flight.departure_time = departure_time;
}
else if(i === flight_data.flights[key][0].length - 1) {
current_flight.arrival_time = arrival_time;
}
current_flight.legs.push(current_leg);
}
if(is_hidden_city === false && incorrect_departure_time === false) {
flights.push(current_flight);
}
if(return_count === 1 && flights.length === 1) {
break;
}
}
callback(null, {flightData: flights});
});
});
}
function appendLogFile(text) {
var moment = require('moment-timezone');
var datetime = moment().tz("America/Los_Angeles").format();
fs.appendFile(log, datetime + " " + text);
}
Flight.prototype.parseDuration = function(duration) {
return parseDurationInt(duration);
};
Flight.prototype.timestampDifference = function(start_timestamp, end_timestamp) {
return findTimestampDifference(start_timestamp, end_timestamp);
};
Flight.prototype.getFlightUrl = function() {
return this.flightUrl;
};
Flight.prototype.getFlightData = function(callback) {
var data = {
flightUrl: this.flightUrl,
return_count: this.return_count,
destination: this.destination,
skip_hidden_city: this.skip_hidden_city
};
return getFlightData(data, callback);
};