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
import { fetch } from "https://esm.town/v/std/fetch";
export const getPoolTimes = (async () => {
const days = [
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
];
const cheerio = await import("npm:cheerio");
const response = await fetch(
"https://www.swanleisure.ie/timetables?type=pool",
);
const body = await response.text();
const $ = cheerio.load(body);
const timetable = [];
const tables = $(".tab-panels__panel");
for (let i = 7; i < 14; i++) {
const table = tables[i];
const dayOfWeek = $(table).attr("data-tab-id");
const tableElement = $(table).find("table.tab-panels__table");
const tableBody = tableElement.find("tbody");
const day = {
name: dayOfWeek,
timetable: [],
};
tableBody.find("tr").each((i, row) => {
const time = $(row).find("td:nth-child(1)").text().trim();
const activity = $(row).find("td:nth-child(2)").text().trim();
const adultsOnly =
$(row).find("td:nth-child(3)").text().trim().toUpperCase() === "Y";
if (time !== "" && activity !== "" && adultsOnly) {
day.timetable.push({ time, activity });
}
});
timetable.push(day);
}
const date = new Date();
const todayDayName = days[date.getDay()];
const todaysTimetable = timetable.filter((t) => t.name === todayDayName);
date.setDate(date.getDate() + 1);
const tomorrowDayName = days[date.getDay()];
const tomorrowsTimetable = timetable.filter((t) =>
t.name === tomorrowDayName
);
return {
today: todaysTimetable[0].timetable,
tomorrow: tomorrowsTimetable[0].timetable,
all: timetable,
};
});
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!
October 23, 2023