Last active
September 18, 2016 13:51
-
-
Save andy-berry-dev/6728fa0c631b1ec5e84f to your computer and use it in GitHub Desktop.
Node script to query the apple stock JSON blob and report which stores have iphone 6 stock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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