|
| 1 | +import { writeFile } from "fs/promises" |
| 2 | + |
| 3 | +/** |
| 4 | + * https://calendar.google.com/calendar/u/0/embed?src=linuxfoundation.org_ik79t9uuj2p32i3r203dgv5mo8@group.calendar.google.com |
| 5 | + */ |
| 6 | +const CALENDAR_ID = |
| 7 | + "linuxfoundation.org_ik79t9uuj2p32i3r203dgv5mo8@group.calendar.google.com" |
| 8 | +const API_KEY = process.env.GOOGLE_CALENDAR_API_KEY |
| 9 | +const OUTPUT_FILE = "upcoming_events.json" |
| 10 | + |
| 11 | +interface CalendarEvent { |
| 12 | + summary: string |
| 13 | + location?: string |
| 14 | + description?: string |
| 15 | + start: { |
| 16 | + dateTime?: string // For timed events |
| 17 | + date?: string // For all-day events |
| 18 | + } |
| 19 | + end: { |
| 20 | + dateTime?: string |
| 21 | + date?: string |
| 22 | + } |
| 23 | + htmlLink: string |
| 24 | +} |
| 25 | + |
| 26 | +interface EventsListResponse { |
| 27 | + items: CalendarEvent[] |
| 28 | +} |
| 29 | + |
| 30 | +async function fetchCalendarEvents() { |
| 31 | + if (!API_KEY) { |
| 32 | + console.error( |
| 33 | + "Error: GOOGLE_CALENDAR_API_KEY environment variable not set.", |
| 34 | + ) |
| 35 | + process.exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + const timeMin = new Date().toISOString() |
| 39 | + |
| 40 | + const apiUrl = |
| 41 | + "https://www.googleapis.com/calendar/v3/calendars/" + |
| 42 | + `${CALENDAR_ID}/events?key=${API_KEY}` + |
| 43 | + `&timeMin=${timeMin}` + |
| 44 | + "&singleEvents=true" + |
| 45 | + "&orderBy=startTime" + |
| 46 | + "&maxResults=20" // Fetch up to 20 upcoming events |
| 47 | + |
| 48 | + console.log(`\nFetching events for Calendar ID: ${CALENDAR_ID}`) |
| 49 | + console.log(`Filtering from: ${timeMin}\n`) |
| 50 | + |
| 51 | + try { |
| 52 | + const response = await fetch(apiUrl) |
| 53 | + |
| 54 | + if (!response.ok) { |
| 55 | + const errorBody = await response.json().catch(() => ({})) |
| 56 | + const errorMessage = errorBody.error?.message || response.statusText |
| 57 | + throw new Error( |
| 58 | + `API Request Failed: ${response.status} - ${errorMessage}`, |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + const data: EventsListResponse = await response.json() |
| 63 | + |
| 64 | + if (data.items.length === 0) { |
| 65 | + console.log("No upcoming events found.") |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + const jsonContent = JSON.stringify(data.items, null, 2) |
| 70 | + await writeFile(OUTPUT_FILE, jsonContent, "utf-8") |
| 71 | + |
| 72 | + console.log(`Successfully fetched ${data.items.length} events.`) |
| 73 | + console.log(`Data saved to ${OUTPUT_FILE}`) |
| 74 | + } catch (error) { |
| 75 | + if (error instanceof Error) { |
| 76 | + console.error( |
| 77 | + `\nAn error occurred while fetching the calendar: ${error.message}`, |
| 78 | + ) |
| 79 | + } else { |
| 80 | + console.error("\nAn unknown error occurred.", error) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fetchCalendarEvents() |
0 commit comments