-
-
Save apparition47/b18016309b9cdabf1c58 to your computer and use it in GitHub Desktop.
iPhone 6 Availability Checker
This file contains hidden or 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
/** | |
* Node script to query the apple stock JSON blob and report which stores have iphone 6 stock | |
* Change the model nums and models below. | |
* | |
* Based on work done by andyberry88 and jbrooksuk | |
*/ | |
var https = require('https'); | |
var Notification = require('node-notifier'); | |
var Repeat = require('repeat'); | |
var storesUrl = 'https://reserve.cdn-apple.com/CA/en_CA/reserve/iPhone/stores.json'; | |
var stockUrl = 'https://reserve.cdn-apple.com/CA/en_CA/reserve/iPhone/availability.json'; | |
var stockLastUpdated; | |
var stores; | |
var stock; | |
var storeNameMap = {}; | |
Repeat(checkStock).every(5000, 'ms').start.now(); | |
function checkStock() { | |
console.log("Checking stock in the CA 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() { | |
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]; | |
// opt filter by store | |
if ((storeName == 'Richmond Centre') || (storeName == 'Pacific Centre') || (storeName == 'Oakridge Centre') || (storeName == 'Metrotown') || (storeName == 'Coquitlam Centre') || (storeName == 'Guildford Town Centre')) | |
{ | |
for (var product in stockEntry) { | |
// opt filter by model, finish, storage | |
if ((product == 'MG3A2CL/A')) | |
{ // space grey 16gb | |
if (stockEntry[product] == true) { | |
foundStock = true; | |
console.log("\t *** " + storeName + " has stock of " + product + "! ***"); | |
notifier.notify({ | |
title: 'Stock found!', | |
message: storeName + " has stock of " + product, | |
open: "https://reserve-ca.apple.com/CA/en_CA/reserve/iPhone" | |
}); | |
break; | |
} | |
} | |
} | |
} | |
} | |
console.log("Last updated: " + stockLastUpdated.getHours()+":"+stockLastUpdated.getMinutes()); | |
} | |
} |
This file contains hidden or 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
{ | |
"name": "iPhone6", | |
"description": "CA", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"node-notifier": "*", | |
"repeat": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only way I could get this to work on my OSX installed node was to change the following:
var Notification = require('node-notifier');
to
var notifier = require('node-notifier');
and comment: