Readme

A val that emails you when the Sperry Chalet in Glacier National Park has open vacancies

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
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) => {
// Skip the first two rows
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(", ") }) });
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 12, 2024