Skip to content

Commit 9b8d00c

Browse files
committed
Fixed getTeamLineup method
1 parent 6936268 commit 9b8d00c

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/endpoints/endpoints.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ interface staticLinks {
22
drivers: string;
33
teams: string;
44
hallOfFame: string;
5+
teamsPoints: string;
56
}
67

78
interface dynamicLinks {
@@ -16,6 +17,7 @@ interface dynamicLinks {
1617
export const staticLinks: staticLinks = {
1718
drivers: "https://www.formula1.com/en/drivers.html",
1819
teams: "https://www.formula1.com/en/teams.html",
20+
teamsPoints: `https://www.formula1.com/en/results/${new Date().getFullYear()}/team`,
1921
hallOfFame: "https://www.formula1.com/en/drivers/hall-of-fame.html",
2022
};
2123

src/scraper/team-lineup.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,43 @@ import { isTeam } from "../types/types";
88
export const getTeamLineup = async (): Promise<isTeam[]> => {
99
try {
1010
let teams: isTeam[] = [];
11-
1211
const response = await axios(staticLinks.teams);
1312
const $ = cheerio.load(response.data);
1413

15-
$("a.group").each(function () {
16-
const name: string = $(this).find("div > div:nth-child(3) > div:nth-child(1) > span:nth-child(1)").text();
17-
const driver1_0: string = $(this).find("div > div:nth-child(5) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1)").text();
18-
const driver1_1: string = $(this).find("div > div:nth-child(5) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p:nth-child(2)").text();
19-
const driver2_0: string = $(this).find("div > div:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1)").text();
20-
const driver2_1: string = $(this).find("div > div:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > p:nth-child(2)").text();
14+
async function getTeamPoints(): Promise<Array<[string, number]>> {
15+
const response = await axios(staticLinks.teamsPoints);
16+
const $ = cheerio.load(response.data);
17+
18+
const teamPoints: Array<[string, number]> = [];
19+
$(".f1-table tbody tr").each(function () {
20+
const teamName = $(this).find("td:nth-child(2)").text().trim();
21+
const points = $(this).find("td:nth-child(3)").text().trim();
22+
teamPoints.push([teamName, Number(points)]);
23+
});
24+
return teamPoints;
25+
}
26+
const teamsPoints = await getTeamPoints();
27+
28+
$('a[data-f1rd-a7s-click="team_card_click"]').each(function () {
29+
const name: string = $(this).find("span:nth-child(3) > span > span:nth-child(1) > p").text().trim();
30+
const driver1_0: string = $(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();
31+
const driver1_1: string = $(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();
32+
const driver2_0: string = $(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();
33+
const driver2_1: string = $(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();
2134
const driver1 = driver1_0.concat(" ", driver1_1);
2235
const driver2 = driver2_0.concat(" ", driver2_1);
2336
const drivers: string[] = [driver1, driver2];
2437

25-
const carLogo: string | undefined = $(this).find("div > div:nth-child(3) > img:nth-child(2)").attr("src");
26-
const carImage: string | undefined = $(this).find("div > div:nth-child(6) > img:nth-child(1)").attr("src");
38+
const carLogo: string | undefined = $(this).find("span:nth-child(3) > span > span:nth-child(2) > img").attr("src");
39+
const carImage: string | undefined = $(this).find("span:nth-child(3) > span:nth-child(2) > img").attr("src");
2740

28-
const points: string | undefined = $(this).find("div> div:nth-child(1) > div:nth-child(2) > p:nth-child(1)").text();
41+
const foundedPoints = teamsPoints.find((teamPoints) => teamPoints[0] === name);
42+
const points: number | undefined = foundedPoints ? foundedPoints[1] : 0;
2943

3044
if (name.length !== 0) {
3145
const team: isTeam = {
3246
name,
33-
points: parseInt(points),
47+
points,
3448
drivers,
3549
carLogo,
3650
carImage,

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { getFastestLaps } from "./scraper/fastest-laps";
22
export { getDriverLineup } from "./scraper/driver-lineup";
3-
// export { getTeamLineup } from "./scraper/team-lineup";
3+
export { getTeamLineup } from "./scraper/team-lineup";
44
// export { getDriverStandings } from "./scraper/driver-standings";
55
// export { getConstructorStandings } from "./scraper/constructors-standings";
66
// export { getWorldChampions } from "./scraper/world-champions";

0 commit comments

Comments
 (0)