import { fetch } from "https://esm.town/v/std/fetch";
export const dsnyEventsFeed = (async () => {
const ics = await import("npm:ics");
const events = await fetch(
"https://a827-donatenyc.nyc.gov/DSNYApi/api/Events/GetAllByBorough?borough=",
)
.then((r) => r.json());
const converted = events.map((evt) => {
const dp = evt.FromDateTime.split(/[\/\s:]+/);
return {
start: [
dp[2],
dp[0],
dp[1],
+dp[3] + 4 + (dp[6] === "AM" ? 0 : 12),
dp[4],
]
.map((n) => parseInt(n, 10)),
title: evt.EventName,
description: evt.Description,
location: evt.Street + " " + evt.City + " " + evt.State + " " + evt.Zip,
url: `https://${evt.Website}`,
busyStatus: "BUSY",
organizer: { name: evt.Name, email: evt.EmailAddress },
attendees: [],
};
});
const { error, value } = ics.createEvents(converted);
return new Response(
value || JSON.stringify(error),
{
headers: {
"Content-Type": "text/calendar",
},
},
);
});