mirror of
https://github.com/clarencechaan/ufc-cal.git
synced 2026-10-19 00:38:30 -07:00
Implement updateEventsFile function in api.js
This commit is contained in:
1 parent
f299b1ff44
commit
426097ec7d
1 file changed
+50
-1
@@ -1,4 +1,4 @@
|
|||||||
import { writeFile } from "fs/promises";
|
import { writeFile, readFile } from "fs/promises";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the details of a UFC event given its event ID and using the UFC API
|
* Returns the details of a UFC event given its event ID and using the UFC API
|
||||||
@@ -68,6 +68,8 @@ async function initializeEvents() {
|
|||||||
Date.now() - 1000 * 60 * 60 * 24 * 90
|
Date.now() - 1000 * 60 * 60 * 24 * 90
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log("Creating events.json");
|
||||||
|
|
||||||
// Write the fetched events to events.json
|
// Write the fetched events to events.json
|
||||||
fetchedEvents = JSON.stringify(fetchedEvents, null, 2);
|
fetchedEvents = JSON.stringify(fetchedEvents, null, 2);
|
||||||
writeFile("./events.json", fetchedEvents);
|
writeFile("./events.json", fetchedEvents);
|
||||||
@@ -75,3 +77,50 @@ async function initializeEvents() {
|
|||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update upcoming events in events.json, and add newly found events to events.json
|
||||||
|
*
|
||||||
|
* @returns {Promise<void>} - A Promise that resolves when events.json is updated
|
||||||
|
*/
|
||||||
|
async function updateEventsFile() {
|
||||||
|
try {
|
||||||
|
let events = JSON.parse(await readFile("./events.json"));
|
||||||
|
|
||||||
|
// Update upcoming events
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
// Skip past events
|
||||||
|
if (new Date(events[i].LiveEventDetail.StartTime) < Date.now()) continue;
|
||||||
|
|
||||||
|
console.log("Updating event", events[i].LiveEventDetail.EventId);
|
||||||
|
events[i] = await fetchEvent(events[i].LiveEventDetail.EventId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Starting from the latest event ID previously found, find and add any
|
||||||
|
// new events
|
||||||
|
let eventId = events[0].LiveEventDetail.EventId;
|
||||||
|
let event;
|
||||||
|
do {
|
||||||
|
eventId++;
|
||||||
|
|
||||||
|
event = await fetchEvent(eventId);
|
||||||
|
|
||||||
|
// Only filter in UFC events
|
||||||
|
if (
|
||||||
|
event.LiveEventDetail.EventId &&
|
||||||
|
event.LiveEventDetail.Organization.OrganizationId === 1
|
||||||
|
) {
|
||||||
|
console.log("Found new event", event.LiveEventDetail.EventId);
|
||||||
|
events.unshift(event);
|
||||||
|
}
|
||||||
|
} while (event?.LiveEventDetail.EventId);
|
||||||
|
|
||||||
|
console.log("Updating events.json");
|
||||||
|
|
||||||
|
// Write the newly added and updated events to events.json
|
||||||
|
events = JSON.stringify(events, null, 2);
|
||||||
|
writeFile("./events.json", events);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user