From 226ec8dabacd973a76e61b12a29136337dc15883 Mon Sep 17 00:00:00 2001 From: Clarence Chan Date: Tue, 28 Jan 2025 20:09:30 -0500 Subject: [PATCH] Clean up helper functions --- src/index.ts | 6 +++--- src/scrape.ts | 39 +++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6af7a564..b5a60d3e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import * as fs from "fs"; import { createEvents, DateArray, EventAttributes } from "ics"; /** - * Extracts the details of recent and near-future UFC events, then creates an + * Extracts the details of recent and upcoming UFC events, then creates an * ICS file named "UFC.ics" in the current directory containing these events */ async function createICS() { @@ -25,7 +25,7 @@ async function createICS() { } } -function formatEventForCalendar(event: UFCEvent) { +function formatEventForCalendar(event: UFCEvent): EventAttributes { const date = new Date(parseInt(event.date) * 1000); const start: DateArray = [ date.getFullYear(), @@ -84,7 +84,7 @@ function formatEventForCalendar(event: UFCEvent) { const uid = event.url.href; const calName = "UFC"; - const calendarEvent: EventAttributes = { + const calendarEvent = { start, duration, title, diff --git a/src/scrape.ts b/src/scrape.ts index 36d487b8..5636148e 100644 --- a/src/scrape.ts +++ b/src/scrape.ts @@ -11,7 +11,22 @@ async function getEventURLs() { new URL("https://www.ufc.com/events?page=1"), ]; - async function getEventLinksFromUrl(url: URL) { + try { + const eventURLs = ( + await Promise.all(pageURLs.map(getEventURLsFromPageURL)) + ).flat(); + + console.log("\nEvent URLs found:"); + console.log(eventURLs); + return eventURLs; + } catch (error) { + console.error(error); + throw new Error("Failed to retrieve event URLs"); + } +} + +async function getEventURLsFromPageURL(url: URL) { + try { const response = await fetch(url); const text = await response.text(); const root = parse(text); @@ -29,25 +44,15 @@ async function getEventURLs() { ) ); - return eventURLs; - } - - try { - const eventURLs = ( - await Promise.all(pageURLs.map(getEventLinksFromUrl)) - ).flat(); - - console.log("\nEvent URLs found:"); - console.log(eventURLs); return eventURLs; } catch (error) { console.error(error); + throw new Error("Failed to retrieve event URLs from page URL"); } } /** - * Helper function to return the output string of a fight given its HTML - * element list item + * Returns the output string of a fight given its HTML element list item */ function convertLiToStr(li: HTMLElement) { const bout = li.querySelector(".c-listing-fight__class-text")?.textContent; @@ -112,7 +117,7 @@ function convertLiToStr(li: HTMLElement) { /** * Returns the fight card details of a UFC event given its URL */ -async function getDetailsFromEventLink(url: URL) { +async function getDetailsFromEventURL(url: URL) { try { const response = await fetch(url); const text = await response.text(); @@ -201,6 +206,7 @@ async function getDetailsFromEventLink(url: URL) { }; return details; } catch (error) { + console.error(error); throw new Error(`Failed to retrieve event: ${url.href}\n${error}`); } } @@ -212,13 +218,14 @@ async function getAllDetailedEvents() { try { const eventURLs = await getEventURLs(); - // Convert each URL to the details of the corresponding UFC event + // Get UFC events from URLs const detailedEvents = await Promise.all( - eventURLs?.map(getDetailsFromEventLink) ?? [] + eventURLs?.map(getDetailsFromEventURL) ); return detailedEvents; } catch (error) { console.error(error); + throw new Error("Failed to retrieve all events"); } }