From 205ea819d05cc1c38f64dce7aa25350e8e25e181 Mon Sep 17 00:00:00 2001 From: Carlos E Silva Date: Thu, 9 Nov 2017 17:38:54 -0500 Subject: [PATCH] Initial commit --- .gitignore | 2 + index.js | 81 +++++++++++++++++++++++++++++++++++++++ package-lock.json | 96 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 16 ++++++++ 4 files changed, 195 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f32e4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +node_modules \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..8c8b552 --- /dev/null +++ b/index.js @@ -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`); +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f78f5af --- /dev/null +++ b/package-lock.json @@ -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=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..42be484 --- /dev/null +++ b/package.json @@ -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" + } +}