Skip to content

Instantly share code, notes, and snippets.

@bemoty
Created September 20, 2022 11:07
Show Gist options
  • Save bemoty/c2179417d85c68cb003ef19faf117d4f to your computer and use it in GitHub Desktop.
Save bemoty/c2179417d85c68cb003ef19faf117d4f to your computer and use it in GitHub Desktop.
checks the apple store if specific parts are available at a store and notifies you on discord
import { Client, GatewayIntentBits } from "discord.js";
import fetch from "node-fetch";
import process from "process";
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const DELAY_IN_SECONDS = 60 * 3;
const TARGET_URL = "https://www.apple.com/de/shop/fulfillment-messages";
const locations = [
{
plz: "80331", // munich
name: "Rosenstraße",
},
];
const parts = [
"MQ083ZD/A", // iphone 14 pro gold 128gb
"MQ183ZD/A", // iphone 14 pro gold 256gb
"MQ233ZD/A", // iphone 14 pro gold 512gb
];
const notify = async (location, part) => {
const target = await client.users.fetch("your discord id");
await target.send(
`ALERT: Part ${part} is available in ${location.plz}-${location.name}!`
);
process.exit(0);
};
/** @param {URLSearchParams} urlParams */
const sendRequest = (urlParams) => {
return fetch(`${TARGET_URL}?${urlParams.toString()}`);
};
const runJob = async (location, part) => {
console.log(`Checking ${part} for ${location.plz}-${location.name}`);
const urlParams = new URLSearchParams({
pl: true,
"mts.0": "regular",
location: location.plz,
"parts.0": part,
});
const res = await sendRequest(urlParams).then((res) => res.json());
const eligibleStores = res.body.content.pickupMessage.stores.filter(
(store) =>
store.storeName == location.name &&
store.partsAvailability[part]?.pickupDisplay !== "unavailable"
);
if (eligibleStores.length > 0) {
console.log("We got em, notifying");
await notify(location, part);
} else {
console.log("None available here...");
}
};
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
let locationIndex = 0;
let partIndex = 0;
setInterval(() => {
if (partIndex >= parts.length) {
locationIndex++;
partIndex = 0;
}
if (locationIndex >= locations.length) {
locationIndex = 0;
partIndex = 0;
}
runJob(locations[locationIndex], parts[partIndex]);
partIndex++;
}, 1000 * DELAY_IN_SECONDS);
});
client.login(
"your token"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment