UNPKG

f1-api-node

Version:

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

92 lines 5.53 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDriverLineup = void 0; const axios_1 = __importDefault(require("axios")); const cheerio = __importStar(require("cheerio")); const endpoints_1 = require("../endpoints/endpoints"); const getDriverLineup = () => __awaiter(void 0, void 0, void 0, function* () { try { let drivers = []; const response = yield (0, axios_1.default)(endpoints_1.staticLinks.drivers); const $ = cheerio.load(response.data); $("a.focus-visible\\:outline-0").each(function () { const firstName = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1)").text().trim(); const secondName = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > p:nth-child(2)").text().trim(); const team = $(this).find("div:nth-child(1) > div:nth-child(1) > p:nth-child(5)").text(); const rank = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1)").text().trim(); const nationalityImage = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > img:nth-child(2)").attr("src"); const driverImage = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(6) > img:nth-child(2)").attr("src"); if (firstName.length !== 0 && secondName.length !== 0 && team.length !== 0 && (rank === null || rank === void 0 ? void 0 : rank.length) !== 0 && (nationalityImage === null || nationalityImage === void 0 ? void 0 : nationalityImage.length) !== 0 && (driverImage === null || driverImage === void 0 ? void 0 : driverImage.length) !== 0) { const driver = { name: firstName.concat(" ", secondName), team, rank, nationalityImage, driverImage, }; drivers.push(driver); } }); $("a.group").each(function () { const firstName = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1)").text().trim(); const secondName = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p:nth-child(2)").text().trim(); const team = $(this).find("div:nth-child(1) > div:nth-child(1) > p:nth-child(3)").text(); const nationalityImage = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > img:nth-child(2)").attr("src"); const driverImage = $(this).find("div:nth-child(1) > div:nth-child(1) > div:nth-child(4) > img:nth-child(2)").attr("src"); if (firstName.length !== 0 && secondName.length !== 0 && team.length !== 0 && (nationalityImage === null || nationalityImage === void 0 ? void 0 : nationalityImage.length) !== 0 && (driverImage === null || driverImage === void 0 ? void 0 : driverImage.length) !== 0) { const driver = { name: firstName.concat(" ", secondName), team, rank: undefined, nationalityImage, driverImage, }; } }); if (drivers.length === 0) { throw new Error("No data found"); } return drivers; } catch (error) { throw new Error(error); } }); exports.getDriverLineup = getDriverLineup; (0, exports.getDriverLineup)(); //# sourceMappingURL=driver-lineup.js.map