Skip to content

Instantly share code, notes, and snippets.

@jbrooksuk jbrooksuk/index.js forked from andyberry88/index.js
Last active Aug 29, 2015

Embed
What would you like to do?
var https = require('https');
var Notification = require('node-notifier');
var CronJob = require('cron').CronJob;
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 = {};
new CronJob('*/2 * * * *', function() {
console.log("Checking stock in the UK stores...")
console.log("");
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);
});
}, null, true);
function listAvailableStock() {
var notifier = new Notification();
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];
for (var s in stockEntry) {
if (stockEntry[s] == true) {
foundStock = true;
// console.log(storeName + " has stock!");
notifier.notify({
title: 'Stock found!',
message: storeName + " has stock!"
});
break;
}
}
}
if (!foundStock) {
notifier.notify({
title: 'No stock anywhere!',
message: "Dang."
});
}
console.log("");
console.log("Go to https://reserve.cdn-apple.com/GB/en_GB/reserve/iPhone/availability for more info");
console.log("");
console.log("Last updated: " + stockLastUpdated.getHours()+":"+stockLastUpdated.getMinutes());
}
}
@ilium007

This comment has been minimized.

Copy link

ilium007 commented Oct 15, 2014

How do I run this ? I am running:

node index.js

after installing the dependancies with npm) but all I get is:

var notifier = new Notification(); ^ TypeError: object is not a function at listAvailableStock (/Users/ilium007/index.js:48:17) at IncomingMessage.<anonymous> (/Users/ilium007/index.js:25:4) at IncomingMessage.emit (events.js:117:20) at _stream_readable.js:943:16 at process._tickCallback (node.js:419:13)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.