Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node script to query the apple stock JSON blob and report which stores have iphone 6 stock
var https = require('https');
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 = {};
console.log("checking stock for 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);
});
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];
for (var s in stockEntry) {
if (stockEntry[s] == true) {
foundStock = true;
console.log(storeName + " has stock!");
break;
}
}
}
if (!foundStock) {
console.log("No stores have stock :-(");
}
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());
}
}
@jbrooksuk

This comment has been minimized.

Copy link

commented Sep 22, 2014

Added checking every 5 minutes and an OSX notification; https://gist.github.com/jbrooksuk/fc50ec34f4864ec431d9

@apparition47

This comment has been minimized.

Copy link

commented Oct 1, 2014

Canada Apple Store version. Runs every 5 seconds, filter by store and model, clicking on popup notification opens reservation page. Use npm install to install dependencies.
https://gist.github.com/apparition47/b18016309b9cdabf1c58

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.