f1-api-node
Version:
A simple library written in typescript to fetch Formula-1 data
86 lines • 4.2 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRaceSchedule = void 0;
const axios_1 = __importDefault(require("axios"));
const cheerio = __importStar(require("cheerio"));
const endpoints_1 = require("../endpoints/endpoints");
const getRaceSchedule = async (year = new Date().getFullYear()) => {
try {
let raceSchedule = [];
const response = await (0, axios_1.default)(`${endpoints_1.dynamicLinks.raceSchedule}/${year}.html`);
const $ = cheerio.load(response.data);
$("a.group").each(function () {
let round = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(1)").text().trim();
let date = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(2)").contents().get()[1];
date = $(date).text();
let eventTitle = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(3)").text().trim();
let raceCountry = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(2) > p:nth-child(2)").text().trim();
if (round === "") {
round = $(this).find("div:nth-child(1) > div:nth-child(2) > span:nth-child(1) > span:nth-child(1)").text().trim();
}
const dateGetPath = [
"div:nth-child(1) > div:nth-child(1) > span:nth-child(4)",
"div:nth-child(1) > div:nth-child(1) > div:nth-child(4) > span:nth-child(1)",
"div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > span:nth-child(1)",
];
let i = 0;
while (date === "") {
date = $(this).find(dateGetPath[i++]).text().trim();
}
if (eventTitle === "")
eventTitle = $(this).find("div:nth-child(1) > div:nth-child(2) > span:nth-child(3)").text().trim();
if (raceCountry === "")
raceCountry = $(this).find("div:nth-child(1) > div:nth-child(2) > span:nth-child(2) > p:nth-child(2)").text().trim();
date = date.replaceAll(" ", "");
if (date.length > 8) {
const dateParts = [date.slice(0, 2), date.slice(2, 5), date.slice(6, 8), date.slice(8, 13)];
date = `${dateParts[0]}-${dateParts[2]}${dateParts[1]}-${dateParts[3]}`;
}
if (round !== undefined && raceCountry !== undefined && eventTitle !== undefined) {
const singleSchedule = {
round,
date,
raceCountry,
eventTitle,
};
raceSchedule.push(singleSchedule);
}
});
if (raceSchedule.length === 0) {
throw new Error(" No data found");
}
return raceSchedule;
}
catch (error) {
throw new Error(error);
}
};
exports.getRaceSchedule = getRaceSchedule;
(0, exports.getRaceSchedule)(2024);
//# sourceMappingURL=race-schedule.js.map