Skip to content

Commit 6580192

Browse files
committed
Skip existing events unless optional --force flag
1 parent 9a3d7fc commit 6580192

1 file changed

Lines changed: 45 additions & 18 deletions

File tree

scripts/update-events.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
* Events are deduplicated by api_id and sorted chronologically (newest first).
1010
*
1111
* Usage:
12-
* bun run scripts/update-events.ts
12+
* bun run scripts/update-events.ts # Only fetch new events
13+
* bun run scripts/update-events.ts --force # Re-fetch all events
1314
*/
1415

16+
// CLI Flags //
17+
18+
const FORCE_MODE = process.argv.includes("--force")
19+
1520
import { readFileSync, writeFileSync } from "fs"
1621
import { join } from "path"
1722
import { marked } from "marked"
@@ -173,7 +178,10 @@ function transformApiEventToLumaEvent(apiEvent: LumaApiEvent): LumaEvent {
173178
}
174179
}
175180

176-
async function fetchAllEvents(apiKey: string): Promise<Map<string, LumaEvent>> {
181+
async function fetchAllEvents(
182+
apiKey: string,
183+
existingEventIds: Set<string>
184+
): Promise<Map<string, LumaEvent>> {
177185
const eventMap = new Map<string, LumaEvent>()
178186
const eventIds: string[] = []
179187
let paginationCursor: string | undefined
@@ -225,26 +233,38 @@ async function fetchAllEvents(apiKey: string): Promise<Map<string, LumaEvent>> {
225233
paginationCursor = data.has_more ? data.next_cursor : undefined
226234
} while (paginationCursor)
227235

228-
// Now fetch full details for each event (includes description_html)
229-
console.log(`\nFetching full details for ${eventIds.length} events...`)
236+
// Filter out existing events unless force mode
237+
const idsToFetch = FORCE_MODE ? eventIds : eventIds.filter((id) => !existingEventIds.has(id))
230238

231-
for (let i = 0; i < eventIds.length; i++) {
232-
const eventId = eventIds[i]
233-
process.stdout.write(` Fetching event ${i + 1}/${eventIds.length}...\r`)
239+
const skippedCount = eventIds.length - idsToFetch.length
240+
if (skippedCount > 0) {
241+
console.log(`\nSkipping ${skippedCount} existing events (use --force to re-fetch)`)
242+
}
234243

235-
const eventDetails = await fetchEventDetails(apiKey, eventId)
236-
if (eventDetails) {
237-
const lumaEvent = transformApiEventToLumaEvent(eventDetails)
238-
eventMap.set(eventId, lumaEvent)
239-
}
244+
// Now fetch full details for new events (includes description_md)
245+
if (idsToFetch.length === 0) {
246+
console.log("\nNo new events to fetch")
247+
} else {
248+
console.log(`\nFetching full details for ${idsToFetch.length} events...`)
249+
250+
for (let i = 0; i < idsToFetch.length; i++) {
251+
const eventId = idsToFetch[i]
252+
process.stdout.write(` Fetching event ${i + 1}/${idsToFetch.length}...\r`)
253+
254+
const eventDetails = await fetchEventDetails(apiKey, eventId)
255+
if (eventDetails) {
256+
const lumaEvent = transformApiEventToLumaEvent(eventDetails)
257+
eventMap.set(eventId, lumaEvent)
258+
}
240259

241-
// Small delay to avoid rate limiting
242-
if (i < eventIds.length - 1) {
243-
await new Promise((resolve) => setTimeout(resolve, 100))
260+
// Small delay to avoid rate limiting
261+
if (i < idsToFetch.length - 1) {
262+
await new Promise((resolve) => setTimeout(resolve, 100))
263+
}
244264
}
245-
}
246265

247-
console.log(` Fetched details for ${eventMap.size} events `)
266+
console.log(` Fetched details for ${eventMap.size} events `)
267+
}
248268

249269
return eventMap
250270
}
@@ -289,6 +309,10 @@ function synchronizeEvents(
289309
async function main() {
290310
console.log("=== Luma Events Sync ===\n")
291311

312+
if (FORCE_MODE) {
313+
console.log("🔄 Force mode: re-fetching all events\n")
314+
}
315+
292316
const apiKey = process.env.LUMA_API_KEY
293317
if (!apiKey) {
294318
console.error("❌ LUMA_API_KEY environment variable is required")
@@ -307,8 +331,11 @@ async function main() {
307331
console.log("No existing events file, starting fresh\n")
308332
}
309333

334+
// Build set of existing event IDs for quick lookup
335+
const existingEventIds = new Set(existingEvents.map((e) => e.api_id))
336+
310337
// Fetch from API
311-
const fetchedEvents = await fetchAllEvents(apiKey)
338+
const fetchedEvents = await fetchAllEvents(apiKey, existingEventIds)
312339
console.log(`\nFetched ${fetchedEvents.size} unique events from API`)
313340

314341
// Synchronize

0 commit comments

Comments
 (0)