/
app.js
153 lines (125 loc) · 3.27 KB
/
app.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
var notifier = require('node-notifier');
var path = require('path');
var fs = require('fs');
var Prices = require('./prices');
var deals = [],
priceArray = [],
checkFreq = 1800000;
function convertToMilliseconds(minutes) {
return minutes * 60000;
}
function readJSON(data, callback) {
console.log('File read');
data = JSON.parse(data);
if (data.constructor !== Array) {
console.log('Hmm... data read wasn\'t an array');
return;
}
callback(data);
}
function checkForJSON(callback) {
fs.readFile(path.join(__dirname, '/data/prices.json'), 'utf-8', function(err, data) {
if (err) {
fs.writeFile(path.join(__dirname, '/data/prices.json'), '[]', function() {
readJSON('[]', callback);
});
} else {
readJSON(data, callback)
}
});
}
function saveJSON(obj) {
var jsonObj = JSON.stringify(obj);
fs.writeFile(path.join(__dirname, '/data/prices.json'), jsonObj, function(err) {
if (err) {
console.log('Uh oh, there was an error saving: ', err);
} else {
console.log('File saved');
}
});
}
function getNewPrices() {
return Prices.getPrices();
}
function checkForDiffs(orgPrice, newPrice) {
return newPrice - orgPrice;
}
function newDeal(dealsObj) {
var title,
msg;
//console.log('A new deal!', dealsObj);
// is it a deal?
if (dealsObj.difference > 0) {
title = 'Uh oh...';
msg = 'The price on one of the flights just jumped from $' + dealsObj.origPrice + ' to $' + dealsObj.newPrice;
} else {
title = 'A new deal!',
msg = 'There\'s a new deal from Southwest. The $' + dealsObj.origPrice + ' price just went down to $' + dealsObj.newPrice;
}
notifier.notify({
title: title,
message: msg,
icon: path.join(__dirname, 'southwest-airlines-heart.png')
});
}
function processPrices(matches) {
var newDeals = [];
// look for any deals by comparing the existing array
matches.forEach(function(price, i) {
if (checkForDiffs(priceArray[i], price) !== 0) {
newDeals.push({
origPrice : priceArray[i],
newPrice: price,
difference: checkForDiffs(priceArray[i], price)
});
}
});
// set the price array with these matches
priceArray = matches;
return newDeals;
}
function timer(delay) {
var newDeals;
// set delay to zero if it isn't set
if (!delay) {
delay = 0;
}
// get new prices
setTimeout(function() {
getNewPrices().then(function(matches) {
// when promise is resolved...
// show matches in console
//console.log('New data retrieved: ', matches);
console.log('New data retrieved!')
// save the matches
saveJSON(matches);
// process the results
newDeals = processPrices(matches);
if (newDeals.length > 0) {
newDeals.forEach(function(aNewDeal) {
newDeal(aNewDeal);
});
}
// reset the timer
timer(checkFreq);
});
}, delay);
}
function init() {
// set things up
Prices.setData({
origin: 'BWI',
destination: 'OAK',
outboundDate: '11/28/2016',
returnDate: '11/29/2016'
});
// set the delay (pass in minutes)
checkFreq = convertToMilliseconds(30);
// get prices from last time
checkForJSON(function(data) {
priceArray = data;
// start the timer!
timer();
});
}
init();