From 4092a2e2f5f0568c438d7d875a08466077cefef8 Mon Sep 17 00:00:00 2001 From: Clarence Chan Date: Tue, 28 May 2024 12:35:53 -0400 Subject: [PATCH] Change strings to use template literals --- index.js | 23 +++++++++++------------ scrape.js | 18 ++++++------------ 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 65330346..0ea18708 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,7 @@ async function createICS() { // Distinguish between main card and prelims if this information has been // announced by the UFC, otherwise list all fights without categorizing if (event.fightCard.length) - description += event.fightCard.join("\n") + "\n"; + description = `${event.fightCard.join("\n")}\n`; if (event.mainCard.length) description += "Main Card\n--------------------\n" + @@ -51,23 +51,22 @@ async function createICS() { if (event.prelims.length) { let prelimsTime = new Date(parseInt(event.prelimsTime) * 1000); let hoursAgo = (date - prelimsTime) / 1000 / 60 / 60; - description += - `\nPrelims (${hoursAgo} hrs before Main)\n--------------------\n` + - event.prelims.join("\n") + - "\n"; + description += `\nPrelims (${hoursAgo} hrs before Main)\n--------------------\n${event.prelims.join( + "\n" + )}\n`; } if (event.earlyPrelims.length) { let earlyPrelimsTime = new Date( parseInt(event.earlyPrelimsTime) * 1000 ); let hoursAgo = (date - earlyPrelimsTime) / 1000 / 60 / 60; - description += - `\nEarly Prelims (${hoursAgo} hrs before Main)\n--------------------\n` + - event.earlyPrelims.join("\n") + - "\n"; + if (!hoursAgo) console.log("hoursAgo", date, earlyPrelimsTime); + description += `\nEarly Prelims (${hoursAgo} hrs before Main)\n--------------------\n${event.earlyPrelims.join( + "\n" + )}\n`; } - description += "\n" + event.url; - description += "\n\nAccurate as of " + dateTimestr; + description += `\n${event.url}`; + description += `\n\nAccurate as of ${dateTimestr}`; let location = event.location; let uid = event.url; @@ -87,7 +86,7 @@ async function createICS() { console.log(events); // Create UFC.ics - writeFileSync(`UFC.ics`, createEvents(events).value); + writeFileSync("UFC.ics", createEvents(events).value); } catch (error) { console.error(error); } diff --git a/scrape.js b/scrape.js index 1dd86e20..3c9c8e29 100644 --- a/scrape.js +++ b/scrape.js @@ -8,7 +8,7 @@ import { decode } from "html-entities"; * of URL strings of UFC events */ async function getEventLinks() { - let url = `https://www.ufc.com/events`; + let url = "https://www.ufc.com/events"; try { const response = await fetch(url); const text = await response.text(); @@ -17,7 +17,7 @@ async function getEventLinks() { // Extract the URLs of the relevant events from the DOM object let links = root.querySelectorAll(".c-card-event--result__headline"); links = links.map( - (html) => "https://www.ufc.com" + html.firstChild.getAttribute("href") + (html) => `https://www.ufc.com${html.firstChild.getAttribute("href")}` ); console.log("\nEvent links found:"); @@ -110,15 +110,9 @@ async function getDetailsFromEventLink(url) { ".c-listing-fight__ranks-row .js-listing-fight__corner-rank span" ); ranks = ranks.map((rank) => rank?.innerText); - let fightStr = - "• " + - red + - (ranks[0] ? " (" + ranks[0] + ")" : "") + - " vs. " + - blue + - (ranks[1] ? " (" + ranks[1] + ")" : "") + - " @" + - bout; + let redRankStr = ranks[0] ? ` (${ranks[0]})` : ""; + let blueRankStr = ranks[1] ? ` (${ranks[1]})` : ""; + let fightStr = `• ${red}${redRankStr} vs. ${blue}${blueRankStr} @${bout}`; // Deentitize HTML entities fightStr = decode(fightStr); @@ -168,7 +162,7 @@ async function getDetailsFromEventLink(url) { }; return details; } catch (error) { - throw new Error("Failed to retrieve event: " + url + "\n" + error); + throw new Error(`Failed to retrieve event: ${url}\n${error}`); } }