-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
147 lines (122 loc) · 4.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const request = require("request");
const notifier = require("node-notifier");
const flatMap = require('array.prototype.flatmap');
const replaceAll = require("string.prototype.replaceall");
flatMap.shim();
replaceAll.shim();
const { COUNTRIES } = require("./constants");
const args = process.argv.slice(2);
let skusForCountry = (countrySkuCode) => {
return {
[`MKGR3${countrySkuCode}/A`]: `14" M1 Pro 8 Core CPU 14 Core GPU 512GB Silver`,
[`MKGP3${countrySkuCode}/A`]: '14" M1 Pro 8 Core CPU 14 Core GPU 512GB Space Grey',
[`MKGT3${countrySkuCode}/A`]: '14" M1 Pro 10 Core CPU 16 Core GPU 1TB Silver',
[`MKGQ3${countrySkuCode}/A`]: '14" M1 Pro 10 Core CPU 16 Core GPU 1TB Space Grey',
[`MMQX3${countrySkuCode}/A`]: '14" M1 Max 10 Core CPU 32 Core GPU 2TB Silver, Ultimate',
[`MKH53${countrySkuCode}/A`]: '14" M1 Max 10 Core CPU 32 Core GPU 2TB Space Grey, Ultimate',
[`MK1H3${countrySkuCode}/A`]: '16" M1 Max 10 Core CPU 32 Core GPU 1TB Silver',
[`MK1A3${countrySkuCode}/A`]: '16" M1 Max 10 Core CPU 32 Core GPU 1TB Space Grey',
[`MMQW3${countrySkuCode}/A`]: '16" M1 Max 10 Core CPU 32 Core GPU 4TB Silver, Ultimate',
[`MK233${countrySkuCode}/A`]: '16" M1 Max 10 Core CPU 32 Core GPU 4TB Space Grey, Ultimate',
[`MK1F3${countrySkuCode}/A`]: '16" M1 Pro 10 Core CPU 16 Core GPU 1TB Silver',
[`MK193${countrySkuCode}/A`]: '16" M1 Pro 10 Core CPU 16 Core GPU 1TB Space Grey',
[`MK1E3${countrySkuCode}/A`]: '16" M1 Pro 10 Core CPU 16 Core GPU 512GB Silver',
[`MK183${countrySkuCode}/A`]: '16" M1 Pro 10 Core CPU 16 Core GPU 512GB Space Grey',
}
}
let favouritesForCountry = (countrySkuCode) => {
return [
`MMQX3${countrySkuCode}/A`,
`MKH53${countrySkuCode}/A`,
`MK1A3${countrySkuCode}/A`,
`MK1H3${countrySkuCode}/A`,
]
}
const control = "MYD92LL/A";
let storeNumber = "R172";
let state = "CO";
let country = "US"
if (args.length > 0) {
const passedStore = args[0];
country = (args[1] ? args[1] : "US").toUpperCase();
if (passedStore.charAt(0) === "R") {
// All retail store numbers start with R
storeNumber = passedStore;
state = null;
}
}
const countryConfig = COUNTRIES[country];
let storePath = countryConfig["storePath"];
let skuList = skusForCountry(countryConfig["skuCode"]);
let favorites = favouritesForCountry(countryConfig["skuCode"]);
const query =
Object.keys(skuList)
.map((k, i) => `parts.${i}=${encodeURIComponent(k)}`)
.join("&") + `&searchNearby=true&store=${storeNumber}`;
let options = {
method: "GET",
url: `https://www.apple.com${storePath}/shop/fulfillment-messages?` + query,
};
request(options, function (error, response) {
if (error) throw new Error(error);
const body = JSON.parse(response.body);
const storesArray = body.body.content.pickupMessage.stores;
let skuCounter = {};
let hasStoreSearchError = false;
console.log('Inventory');
console.log('---------');
const statusArray = storesArray
.flatMap((store) => {
if (state && state !== store.state) return null;
const name = store.storeName;
let productStatus = [];
for (const [key, value] of Object.entries(skuList)) {
const product = store.partsAvailability[key];
hasStoreSearchError = product.storeSearchEnabled !== true;
if (key === control && hasStoreSearchError !== true) {
hasStoreSearchError = product.pickupDisplay !== "available";
} else {
productStatus.push(`${value}: ${product.pickupDisplay}`);
if (product.pickupDisplay === "available") {
console.log(`${value} in stock at ${store.storeName}`);
let count = skuCounter[key] ? skuCounter[key] : 0;
count += 1;
skuCounter[key] = count;
}
}
}
return {
name: name,
products: productStatus,
};
})
.filter((n) => n);
let hasError = hasStoreSearchError;
const inventory = Object.entries(skuCounter)
.map(([key, value]) => `${skuList[key]}: ${value}`)
.join(" | ");
console.log('\nInventory counts');
console.log('----------------');
console.log(inventory.replaceAll(" | ", "\n"));
let hasUltimate = Object.keys(skuCounter).some(
(r) => favorites.indexOf(r) >= 0
);
let notificationMessage;
if (inventory) {
notificationMessage = `${hasUltimate ? "FOUND ULTIMATE! " : ""
}Some models found: ${inventory}`;
} else {
notificationMessage = "No models found.";
console.log(statusArray);
console.log(notificationMessage);
}
const message = hasError ? "Possible error?" : notificationMessage;
notifier.notify({
title: "MacBook Pro Availability",
message: message,
sound: hasError || inventory,
timeout: false,
});
// Log time at end
console.log(`\nGenerated: ${new Date().toLocaleString()}`);
});