mirror of
https://github.com/clarencechaan/ufc-cal.git
synced 2026-10-19 00:38:30 -07:00
Implement getDetailsFromEventLink function in scrape.js
This commit is contained in:
1 parent
e061a82cbc
commit
bb1140f8fb
1 file changed
+67
@@ -1,4 +1,5 @@
|
||||
import { parse } from "node-html-parser";
|
||||
import { decode } from "html-entities";
|
||||
|
||||
async function getEventLinks() {
|
||||
let url = `https://www.ufc.com/events`;
|
||||
@@ -17,3 +18,69 @@ async function getEventLinks() {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getDetailsFromEventLink(url) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const text = await response.text();
|
||||
const root = parse(text);
|
||||
let name = root.querySelector("title").innerText.split(" | ")[0];
|
||||
let date = root
|
||||
.querySelector(".c-hero__headline-suffix")
|
||||
.getAttribute("data-timestamp");
|
||||
let location = root
|
||||
.querySelector(".field--name-venue")
|
||||
.innerText.replaceAll("\n", "")
|
||||
.replaceAll(" ", " ");
|
||||
let fightCard = [];
|
||||
let mainCard = root.querySelectorAll("#main-card .l-listing__item");
|
||||
let prelims = [];
|
||||
|
||||
function convertLiToStr(li) {
|
||||
let bout = li
|
||||
.querySelector(".c-listing-fight__class-text")
|
||||
.innerText.replace(" Bout", "");
|
||||
let red = li
|
||||
.querySelector(".c-listing-fight__corner-name--red")
|
||||
.innerText.replaceAll("\n", "")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
let blue = li
|
||||
.querySelector(".c-listing-fight__corner-name--blue")
|
||||
.innerText.replaceAll("\n", "")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
let ranks = li.querySelectorAll(
|
||||
".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 +
|
||||
"]";
|
||||
fightStr = decode(fightStr);
|
||||
return fightStr;
|
||||
}
|
||||
|
||||
if (mainCard.length) {
|
||||
mainCard = mainCard.map(convertLiToStr);
|
||||
prelims = root.querySelectorAll("#prelims-card .l-listing__item");
|
||||
prelims = prelims.map(convertLiToStr);
|
||||
} else {
|
||||
fightCard = root.querySelectorAll(
|
||||
".l-listing__group--bordered .l-listing__item"
|
||||
);
|
||||
fightCard = fightCard.map(convertLiToStr);
|
||||
}
|
||||
|
||||
let details = { name, date, location, fightCard, mainCard, prelims };
|
||||
return details;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user