import { email } from "https://esm.town/v/std/email?v=9";
import * as cheerio from "npm:cheerio";
const datePattern = /\b[A-Za-z]{3}\s\d{1,2}\b/;
export default async function(interval: Interval) {
const response = await fetch("https://www.sperrychalet.com/vacancy_s.html");
const body = await response.text();
const $ = cheerio.load(body);
const table = $("#availability");
if (table.length === 0) return;
const availableDays = [];
const unavailableDays = [];
table.find("tr").each((i, row) => {
if (i < 2) return;
$(row)
.find("td")
.each((j, td) => {
const tdText = $(td).text().trim();
const match = tdText.match(datePattern);
if (!match) {
return;
}
if (
!$(td).hasClass("novacancy")
) {
availableDays.push(match[0]);
} else {
unavailableDays.push(match[0]);
}
});
});
if (availableDays.length > 0) {
await email({ subject: "SPERRY IS AVAILABLE", text: JSON.stringify({ message: availableDays.join(", ") }) });
}
}