Skip to content

Commit ea64970

Browse files
committed
function to get race results added and name changed for types
1 parent 43ced8b commit ea64970

File tree

10 files changed

+80
-25
lines changed

10 files changed

+80
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"name": "Yash Kathe"
3131
},
3232
"license": "GPL-3.0",
33-
"bugs":"https://github.com/yashkathe/F1-API/issues",
33+
"bugs": "https://github.com/yashkathe/F1-API/issues",
3434
"devDependencies": {
3535
"@types/cheerio": "^0.22.31",
3636
"@types/jest": "^29.1.1",

src/endpoints/endpoints.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ interface dynamicLinks {
99
driverStandings_2: string;
1010
constructorStandings_1: string;
1111
constructorStandings_2: string;
12+
results_1: string;
13+
results_2: string;
1214
}
1315

1416
export const staticLinks: staticLinks = {
@@ -22,4 +24,6 @@ export const dynamicLinks: dynamicLinks = {
2224
driverStandings_2: "drivers.html",
2325
constructorStandings_1: "https://www.formula1.com/en/results.html",
2426
constructorStandings_2: "team.html",
27+
results_1: "https://www.formula1.com/en/results.html",
28+
results_2: "races.html",
2529
};

src/scraper/constructors-standings.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import cheerio from "cheerio";
33

44
import { dynamicLinks } from "../endpoints/endpoints";
55

6-
import { constructorStanding } from "../types/types";
6+
import { isConstructorStanding } from "../types/types";
77

8-
export const getConstructorStandings = async (year: number = new Date().getFullYear()): Promise<constructorStanding[]> => {
8+
export const getConstructorStandings = async (year: number = new Date().getFullYear()): Promise<isConstructorStanding[]> => {
99
try {
10-
let constructorStandings: constructorStanding[] = [];
10+
let constructorStandings: isConstructorStanding[] = [];
1111

1212
const response = await axios(`${dynamicLinks.constructorStandings_1}/${year}/${dynamicLinks.constructorStandings_2}`);
1313
const $ = cheerio.load(response.data);
@@ -18,7 +18,7 @@ export const getConstructorStandings = async (year: number = new Date().getFullY
1818
const points: number = parseInt($(this).find("td:nth-child(4)").text().trim());
1919

2020
if (!Number.isNaN(position) && team.length !== 0 && !Number.isNaN(points)) {
21-
const constructorStanding: constructorStanding = {
21+
const constructorStanding: isConstructorStanding = {
2222
position,
2323
team,
2424
points,

src/scraper/driver-data.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import cheerio from "cheerio";
33

44
import { staticLinks } from "../endpoints/endpoints";
55

6-
import { drivers } from "../types/types";
6+
import { isDriver } from "../types/types";
77

8-
export const getDriverData = (): Promise<drivers[]> => {
8+
export const getDriverData = (): Promise<isDriver[]> => {
99
try {
10-
let drivers: drivers[] = [];
10+
let drivers: isDriver[] = [];
1111

1212
return new Promise(async (resolve, reject) => {
1313
const response = await axios(staticLinks.drivers);
@@ -25,7 +25,7 @@ export const getDriverData = (): Promise<drivers[]> => {
2525
const driverImage: string | undefined = $(this).find(" div:nth-child(4) > picture:nth-child(1) > img:nth-child(2)").attr("data-src");
2626

2727
if (firstName.length !== 0 && secondName.length !== 0 && team.length !== 0 && !Number.isNaN(rank) && !Number.isNaN(points)) {
28-
const driver: drivers = {
28+
const driver: isDriver = {
2929
name: firstName.concat(" ", secondName),
3030
team,
3131
rank,

src/scraper/driver-standings.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import cheerio from "cheerio";
33

44
import { dynamicLinks } from "../endpoints/endpoints";
55

6-
import { driverStanding } from "../types/types";
6+
import { isDriverStanding } from "../types/types";
77

8-
export const getDriverStandings = (year: number = new Date().getFullYear()): Promise<driverStanding[]> => {
8+
export const getDriverStandings = (year: number = new Date().getFullYear()): Promise<isDriverStanding[]> => {
99
try {
10-
let driverStandings: driverStanding[] = [];
10+
let driverStandings: isDriverStanding[] = [];
1111

1212
return new Promise(async (resolve, reject) => {
1313
const response = await axios(`${dynamicLinks.driverStandings_1}/${year}/${dynamicLinks.driverStandings_2}`);
@@ -23,7 +23,7 @@ export const getDriverStandings = (year: number = new Date().getFullYear()): Pro
2323
const points: number = parseInt($(this).find(" td:nth-child(6) ").text());
2424

2525
if (!Number.isNaN(position) && !Number.isNaN(points) && driver.length !== 0 && nationality.length !== 0 && team.length !== 0) {
26-
const driverStanding: driverStanding = {
26+
const driverStanding: isDriverStanding = {
2727
position,
2828
driver,
2929
nationality,

src/scraper/race-results.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import axios from "axios";
2+
import cheerio from "cheerio";
3+
4+
import { dynamicLinks } from "../endpoints/endpoints";
5+
6+
import { isRaceResult } from "../types/types";
7+
8+
export const getRaceResults = (year: number): Promise<isRaceResult[]> => {
9+
try {
10+
let raceResults: isRaceResult[] = [];
11+
12+
return new Promise(async (resolve, reject) => {
13+
const response = await axios(`${dynamicLinks.results_1}/${year}/${dynamicLinks.results_2}`);
14+
const $ = cheerio.load(response.data);
15+
16+
$("tr").each(function () {
17+
const grandPrix: string = $(this).find("td:nth-child(2) > a:nth-child(1)").text().trim();
18+
const raceDate: string = $(this).find("td:nth-child(3)").text().trim();
19+
const winner: string = $(this).find("td:nth-child(4) > span:nth-child(2)").text().trim();
20+
const car: string = $(this).find("td:nth-child(5)").text().trim();
21+
const laps: number = parseInt($(this).find("td:nth-child(6)").text().trim());
22+
const time: string = $(this).find("td:nth-child(7)").text().trim();
23+
24+
if ((grandPrix.length !== 0 && raceDate.length !== 0 && winner.length !== 0, car.length !== 0, !Number.isNaN(laps), time.length !== 0)) {
25+
const raceResult: isRaceResult = {
26+
grandPrix,
27+
date: new Date(raceDate),
28+
winner,
29+
car,
30+
laps,
31+
time,
32+
};
33+
raceResults.push(raceResult);
34+
}
35+
resolve(raceResults);
36+
});
37+
});
38+
} catch (error: any) {
39+
throw new Error(error);
40+
}
41+
};

src/scraper/team-data.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import cheerio from "cheerio";
33

44
import { staticLinks } from "../endpoints/endpoints";
55

6-
import { teams } from "../types/types";
6+
import { isTeam } from "../types/types";
77

8-
export const getTeamsData = (): Promise<teams[]> => {
8+
export const getTeamsData = (): Promise<isTeam[]> => {
99
try {
10-
let teams: teams[] = [];
10+
let teams: isTeam[] = [];
1111

1212
return new Promise(async (resolve, reject) => {
1313
const response = await axios(staticLinks.teams);
@@ -32,7 +32,7 @@ export const getTeamsData = (): Promise<teams[]> => {
3232
const carImage: string | undefined = $(this).find("div:nth-child(1) > div:nth-child(4) > picture:nth-child(1) > img:nth-child(5)").attr("data-src");
3333

3434
if (name.length !== 0 && !Number.isNaN(points) && !Number.isNaN(rank)) {
35-
const team: teams = {
35+
const team: isTeam = {
3636
name,
3737
drivers,
3838
points,

src/scraper/world-champions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import cheerio from "cheerio";
33

44
import { staticLinks } from "../endpoints/endpoints";
55

6-
import { hallOfFame } from "../types/types";
6+
import { isHallOfFame } from "../types/types";
77

8-
export const getWorldChampions = (): Promise<hallOfFame[]> => {
8+
export const getWorldChampions = (): Promise<isHallOfFame[]> => {
99
try {
1010
let worldChampions: any = [];
1111

@@ -31,7 +31,7 @@ export const getWorldChampions = (): Promise<hallOfFame[]> => {
3131
});
3232

3333
if (name.length !== 0 && years.length !== 0) {
34-
const worldChampion: hallOfFame = {
34+
const worldChampion: isHallOfFame = {
3535
name,
3636
years,
3737
};

src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { getTeamsData } from "./scraper/team-data";
33
export { getDriverStandings } from "./scraper/driver-standings";
44
export { getConstructorStandings } from "./scraper/constructors-standings";
55
export { getWorldChampions } from "./scraper/world-champions";
6+
export { getRaceResults } from "./scraper/race-results";

src/types/types.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export type constructorStanding = {
1+
export type isConstructorStanding = {
22
position: number;
33
team: string;
44
points: number;
55
};
66

7-
export type drivers = {
7+
export type isDriver = {
88
name: string;
99
team: string;
1010
rank: number;
@@ -13,15 +13,15 @@ export type drivers = {
1313
driverImage: string | undefined;
1414
};
1515

16-
export type driverStanding = {
16+
export type isDriverStanding = {
1717
position: number;
1818
driver: string;
1919
nationality: string;
2020
team: string;
2121
points: number;
2222
};
2323

24-
export type teams = {
24+
export type isTeam = {
2525
name: string;
2626
drivers: string[];
2727
points: number;
@@ -30,7 +30,16 @@ export type teams = {
3030
carImage: string | undefined;
3131
};
3232

33-
export type hallOfFame = {
33+
export type isHallOfFame = {
3434
name: string;
3535
years: number[];
3636
};
37+
38+
export type isRaceResult = {
39+
grandPrix: string;
40+
date: Date;
41+
winner: string;
42+
car: string;
43+
laps: number;
44+
time: string;
45+
};

0 commit comments

Comments
 (0)