import { getLaunches } from "https://esm.town/v/moe/spacexapi"
import ical from "npm:ical-generator"
import moment from "npm:moment-timezone"
export default async function(req: Request): Promise<Response> {
const data = await spacexCalendar()
return new Response(data, {
headers: {
"Content-Type": "text/plain; charset=utf-8",
},
})
}
export const spacexCalendar = async () => {
const data = await getLaunches()
if (!data) { throw null }
data.launches = data.launches.filter(l => !!l.date)
const timezone = "UTC"
const cal = ical({ domain: "spacex.page", name: "SpaceX Launches" }).timezone(timezone)
for (let launch of data.launches) {
const event = cal.createEvent({
start: moment.tz(launch.date, timezone),
end: moment.tz(launch.date, timezone).add(1, "hour"),
timezone: timezone,
summary: "🚀 " + (launch.payloadIcon ? launch.payloadIcon + " " : "") + launch.payload + " • " + launch.customer,
location: launch.type + " • " + launch.site + " • " + launch.orbit,
description: launch.note,
organizer: "SpaceX <hello@spacex.com>",
transparency: "TRANSPARENT",
})
const alarm = event.createAlarm({ type: "audio", trigger: 1800 })
}
return cal.toString()
}