UNPKG

f1-api-node

Version:

A simple library written in typescript to fetch Formula-1 data

70 lines 3.35 kB
"use strict"; 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.getDriverStandings = void 0; const axios_1 = __importDefault(require("axios")); const cheerio = __importStar(require("cheerio")); const endpoints_1 = require("../endpoints/endpoints"); const getDriverStandings = async (year = new Date().getFullYear()) => { try { let driverStandings = []; const response = await (0, axios_1.default)(`${endpoints_1.dynamicLinks.rootLink}/${year}/${endpoints_1.dynamicLinks.driverStandings}`); const $ = cheerio.load(response.data); $(".f1-table > tbody:nth-child(2) > tr").each(function () { const position = parseInt($(this).find("td:nth-child(1) > p:nth-child(1)").text().trim()); const driver_1 = $(this).find("td:nth-child(2) > p:nth-child(1) > a:nth-child(1) > span:nth-child(1)").text().trim(); const driver_2 = $(this).find("td:nth-child(2) > p:nth-child(1) > a:nth-child(1) > span:nth-child(2)").text().trim(); const driver = driver_1 .concat(" ", driver_2.slice(0, driver_2.length - 3)) .trim() .replace(/\u00a0/g, " "); const nationality = $(this).find(" td:nth-child(3) > p:nth-child(1)").text(); const team = $(this).find("td:nth-child(4) > p:nth-child(1) > a:nth-child(1)").text(); const points = parseInt($(this).find(" td:nth-child(5) > p:nth-child(1) ").text()); if (!Number.isNaN(position) && !Number.isNaN(points) && driver.length !== 0 && nationality.length !== 0 && team.length !== 0) { const driverStanding = { position, driver, nationality, team, points, }; driverStandings.push(driverStanding); } }); if (driverStandings.length === 0) { throw new Error(" No data found"); } return driverStandings; } catch (error) { throw new Error(error); } }; exports.getDriverStandings = getDriverStandings; //# sourceMappingURL=driver-standings.js.map