Skip to content

Commit 0841763

Browse files
committed
scraping the list of drivers from hall of fame
1 parent ce0ba77 commit 0841763

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# F1 API
22

3-
The following scraper scrapes this given [website](https://www.formula1.com/en.html)
3+
The scraper scrapes this given https://www.formula1.com/
44

55
## Functions added
66

@@ -11,4 +11,10 @@ The following scraper scrapes this given [website](https://www.formula1.com/en.h
1111
Fetch the current list of F1 teams along with their information
1212

1313
- getDriverStandings
14-
Fetch F1 driver standings from points table
14+
Fetch F1 driver standings from points table
15+
16+
- getConstructorStandings
17+
Fetch Constructors standings from points table
18+
19+
- getWorldChampions
20+
Fetch all the world champions

src/endpoints/endpoints.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
interface staticLinks {
22
drivers: string
33
teams: string
4+
hallOfFame: string
45
}
56

67
interface dynamicLinks {
@@ -13,6 +14,7 @@ interface dynamicLinks {
1314
export const staticLinks: staticLinks = {
1415
drivers: "https://www.formula1.com/en/drivers.html",
1516
teams: "https://www.formula1.com/en/teams.html",
17+
hallOfFame: "https://www.formula1.com/en/drivers/hall-of-fame.html"
1618
}
1719

1820
export const dynamicLinks: dynamicLinks = {

src/scraper/world-champions.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import axios from "axios"
2+
import cheerio from "cheerio"
3+
4+
import { staticLinks } from "../endpoints/endpoints"
5+
6+
type hallOfFame = {
7+
name: string
8+
years: number[]
9+
}
10+
11+
export const getWorldChampions = (): Promise<hallOfFame[]> => {
12+
let worldChampions: any = []
13+
14+
return new Promise((resolve, reject) => {
15+
axios(staticLinks.hallOfFame)
16+
.then(response => {
17+
const html = response.data
18+
const $ = cheerio.load(html)
19+
20+
$('article').each(
21+
function () {
22+
const years: number[] = []
23+
24+
const name: string = $(this).find('section > h4').text().trim().split('-')[0]
25+
$(this).find('section > h4').text().trim().split('-')[1]
26+
.split(',').forEach(x => {
27+
if (x) {
28+
const year: number = parseInt(x)
29+
years.push(year)
30+
}
31+
})
32+
33+
if (name.length !== 0 && years.length !== 0) {
34+
const worldChampion: hallOfFame = {
35+
name,
36+
years
37+
}
38+
worldChampions.push(worldChampion)
39+
}
40+
}
41+
)
42+
resolve(worldChampions)
43+
})
44+
.catch(err => { reject(err) })
45+
})
46+
}

src/server.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@ import { getDriverData } from "./scraper/driver-data";
22
import { getTeamsData } from "./scraper/team-data";
33
import { getDriverStandings } from "./scraper/driver-standings";
44
import { getConstructorStandings } from './scraper/constructors-standings';
5-
6-
const printDriverData = async (): Promise<void> => {
7-
const f1Drivers = await getConstructorStandings(2003)
8-
console.log(f1Drivers)
9-
}
10-
11-
printDriverData()
5+
import { getWorldChampions } from './scraper/world-champions'
126

137
export {
148
getDriverData,
159
getTeamsData,
1610
getDriverStandings,
17-
getConstructorStandings
11+
getConstructorStandings,
12+
getWorldChampions
1813
}

0 commit comments

Comments
 (0)