Skip to content

Commit 78f1a4d

Browse files
committed
scraping constructors standings
1 parent 4deff26 commit 78f1a4d

File tree

9 files changed

+7440
-2437
lines changed

9 files changed

+7440
-2437
lines changed

package-lock.json

Lines changed: 7250 additions & 2303 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
{
2-
"name": "f1-api",
3-
"version": "0.1.0",
4-
"description": "",
5-
"main": "server.js",
6-
"scripts": {
7-
"start": "nodemon '/home/katheyash/WEB SCRAPING/F1-api/dist/server.js'"
8-
},
9-
"keywords": [],
10-
"author": "",
11-
"license": "ISC",
12-
"devDependencies": {
13-
"nodemon": "^2.0.20",
14-
"typescript": "^4.8.4"
15-
},
16-
"dependencies": {
17-
"@types/cheerio": "^0.22.31",
18-
"@types/express": "^4.17.14",
19-
"axios": "^0.27.2",
20-
"cheerio": "^1.0.0-rc.12",
21-
"express": "^4.18.1"
22-
}
23-
}
2+
"name": "f1-api",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "server.js",
6+
"files": [
7+
"dist"
8+
],
9+
"scripts": {
10+
"start": "nodemon './dist/server.js'",
11+
"test": "jest"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"@types/jest": "^29.1.1",
18+
"jest": "^29.1.2",
19+
"nodemon": "^2.0.20",
20+
"ts-jest": "^29.0.3",
21+
"typescript": "^4.8.4"
22+
},
23+
"dependencies": {
24+
"@types/cheerio": "^0.22.31",
25+
"axios": "^0.27.2",
26+
"cheerio": "^1.0.0-rc.12"
27+
}
28+
}
File renamed without changes.

src/scraper/constructors-standings.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import axios from "axios"
2+
import cheerio from "cheerio"
3+
4+
import { dynamicLinks } from "../endpoints/endpoints"
5+
6+
interface constructorStanding {
7+
position: number
8+
team: string
9+
points: number
10+
}
11+
12+
export const getConstructorStandings = (year: number): Promise<constructorStanding[]> => {
13+
14+
let constructorStandings: constructorStanding[] = []
15+
16+
return new Promise((resolve, reject) => {
17+
axios(`${dynamicLinks.constructorStandings_1}/${year}/${dynamicLinks.constructorStandings_2}`)
18+
.then(
19+
response => {
20+
const html = response.data
21+
const $ = cheerio.load(html)
22+
23+
$('tr').each(
24+
function () {
25+
const position: number = parseInt($(this).find('td:nth-child(2)').text().trim())
26+
const team: string = $(this).find('td:nth-child(3)').text().trim()
27+
const points: number = parseInt($(this).find('td:nth-child(4)').text().trim())
28+
29+
if (position !== NaN && team.length !== 0 && points !== NaN) {
30+
const constructorStanding: constructorStanding = {
31+
position,
32+
team,
33+
points
34+
}
35+
constructorStandings.push(constructorStanding)
36+
}
37+
})
38+
resolve(constructorStandings)
39+
return Promise.all(constructorStandings)
40+
})
41+
.catch(err => { reject(err) })
42+
})
43+
44+
}

src/scraper/driver-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from 'axios'
22
import cheerio from 'cheerio'
33

4-
import { staticLinks } from "../links/links";
4+
import { staticLinks } from "../endpoints/endpoints";
55

66
interface drivers {
77
name: string

src/scraper/driver-standings.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios from "axios"
22
import cheerio from "cheerio"
33

4-
import { dynamicLinks } from "../links/links"
4+
import { dynamicLinks } from "../endpoints/endpoints"
55

66
interface driverStanding {
77
position: number
88
driver: string
99
nationality: string
10-
car: string
10+
team: string
1111
points: number
1212
}
1313

@@ -21,7 +21,7 @@ export const getDriverStandings = (year: number): Promise<driverStanding[]> => {
2121
response => {
2222
const html = response.data
2323
const $ = cheerio.load(html)
24-
//
24+
2525
$('tr').each(
2626
function () {
2727
const position: number = parseInt($(this).find(' td:nth-child(2) ').text())
@@ -31,15 +31,15 @@ export const getDriverStandings = (year: number): Promise<driverStanding[]> => {
3131

3232
const driver = firstName.concat(" ", lastName)
3333
const nationality: string = $(this).find(' td.dark.semi-bold.uppercase').text()
34-
const car: string = $(this).find('td:nth-child(5) > a.grey.semi-bold.uppercase.ArchiveLink').text()
34+
const team: string = $(this).find('td:nth-child(5) > a.grey.semi-bold.uppercase.ArchiveLink').text()
3535
const points: number = parseInt($(this).find(' td:nth-child(6) ').text())
3636

37-
if (position !== NaN && points !== NaN && driver.length !== 0 && nationality.length !== 0 && car.length !== 0) {
37+
if (position !== NaN && points !== NaN && driver.length !== 0 && nationality.length !== 0 && team.length !== 0) {
3838
const driverStanding: driverStanding = {
3939
position,
4040
driver,
4141
nationality,
42-
car,
42+
team,
4343
points
4444
}
4545
driverStandings.push(driverStanding)

src/scraper/team-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from "axios";
22
import cheerio from "cheerio"
33

4-
import { staticLinks } from "../links/links";
4+
import { staticLinks } from "../endpoints/endpoints";
55

66
interface teams {
77
name: string

src/server.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { getDriverData } from "./scraper/driver-data";
22
import { getTeamsData } from "./scraper/team-data";
33
import { getDriverStandings } from "./scraper/driver-standings";
4+
import { getConstructorStandings } from './scraper/constructors-standings';
45

5-
export { getDriverData, getTeamsData, getDriverStandings }
6+
const printDriverData = async (): Promise<void> => {
7+
const f1Drivers = await getConstructorStandings(2003)
8+
console.log(f1Drivers)
9+
}
10+
11+
printDriverData()
12+
13+
export {
14+
getDriverData,
15+
getTeamsData,
16+
getDriverStandings,
17+
getConstructorStandings
18+
}

0 commit comments

Comments
 (0)