forked from TomThorpe/NodeiPhoneStockChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
274 lines (244 loc) · 9.99 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
/*
* Copyright 2016 Tom Thorpe
*
* Distributed under the ISC Licence
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Tom Thorpe
*/
var rp = require('request-promise');
var Promise = require("bluebird");
var NodeCache = require("node-cache");
var models = require("./iphone-models.js");
var config = require("./config.js");
/**
* Cache to stop messages being sent about stock on every request. If a message was already sent in last x seconds, it wont be sent again
*/
var notificationsSentCache = new NodeCache({
stdTTL: 300,
checkperiod: 120
});
var storesRequest = {
uri: config.storesJsonUrl,
json: true
};
var stockRequest = {
uri: config.stockJsonUrl,
json: true
};
/**
* Begin the process to check the stock, then recursively calls itself asyncronously to check the stock again after interval time
* @param {object} stores - flattend associative array of stores. Store code as the key, and the store name as the value
*/
function getStock(stores) {
getStockRequest(stores)
.delay(config.interval)
.then(function() {
process.nextTick(function() {
getStock(stores)
}); //nexttick to stop memory leaking in recursion, force async
})
}
/**
* Makes a single call to the stock url to check the stock.
* @param {object} stores - flattend associative array of stores. Store code as the key, and the store name as the value
*/
function getStockRequest(stores) {
return rp(stockRequest)
.then(function(stock) {
console.log("got stock list");
processStock(stores, stock);
})
.catch(function(err) {
reportError("Error downloading stock list " + err)
});
}
/**
* Once stock is retrieved from the stock url, checks the stock to see if it has any models you're interested in
* @param {object} stores - flattend associative array of stores. Store code as the key, and the store name as the value
* @param {object} stock - parsed json retreived from the stock url
*/
function processStock(stores, stock) {
var storesWithStock = [];
var unfoundModels = {}; //where the model code doesnt exist
Object.keys(stock).forEach(function(storeCode) {
var store = stores[storeCode];
if (store == undefined) {
return; //skip non-stores
}
checkStoreStock(store, storeCode, stock, storesWithStock, unfoundModels);
});
sendStockMessage(storesWithStock);
sendUnfoundModelsMessage(unfoundModels)
}
/**
* For a single store (represented by storcode) checks that stores stock to see if it has a model you are interested in
* @param {string} store - The store name (e.g. Covent Garden)
* @param {object} storeCode - the store code (e.g. R232)
* @param {object} stock - parsed json retreived from the stock url
* @param {array} storesWithStock - A string array, stores that have stock of the model you are interest in will be added to this array (in the format of a user displayable string message saying x store has stock of y)
* @param {object} unfoundModels - associative array, models that were not found in the stores stock list will be added to this so that you can report back to the user that they may have a typo or non-existant model
*/
function checkStoreStock(store, storeCode, stock, storesWithStock, unfoundModels) {
var storeStock = stock[storeCode];
config.modelsWanted.forEach(function(modelCode) {
if (storeStock[modelCode] == undefined) {
unfoundModels[modelCode] = 1;
} else if (storeStock[modelCode].toLowerCase() == "all") {
addStoreToNotification(storesWithStock, store, modelCode, storeCode);
}
});
}
/**
* Adds a store to the list of stores with stock, when stock of a model is found
* First of all checks the cache to see if a notification was already sent about this store and this model, if so, this will skip adding that model to the notification and do nothing.
* @param {array} storesWithStock - A string array, stores that have stock of the model you are interest in (and no message was already sent recently) will be added to this array (in the format of a user displayable string message saying x store has stock of y).
* @param {string} store - The store name (e.g. Covent Garden)
* @param {string} modelCode - The model found in stock
* @param {object} storeCode - the store code (e.g. R232) the stock was found in
*/
function addStoreToNotification(storesWithStock, store, modelCode, storeCode) {
//check if it is in the cache to say a notification was already recently sent about this store
var key = storeCode + modelCode;
var cached = notificationsSentCache.get(key);
if (cached == undefined) {
notificationsSentCache.set(key, "sent");
storesWithStock.push(store + " has stock of " + models.models[modelCode]);
}
}
/**
* Chunks an array.
* Returns an array of arrays, all of max n size
*/
function chunk (array, size) {
return array.reduce(function (res, item, index) {
if (index % size === 0) { res.push([]); }
res[res.length-1].push(item);
return res;
}, []);
}
/**
* Send the notification about models found stock (if any - if no models are found no notification will be displayed, and
* "No Stock" is diplayed in the console)
* Sends at most 50 stores at a time, so chunks into multiple messages if there are more stores with stock.
* @param {array} storesWithStock an array of messages about stock found in stores, e.g. ["model x was found in y", "model z was found in y"]. This will be used as the notificaiton text
*/
function sendStockMessage(storesWithStock) {
if (storesWithStock.length > 0) {
var chunks = chunk(storesWithStock, 50);
chunks.forEach(function(storesChunk){
var message = "";
storesChunk.forEach(function(storeMessage) {
message += storeMessage + "\n";
});
console.log(message);
sendProwlMessage(message, 2);
})
} else {
console.log("No New Stock");
}
}
/**
* Send the notification about models that were not found in any of the store lists (e.g. due to issue with store feed)
* @param {object} unfoundModels an associative array of model codes as keys where the model was not found (e.g. {"B35643" : anything, "FKGJF" : anything} )
*/
function sendUnfoundModelsMessage(unfoundModels) {
var unfound = "";
for (var key in unfoundModels) {
if (unfoundModels.hasOwnProperty(key)) {
unfound += key + " ";
}
}
if (unfound.length > 0) {
reportError("Some of the models you requested were not found in the store stock list, there is either a problem with the store feed, or you have picked the wrong models for the country you chose " + unfound);
}
}
/**
* Sends a message over prowl to the user if theprowlApiKey variable is set up.
* Does nothing if no api key exists
* @param {string} message The message to send. This is the exact text that will get sent as the notification
* @param {int} priority A priority between -2 (least priority) an 2 (most priority) as defined in the prowl API
*/
function sendProwlMessage(message, priority) {
if (config.prowlApiKey.length > 0) {
var prowlApiRequest = {
method: 'POST',
uri: 'https://api.prowlapp.com/publicapi/add',
form: {
apikey: config.prowlApiKey,
priority: priority,
application: "Stock Checker",
"event": "iPhone 7 Stock",
description: message,
},
};
rp(prowlApiRequest)
.then(function() {
console.log("push notification sent");
})
.catch(function(err) {
console.log("Error sending push notification" + err);
});
} else {
console.log("Prowl message skipped due to no api key");
}
}
/**
* Logs an error on the console and by sending a prowl message
* @param {string} error the error message
*/
function reportError(error) {
var message = "iPhone Stock Checker Error: " + error;
console.log("ERROR:" + message);
sendProwlMessage(message, 0);
}
/**
* Validates the models you have asked for are in the valid models list (models) and the list isnt empty;
*/
function validateWantedModels() {
//no wanted models
if (config.modelsWanted.length == 0) {
reportError("You have not set up any wanted models in the modelsWanted property. Polling has NOT started! ");
return false;
}
//no models config
if (models.models.length == 0) {
reportError("There are no models in the models config, this is a configuration error. Polling has NOT started! ");
return false;
}
//check validity of modelsWanted
var invalidModels = [];
config.modelsWanted.forEach(function(model) {
if (models.models[model] == undefined) {
invalidModels.push(model);
}
});
if (invalidModels.length > 0) {
var message = "Invalid models were found in your modelsWanted property. Polling has NOT started! ";
message += invalidModels.reduce(function(result, current) {
return result += " " + current
}, "");
reportError(message);
return false
}
return true;
}
//Go!
if (validateWantedModels()) {
rp(storesRequest)
.then(function(stores) {
console.log("Downloaded stores list");
var storesFlattend = {};
stores.stores.forEach(function(store) {
storesFlattend[store.storeNumber] = store.storeName
});
sendProwlMessage("Stores list has been successfully downloaded, stock checker will now start. This is a test prowl message to preview the message you will get when stock arrives", 2);
getStock(storesFlattend)
})
.catch(function(err) {
reportError("Error downloading stores " + err);
});
}