mirror of
https://github.com/clarencechaan/ufc-cal.git
synced 2026-10-19 00:38:30 -07:00
Clean up helper functions
This commit is contained in:
1 parent
997aee6171
commit
226ec8daba
2 files changed
+26
-19
No files matched your search
+3
-3
@@ -3,7 +3,7 @@ import * as fs from "fs";
|
|||||||
import { createEvents, DateArray, EventAttributes } from "ics";
|
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
|
* ICS file named "UFC.ics" in the current directory containing these events
|
||||||
*/
|
*/
|
||||||
async function createICS() {
|
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 date = new Date(parseInt(event.date) * 1000);
|
||||||
const start: DateArray = [
|
const start: DateArray = [
|
||||||
date.getFullYear(),
|
date.getFullYear(),
|
||||||
@@ -84,7 +84,7 @@ function formatEventForCalendar(event: UFCEvent) {
|
|||||||
const uid = event.url.href;
|
const uid = event.url.href;
|
||||||
const calName = "UFC";
|
const calName = "UFC";
|
||||||
|
|
||||||
const calendarEvent: EventAttributes = {
|
const calendarEvent = {
|
||||||
start,
|
start,
|
||||||
duration,
|
duration,
|
||||||
title,
|
title,
|
||||||
|
|||||||
+23
-16
@@ -11,7 +11,22 @@ async function getEventURLs() {
|
|||||||
new URL("https://www.ufc.com/events?page=1"),
|
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 response = await fetch(url);
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
const root = parse(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;
|
return eventURLs;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(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
|
* Returns the output string of a fight given its HTML element list item
|
||||||
* element list item
|
|
||||||
*/
|
*/
|
||||||
function convertLiToStr(li: HTMLElement) {
|
function convertLiToStr(li: HTMLElement) {
|
||||||
const bout = li.querySelector(".c-listing-fight__class-text")?.textContent;
|
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
|
* Returns the fight card details of a UFC event given its URL
|
||||||
*/
|
*/
|
||||||
async function getDetailsFromEventLink(url: URL) {
|
async function getDetailsFromEventURL(url: URL) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
@@ -201,6 +206,7 @@ async function getDetailsFromEventLink(url: URL) {
|
|||||||
};
|
};
|
||||||
return details;
|
return details;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
throw new Error(`Failed to retrieve event: ${url.href}\n${error}`);
|
throw new Error(`Failed to retrieve event: ${url.href}\n${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,13 +218,14 @@ async function getAllDetailedEvents() {
|
|||||||
try {
|
try {
|
||||||
const eventURLs = await getEventURLs();
|
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(
|
const detailedEvents = await Promise.all(
|
||||||
eventURLs?.map(getDetailsFromEventLink) ?? []
|
eventURLs?.map(getDetailsFromEventURL)
|
||||||
);
|
);
|
||||||
return detailedEvents;
|
return detailedEvents;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
throw new Error("Failed to retrieve all events");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user