Skip to content

Commit 17ee845

Browse files
committed
using async await instead of seperate new Promise
1 parent a2d4618 commit 17ee845

File tree

7 files changed

+133
-141
lines changed

7 files changed

+133
-141
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ npm i f1-api-node
3333
Fetch all the world champions
3434
_No arguments_
3535

36-
- **getRaceResults**
36+
- **getRaceResults**
3737
Fetch race results for all the grand prix in a given year.
3838
The function takes one argument: The year from which you want to extract race results.
3939

src/endpoints/endpoints.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ interface dynamicLinks {
1111
constructorStandings_2: string;
1212
results_1: string;
1313
results_2: string;
14+
fastestLap_1: string;
15+
fastestLap_2: string;
1416
}
1517

1618
export const staticLinks: staticLinks = {
@@ -26,4 +28,6 @@ export const dynamicLinks: dynamicLinks = {
2628
constructorStandings_2: "team.html",
2729
results_1: "https://www.formula1.com/en/results.html",
2830
results_2: "races.html",
31+
fastestLap_1: "https://www.formula1.com/en/results.html",
32+
fastestLap_2: "fastest-laps.html",
2933
};

src/scraper/driver-data.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,37 @@ import { staticLinks } from "../endpoints/endpoints";
55

66
import { isDriver } from "../types/types";
77

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

12-
return new Promise(async (resolve, reject) => {
13-
const response = await axios(staticLinks.drivers);
14-
const $ = cheerio.load(response.data);
12+
const response = await axios(staticLinks.drivers);
13+
const $ = cheerio.load(response.data);
1514

16-
$("fieldset").each(function () {
17-
const firstName: string = $(this).find(".container > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)").text().trim();
18-
const secondName: string = $(this).find(".container > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)").text().trim();
19-
const team: string = $(this).find(" p:nth-child(3)").text();
20-
const rank: number = parseInt($(this).find(".rank").text());
21-
const points: number = parseInt($(this).find(".points > div:nth-child(1)").text());
22-
const nationalityImage: string | undefined = $(this)
23-
.find(".container > .row.justify-content-between.align-items-center.listing-item--head > .col-xs-4.country-flag > picture:nth-child(1) > img:nth-child(2)")
24-
.attr("data-src");
25-
const driverImage: string | undefined = $(this).find(" div:nth-child(4) > picture:nth-child(1) > img:nth-child(2)").attr("data-src");
15+
$("fieldset").each(function () {
16+
const firstName: string = $(this).find(".container > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)").text().trim();
17+
const secondName: string = $(this).find(".container > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)").text().trim();
18+
const team: string = $(this).find(" p:nth-child(3)").text();
19+
const rank: number = parseInt($(this).find(".rank").text());
20+
const points: number = parseInt($(this).find(".points > div:nth-child(1)").text());
21+
const nationalityImage: string | undefined = $(this)
22+
.find(".container > .row.justify-content-between.align-items-center.listing-item--head > .col-xs-4.country-flag > picture:nth-child(1) > img:nth-child(2)")
23+
.attr("data-src");
24+
const driverImage: string | undefined = $(this).find(" div:nth-child(4) > picture:nth-child(1) > img:nth-child(2)").attr("data-src");
2625

27-
if (firstName.length !== 0 && secondName.length !== 0 && team.length !== 0 && !Number.isNaN(rank) && !Number.isNaN(points)) {
28-
const driver: isDriver = {
29-
name: firstName.concat(" ", secondName),
30-
team,
31-
rank,
32-
points,
33-
nationalityImage,
34-
driverImage,
35-
};
36-
drivers.push(driver);
37-
}
38-
});
39-
resolve(drivers);
26+
if (firstName.length !== 0 && secondName.length !== 0 && team.length !== 0 && !Number.isNaN(rank) && !Number.isNaN(points)) {
27+
const driver: isDriver = {
28+
name: firstName.concat(" ", secondName),
29+
team,
30+
rank,
31+
points,
32+
nationalityImage,
33+
driverImage,
34+
};
35+
drivers.push(driver);
36+
}
4037
});
38+
return drivers;
4139
} catch (error: any) {
4240
throw new Error(error);
4341
}

src/scraper/driver-standings.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,34 @@ import { dynamicLinks } from "../endpoints/endpoints";
55

66
import { isDriverStanding } from "../types/types";
77

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

12-
return new Promise(async (resolve, reject) => {
13-
const response = await axios(`${dynamicLinks.driverStandings_1}/${year}/${dynamicLinks.driverStandings_2}`);
14-
const $ = cheerio.load(response.data);
12+
const response = await axios(`${dynamicLinks.driverStandings_1}/${year}/${dynamicLinks.driverStandings_2}`);
13+
const $ = cheerio.load(response.data);
1514

16-
$("tr").each(function () {
17-
const position: number = parseInt($(this).find(" td:nth-child(2) ").text());
18-
const firstName: string = $(this).find(" a.dark.bold.ArchiveLink > span:nth-child(1)").text();
19-
const lastName: string = $(this).find(" a.dark.bold.ArchiveLink > span:nth-child(2)").text();
20-
const driver = firstName.concat(" ", lastName);
21-
const nationality: string = $(this).find(" td.dark.semi-bold.uppercase").text();
22-
const team: string = $(this).find("td:nth-child(5) > a.grey.semi-bold.uppercase.ArchiveLink").text();
23-
const points: number = parseInt($(this).find(" td:nth-child(6) ").text());
15+
$("tr").each(function () {
16+
const position: number = parseInt($(this).find(" td:nth-child(2) ").text());
17+
const firstName: string = $(this).find(" a.dark.bold.ArchiveLink > span:nth-child(1)").text();
18+
const lastName: string = $(this).find(" a.dark.bold.ArchiveLink > span:nth-child(2)").text();
19+
const driver = firstName.concat(" ", lastName);
20+
const nationality: string = $(this).find(" td.dark.semi-bold.uppercase").text();
21+
const team: string = $(this).find("td:nth-child(5) > a.grey.semi-bold.uppercase.ArchiveLink").text();
22+
const points: number = parseInt($(this).find(" td:nth-child(6) ").text());
2423

25-
if (!Number.isNaN(position) && !Number.isNaN(points) && driver.length !== 0 && nationality.length !== 0 && team.length !== 0) {
26-
const driverStanding: isDriverStanding = {
27-
position,
28-
driver,
29-
nationality,
30-
team,
31-
points,
32-
};
33-
driverStandings.push(driverStanding);
34-
}
35-
});
36-
resolve(driverStandings);
24+
if (!Number.isNaN(position) && !Number.isNaN(points) && driver.length !== 0 && nationality.length !== 0 && team.length !== 0) {
25+
const driverStanding: isDriverStanding = {
26+
position,
27+
driver,
28+
nationality,
29+
team,
30+
points,
31+
};
32+
driverStandings.push(driverStanding);
33+
}
3734
});
35+
return driverStandings;
3836
} catch (error: any) {
3937
throw new Error(error);
4038
}

src/scraper/race-results.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,34 @@ import { dynamicLinks } from "../endpoints/endpoints";
55

66
import { isRaceResult } from "../types/types";
77

8-
export const getRaceResults = (year: number): Promise<isRaceResult[]> => {
8+
export const getRaceResults = async (year: number): Promise<isRaceResult[]> => {
99
try {
1010
let raceResults: isRaceResult[] = [];
1111

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);
12+
const response = await axios(`${dynamicLinks.results_1}/${year}/${dynamicLinks.results_2}`);
13+
const $ = cheerio.load(response.data);
1514

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();
15+
$("tr").each(function () {
16+
const grandPrix: string = $(this).find("td:nth-child(2) > a:nth-child(1)").text().trim();
17+
const raceDate: string = $(this).find("td:nth-child(3)").text().trim();
18+
const winner: string = $(this).find("td:nth-child(4) > span:nth-child(2)").text().trim();
19+
const car: string = $(this).find("td:nth-child(5)").text().trim();
20+
const laps: number = parseInt($(this).find("td:nth-child(6)").text().trim());
21+
const time: string = $(this).find("td:nth-child(7)").text().trim();
2322

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-
});
23+
if ((grandPrix.length !== 0 && raceDate.length !== 0 && winner.length !== 0, car.length !== 0, !Number.isNaN(laps), time.length !== 0)) {
24+
const raceResult: isRaceResult = {
25+
grandPrix,
26+
date: new Date(raceDate),
27+
winner,
28+
car,
29+
laps,
30+
time,
31+
};
32+
raceResults.push(raceResult);
33+
}
3734
});
35+
return raceResults;
3836
} catch (error: any) {
3937
throw new Error(error);
4038
}

src/scraper/team-data.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,42 @@ import { staticLinks } from "../endpoints/endpoints";
55

66
import { isTeam } from "../types/types";
77

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

12-
return new Promise(async (resolve, reject) => {
13-
const response = await axios(staticLinks.teams);
14-
const $ = cheerio.load(response.data);
15-
16-
$("fieldset").each(function () {
17-
const name: string = $(this).find(" div:nth-child(1) > .listing-info > .name.f1-bold--m > .f1-color--black").text();
18-
const points: number = parseInt($(this).find(".points > div:nth-child(1)").text());
19-
const rank: number = parseInt($(this).find(".rank").text());
20-
21-
const driver1_0: string = $(this).find(" div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)").text();
22-
const driver1_1: string = $(this).find(" div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)").text();
23-
const driver2_0: string = $(this).find("div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(1)").text();
24-
const driver2_1: string = $(this).find("div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(2)").text();
25-
const driver1 = driver1_0.concat(" ", driver1_1);
26-
const driver2 = driver2_0.concat(" ", driver2_1);
27-
const drivers: string[] = [driver1, driver2];
28-
29-
const carLogo: string | undefined = $(this)
30-
.find("div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > picture:nth-child(1) > img:nth-child(2)")
31-
.attr("data-src");
32-
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");
33-
34-
if (name.length !== 0 && !Number.isNaN(points) && !Number.isNaN(rank)) {
35-
const team: isTeam = {
36-
name,
37-
drivers,
38-
points,
39-
rank,
40-
carLogo,
41-
carImage,
42-
};
43-
teams.push(team);
44-
}
45-
resolve(teams);
46-
});
12+
const response = await axios(staticLinks.teams);
13+
const $ = cheerio.load(response.data);
14+
15+
$("fieldset").each(function () {
16+
const name: string = $(this).find(" div:nth-child(1) > .listing-info > .name.f1-bold--m > .f1-color--black").text();
17+
const points: number = parseInt($(this).find(".points > div:nth-child(1)").text());
18+
const rank: number = parseInt($(this).find(".rank").text());
19+
20+
const driver1_0: string = $(this).find(" div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)").text();
21+
const driver1_1: string = $(this).find(" div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)").text();
22+
const driver2_0: string = $(this).find("div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(1)").text();
23+
const driver2_1: string = $(this).find("div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(2)").text();
24+
const driver1 = driver1_0.concat(" ", driver1_1);
25+
const driver2 = driver2_0.concat(" ", driver2_1);
26+
const drivers: string[] = [driver1, driver2];
27+
28+
const carLogo: string | undefined = $(this).find("div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > picture:nth-child(1) > img:nth-child(2)").attr("data-src");
29+
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");
30+
31+
if (name.length !== 0 && !Number.isNaN(points) && !Number.isNaN(rank)) {
32+
const team: isTeam = {
33+
name,
34+
drivers,
35+
points,
36+
rank,
37+
carLogo,
38+
carImage,
39+
};
40+
teams.push(team);
41+
}
4742
});
43+
return teams;
4844
} catch (error: any) {
4945
throw new Error(error);
5046
}

src/scraper/world-champions.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,39 @@ import { staticLinks } from "../endpoints/endpoints";
55

66
import { isHallOfFame } from "../types/types";
77

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

12-
return new Promise(async (resolve, reject) => {
13-
const response = await axios(staticLinks.hallOfFame);
14-
const $ = cheerio.load(response.data);
12+
const response = await axios(staticLinks.hallOfFame);
13+
const $ = cheerio.load(response.data);
1514

16-
$("article").each(function () {
17-
const years: number[] = [];
15+
$("article").each(function () {
16+
const years: number[] = [];
1817

19-
const name: string = $(this).find("section > h4").text().trim().split("-")[0];
20-
$(this)
21-
.find("section > h4")
22-
.text()
23-
.trim()
24-
.split("-")[1]
25-
.split(",")
26-
.forEach((x) => {
27-
if (x) {
28-
const year: number = parseInt(x);
29-
years.push(year);
30-
}
31-
});
18+
const name: string = $(this).find("section > h4").text().trim().split("-")[0];
19+
$(this)
20+
.find("section > h4")
21+
.text()
22+
.trim()
23+
.split("-")[1]
24+
.split(",")
25+
.forEach((x) => {
26+
if (x) {
27+
const year: number = parseInt(x);
28+
years.push(year);
29+
}
30+
});
3231

33-
if (name.length !== 0 && years.length !== 0) {
34-
const worldChampion: isHallOfFame = {
35-
name,
36-
years,
37-
};
38-
worldChampions.push(worldChampion);
39-
}
40-
});
41-
resolve(worldChampions);
32+
if (name.length !== 0 && years.length !== 0) {
33+
const worldChampion: isHallOfFame = {
34+
name,
35+
years,
36+
};
37+
worldChampions.push(worldChampion);
38+
}
4239
});
40+
return worldChampions;
4341
} catch (error: any) {
4442
throw new Error(error);
4543
}

0 commit comments

Comments
 (0)