Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
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 Response.json(data)
return new Response(data, {
headers: {
"Content-Type": "text/plain; charset=utf-8",
// "Content-Type": "text/calendar; charset=utf-8",
// "Cache-Control": "public, max-age=1800",
},
})
}
export const spacexCalendar = async () => {
const data = await getLaunches()
if (!data) { throw null }
// console.log(data)
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) {
// console.log(launch)
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 (data.launches)
return cal.toString()
}
moe-spacexcalendar.web.val.run
September 8, 2024