Created
December 10, 2023 14:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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