f1-api-node
Version:
A simple library written in typescript to fetch Formula-1 data
84 lines • 4.19 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.getTeamLineup = void 0;
const axios_1 = __importDefault(require("axios"));
const cheerio = __importStar(require("cheerio"));
const endpoints_1 = require("../endpoints/endpoints");
const getTeamLineup = async () => {
try {
let teams = [];
const response = await (0, axios_1.default)(endpoints_1.staticLinks.teams);
const $ = cheerio.load(response.data);
async function getTeamPoints() {
const response = await (0, axios_1.default)(endpoints_1.staticLinks.teamsPoints);
const $ = cheerio.load(response.data);
const teamPoints = [];
$(".f1-table tbody tr").each(function () {
const teamName = $(this).find("td:nth-child(2)").text().trim();
const points = $(this).find("td:nth-child(3)").text().trim();
teamPoints.push([teamName, Number(points)]);
});
return teamPoints;
}
const teamsPoints = await getTeamPoints();
$('a[data-f1rd-a7s-click="team_card_click"]').each(function () {
const name = $(this).find("span:nth-child(3) > span > span:nth-child(1) > p").text().trim();
const driver1_0 = $(this).find("span:nth-child(3) > span > span:nth-child(1) > span > span:nth-child(1) > span:nth-child(2) > span:nth-child(1)").text().trim();
const driver1_1 = $(this).find("span:nth-child(3) > span > span:nth-child(1) > span > span:nth-child(1) > span:nth-child(2) > span:nth-child(2)").text().trim();
const driver2_0 = $(this).find("span:nth-child(3) > span > span:nth-child(1) > span > span:nth-child(2) > span:nth-child(2) > span:nth-child(1)").text().trim();
const driver2_1 = $(this).find("span:nth-child(3) > span > span:nth-child(1) > span > span:nth-child(2) > span:nth-child(2) > span:nth-child(2)").text().trim();
const driver1 = driver1_0.concat(" ", driver1_1);
const driver2 = driver2_0.concat(" ", driver2_1);
const drivers = [driver1, driver2];
const carLogo = $(this).find("span:nth-child(3) > span > span:nth-child(2) > img").attr("src");
const carImage = $(this).find("span:nth-child(3) > span:nth-child(2) > img").attr("src");
const foundedPoints = teamsPoints.find((teamPoints) => teamPoints[0] === name);
const points = foundedPoints ? foundedPoints[1] : 0;
if (name.length !== 0) {
const team = {
name,
points,
drivers,
carLogo,
carImage,
};
teams.push(team);
}
});
if (teams.length === 0) {
throw new Error(" No data found");
}
return teams;
}
catch (error) {
throw new Error(error);
}
};
exports.getTeamLineup = getTeamLineup;
//# sourceMappingURL=team-lineup.js.map