Initial commit

This commit is contained in:
Carlos E Silva
2017-11-09 17:38:54 -05:00
commit 205ea819d0
4 changed files with 195 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode
node_modules

81
index.js Normal file
View File

@ -0,0 +1,81 @@
const fetch = require('node-fetch');
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'carrier', type: String, defaultValue: 'TMOBILE' },
{ name: 'model', type: String, defaultValue: 'x' },
{ name: 'color', type: String, defaultValue: 'gray' },
{ name: 'storage', type: String, defaultValue: '256' },
{ name: 'zip', type: String, defaultOption: true },
{ name: 'delay', type: Number, defaultValue: 30 },
];
const options = commandLineArgs(optionDefinitions);
console.log(options);
const partNumbers = {
'x': {
'gray': {
'64': "",
'256': "MQAU2LL/A",
},
'silver': {
'64': "",
'256': "",
},
},
'8': {
'gray': {
'64': "",
'256': "MQ932LL/A",
},
'silver': {
'64': "",
'256': "",
},
},
};
const partNumber = partNumbers[options.model][options.color][options.storage];
const endpoint = `https://www.apple.com/shop/retail/pickup-message?pl=true&cppart=${options.carrier}/US&parts.0=${partNumber}&location=${options.zip}`;
// const endpoint = 'https://www.apple.com/shop/retail/pickup-message?pl=true&cppart=TMOBILE/US&parts.0=MQ932LL/A&location=Salem,%20NH';
let requestsMade = 0;
makeRequest();
function checkAvailability(data) {
const { stores } = data.body;
const storesAvailable = stores.filter(store => {
const parts = Object.values(store.partsAvailability);
const part = parts[0];
const pickupDisplay = part.pickupDisplay;
const availability = pickupDisplay === 'available';
return availability;
});
if (storesAvailable.length > 0) {
console.log(`Available at ${storesAvailable.length} stores near you:`);
console.log(storesAvailable.map(store => `${store.address.address} which is ${store.storeDistanceWithUnit} away`).reduce((msg,store) => `${msg}\n${store}`));
process.exit();
} else {
displayResultInPlace("unavailable");
setTimeout(() => {
makeRequest();
requestsMade++;
}, options.delay * 1000);
}
}
function makeRequest() {
fetch(endpoint)
.then(stream => stream.json())
.then(data => checkAvailability(data))
.catch(error => console.log('Fetch Error :-S', error));
}
function displayResultInPlace(data) {
process.stdout.write(`${data} --- req: ${requestsMade}\r`);
}

96
package-lock.json generated Normal file
View File

@ -0,0 +1,96 @@
{
"name": "iphone-x-availability",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"array-back": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz",
"integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==",
"requires": {
"typical": "2.6.1"
}
},
"command-line-args": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-4.0.7.tgz",
"integrity": "sha512-aUdPvQRAyBvQd2n7jXcsMDz68ckBJELXNzBybCHOibUWEg0mWTnaYCSRU8h9R+aNRSvDihJtssSRCiDRpLaezA==",
"requires": {
"array-back": "2.0.0",
"find-replace": "1.0.3",
"typical": "2.6.1"
}
},
"encoding": {
"version": "0.1.12",
"resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz",
"integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=",
"requires": {
"iconv-lite": "0.4.19"
}
},
"find-replace": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/find-replace/-/find-replace-1.0.3.tgz",
"integrity": "sha1-uI5zZNLZyVlVnziMZmcNYTBEH6A=",
"requires": {
"array-back": "1.0.4",
"test-value": "2.1.0"
},
"dependencies": {
"array-back": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz",
"integrity": "sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=",
"requires": {
"typical": "2.6.1"
}
}
}
},
"iconv-lite": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz",
"integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ=="
},
"is-stream": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz",
"integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ="
},
"node-fetch": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
"integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
"requires": {
"encoding": "0.1.12",
"is-stream": "1.1.0"
}
},
"test-value": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/test-value/-/test-value-2.1.0.tgz",
"integrity": "sha1-Edpv9nDzRxpztiXKTz/c97t0gpE=",
"requires": {
"array-back": "1.0.4",
"typical": "2.6.1"
},
"dependencies": {
"array-back": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz",
"integrity": "sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=",
"requires": {
"typical": "2.6.1"
}
}
}
},
"typical": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/typical/-/typical-2.6.1.tgz",
"integrity": "sha1-XAgOXWYcu+OCWdLnCjxyU+hziB0="
}
}
}

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "iphone-x-availability",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Carlos E Silva",
"license": "ISC",
"dependencies": {
"command-line-args": "^4.0.7",
"node-fetch": "^1.7.3"
}
}