Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gotcha Your iPhone X
npm install request-promise
npm install moment
node GotchaYouriPhoneX.js CN

output

👉  node GotchaYouriPhoneX.js CN
预约URL: https://reserve-prime.apple.com/CN/zh_CN/reserve/iPhoneX/availability?channel=1
Updated Time:  2017 November 9th, 04:50:30 pm
北京 三里屯		iPhone X 64GB 银色	有合约机 有解锁机
上海 南京东路		iPhone X 64GB 银色	有合约机 有解锁机
北京 王府井		iPhone X 64GB 银色	有合约机 有解锁机
var rp = require('request-promise');
var moment = require('moment');
var storesURL = function(localtion){
return 'https://reserve-prime.apple.com/'+localtion+'/zh_'+localtion+'/reserve/iPhoneX/stores.json';
}
var availabilityURL = function(localtion){
return 'https://reserve-prime.apple.com/'+localtion+'/zh_'+localtion+'/reserve/iPhoneX/availability.json';
};
var location = ['CN','HK'];
var options = {
uri: storesURL,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.1 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.1'
},
json: true
};
var kDeviceName = {
'MQA92CH/A':'iPhone X 256GB 银色',
'MQA82CH/A':'iPhone X 256GB 深空灰',
'MQA52CH/A':'iPhone X 64GB 深空灰',
'MQA62CH/A':'iPhone X 64GB 银色'
}
var kStoreName = {
};
function getAvailableString(info){
var availability = info.availability;
var ret = '';
if (availability.contract) {
ret = ret + '有合约机 ';
}
if (availability.unlocked) {
ret = ret + '有解锁机 ';
}
return ret;
}
function getTSString(ts) {
var timestamp = moment.unix(ts/1000).format("YYYY MMMM Do, hh:mm:ss a");
return timestamp;
}
var checkStatus = function(localtion){
options.uri = storesURL(localtion);
rp(options).then( stores =>{
console.log('预约URL: '+'https://reserve-prime.apple.com/CN/zh_CN/reserve/iPhoneX/availability?channel=1');
var shops = stores.stores;
shops.forEach(elem => {
kStoreName[elem.storeNumber] = elem;
});
var timestamp = getTSString(stores.config.updatedTime);
return shops;
}).then(shops =>{
options.uri = availabilityURL(localtion);
var shopsNum = [];
shops.forEach(elem => {
shopsNum.push(elem.storeNumber);
});
rp(options).then( fengX =>{
var timestamp = getTSString(fengX.updated);
console.log('Updated Time: ',timestamp);
var allShops = fengX.stores;
for (let i = 0; i < shopsNum.length; i++) {
var shopId = shopsNum[i];
var devices = allShops[shopId];
for (let deviceKey in devices) {
var info = devices[deviceKey];
var status = getAvailableString(info);
if (status.length > 0) {
var city = kStoreName[shopId].city;
var store = kStoreName[shopId].storeName;
store = store.replace(city,"");
console.log(city+' '+store+'\t\t'+kDeviceName[deviceKey] +'\t'+ status);
}
}
}
})
})
}
var location = process.argv[2];
setInterval(function () {
if (location == "CN" || location == "cn") {
checkStatus('CN');
}else{
checkStatus('HK');
}
process.stdout.write('\033c');
}, 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment