Skip to content

Instantly share code, notes, and snippets.

@barryf
Created December 10, 2023 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryf/4ae9ea613dcb821d0ff34840d1d8477a to your computer and use it in GitHub Desktop.
Save barryf/4ae9ea613dcb821d0ff34840d1d8477a to your computer and use it in GitHub Desktop.
Val to scrape North Herts Waste website for upcoming bin days and return an ICS
// Make request to this URL with ?id={your house ID}
// Returns ICS
export const northHertsWaste = async (req) => {
const cheerio = await import("npm:cheerio");
const ics = await import("npm:ics");
const searchParams = new URL(req.url).searchParams;
const id = searchParams.get("id");
const url = `https://north-herts.whitespacews.com/north/Address/Details/${id}`;
const response = await fetch(url);
const html = await response.text();
const $ = cheerio.load(html, { xmlMode: false });
const scripts = $("script").toArray();
function extractCollections(scriptIndex) {
const text = $(scripts[scriptIndex]).text();
const dates = [...text.matchAll(/arry\.push\("([0-9\-]*)"/g)];
const collections = dates.map((date) => date[1]);
return collections;
}
const collections = [
["Refuse", extractCollections(3)],
["Recyling", extractCollections(5)],
["Garden", extractCollections(4)],
["Food", extractCollections(2)],
];
const dates = {};
collections.forEach(([title, datesArray]) => {
datesArray.forEach((date) => {
if (dates.hasOwnProperty(date)) {
dates[date] += `, ${title}`;
} else {
dates[date] = title;
}
});
});
console.log(dates);
const calName = "Bins";
const events = [];
Object.keys(dates).forEach((date) => {
const bins = dates[date];
const startDate = new Date(Date.parse(date));
const start = [
startDate.getFullYear(),
startDate.getMonth() + 1,
startDate.getDate(),
];
events.push({
calName,
title: `Bins: ${bins}`,
start,
});
let reminderDate = startDate;
reminderDate.setDate(reminderDate.getDate() - 1);
const reminder = [
reminderDate.getFullYear(),
reminderDate.getMonth() + 1,
reminderDate.getDate(),
19,
0,
];
events.push({
calName,
title: `Put out bins: ${bins}`,
start: reminder,
});
});
console.log(events);
const { error, value } = ics.createEvents(events);
console.log(error, value);
return new Response(value, {
headers: {
"Content-Type": "text/calendar",
},
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment