-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
95 lines (75 loc) · 2.37 KB
/
main.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
var https = require('https');
var notifier = require('node-notifier');
var Repeat = require('repeat');
var storesUrl = 'https://reserve.cdn-apple.com/GB/en_GB/reserve/iPhone/stores.json';
var stockUrl = 'https://reserve.cdn-apple.com/GB/en_GB/reserve/iPhone/availability.json';
var stockLastUpdated;
var stores;
var stock;
var storeNameMap = {};
Repeat(checkStock).every(2, 'minutes').start.now();
function checkStock() {
console.log("Checking stock in the London stores...")
https.get(storesUrl, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
stores = JSON.parse(body).stores;
listAvailableStock();
});
}).on('error', function(e) {
console.log("Got error for URL "+storesUrl+" : ", e);
});
https.get(stockUrl, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
stock = JSON.parse(body);
stockLastUpdated = new Date(res.headers["last-modified"]);
delete stock.updated;
listAvailableStock();
});
}).on('error', function(e) {
console.log("Got error for URL "+storesUrl+" : ", e);
});
};
function listAvailableStock() {
if (stores != null && stock != null) {
for (var i in stores) {
var store = stores[i];
var storeName = store.storeName;
var storeNumber = store.storeNumber;
storeNameMap[storeNumber] = storeName;
}
var foundStock = false;
for (var storeNumber in stock) {
var stockEntry = stock[storeNumber];
var storeName = storeNameMap[storeNumber];
// opt filter by store
if ((storeName == "Brent Cross") || (storeName == "Stratford City") || (storeName == "White City") || (storeName == "Covent Garden") || (storeName == "Watford"))
{
for (var product in stockEntry) {
// opt filter by model, finish, storage
if ((product == 'MN4M2B/A'))
{ //Black 7 Plus 128GB
if (stockEntry[product] == 'ALL') {
foundStock = true;
console.log("\t *** " + storeName + " has stock of " + product + "! ***");
notifier.notify({
title: 'Stock found!',
message: storeName + " has stock of " + product,
open: "https://reserve-gb.apple.com/GB/en_GB/reserve/iPhone?execution=e2s1"
});
break;
}
}
}
}
}
console.log("Last updated: " + stockLastUpdated.getHours()+":"+stockLastUpdated.getMinutes());
}
}