import { DOMParser, Node } from "https://esm.sh/linkedom@0.16.1";
import { email } from "https://esm.town/v/std/email?v=9";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=5";
function isHTMLElement(node: Node): node is HTMLElement {
return node.nodeType === Node.ELEMENT_NODE;
}
const URL = "https://www.barebonestiki.com/shop/swizzles";
const SEND_ONLY_WHEN_AVAILABLE = false;
export const webscrapeBareBonesTiki = async () => {
const html = await fetchText(URL);
const document = new DOMParser().parseFromString(html, "text/html");
const nodeList: NodeListOf<HTMLElement> = document.querySelectorAll(".grid-title");
const titleNodes = Array.from(nodeList).filter((node) => {
if (isHTMLElement(node)) {
return node.textContent?.trim() === "Sea Light Swizzles";
}
return false;
});
const parent = titleNodes[0]?.closest(".grid-meta-wrapper");
const status = parent.querySelector(".grid-meta-status").textContent.trim();
const isAvailable = status !== "SOLD OUT";
if (!SEND_ONLY_WHEN_AVAILABLE || isAvailable) {
await email({
subject: `Sea Light Swizzles: ${isAvailable ? "Available! 🐡" : "Not Available 🚫"}`,
html: `<a href="${URL}">Sea Light Swizzles</a> are ${isAvailable ? "available!" : "currently sold out."}`,
});
}
};