Skip to content

Commit 34152ce

Browse files
committed
Fix getRaceSchedule method
1 parent eea1c46 commit 34152ce

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

src/scraper/driver-standings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const getDriverStandings = async (year: number = new Date().getFullYear()
1616
const position: number = parseInt($(this).find("td:nth-child(1) > p:nth-child(1)").text().trim());
1717
const driver_1: string = $(this).find("td:nth-child(2) > p:nth-child(1) > a:nth-child(1) > span:nth-child(1)").text().trim();
1818
const driver_2: string = $(this).find("td:nth-child(2) > p:nth-child(1) > a:nth-child(1) > span:nth-child(2)").text().trim();
19-
const driver: string = driver_1.concat(" ", driver_2.slice(0, driver_2.length - 3));
19+
const driver: string = driver_1.concat(" ", driver_2.slice(0, driver_2.length - 3)).trim();
2020
const nationality: string = $(this).find(" td:nth-child(3) > p:nth-child(1)").text();
2121
const team: string = $(this).find("td:nth-child(4) > p:nth-child(1) > a:nth-child(1)").text();
2222
const points: number = parseInt($(this).find(" td:nth-child(5) > p:nth-child(1) ").text());

src/scraper/race-schedule.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,40 @@ export const getRaceSchedule = async (year: number = new Date().getFullYear()):
1212
const response = await axios(`${dynamicLinks.raceSchedule}/${year}.html`);
1313
const $ = cheerio.load(response.data);
1414

15-
$("a.outline-offset-4").each(function () {
16-
const round: string = $(this).find("a.outline-offset-4 > fieldset:nth-child(1) > legend:nth-child(1) > p:nth-child(1)").text().trim();
17-
const dateDays1: string = $(this).find("div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1) > span:nth-child(1)").text().trim();
18-
const dateDays2: string = $(this).find("div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > p:nth-child(1) > span:nth-child(1)").text().trim();
19-
const dateMonth1: string = $(this).find("div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > p:nth-child(1) > span:nth-child(1)").text().trim();
20-
const dateMonth2: string = $(this)
21-
.find("div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > span:nth-child(1) > span:nth-child(1)")
22-
.text()
23-
.trim();
24-
const raceCountry: string = $(this)
25-
.find("a.outline-offset-4 > fieldset:nth-child(1) > div:nth-child(2) > div:nth-child(3) > div:nth-child(1) > p:nth-child(1)")
26-
.text()
27-
.trim();
28-
const eventTitle: string = $(this)
29-
.find("a.outline-offset-4 > fieldset:nth-child(1) > div:nth-child(2) > div:nth-child(3) > div:nth-child(2) > p:nth-child(1)")
30-
.text()
31-
.trim();
32-
33-
const dateDays = dateDays1 || dateDays2;
34-
const dateMonth = dateMonth1 || dateMonth2;
15+
$("a.group").each(function () {
16+
let round: string = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(1)").text().trim();
17+
let date: string = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(2)").contents().get()[1];
18+
date = $(date).text();
19+
20+
let eventTitle: string = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(3)").text().trim();
21+
let raceCountry: string = $(this).find("div:nth-child(1) > div:nth-child(1) > span:nth-child(2) > p:nth-child(2)").text().trim();
22+
23+
//Semantics differ for the next race
24+
if (round === "") {
25+
round = $(this).find("div:nth-child(1) > div:nth-child(2) > span:nth-child(1) > span:nth-child(1)").text().trim();
26+
}
27+
28+
//Yes four differ semantics
29+
const dateGetPath: string[] = [
30+
"div:nth-child(1) > div:nth-child(1) > span:nth-child(4)",
31+
"div:nth-child(1) > div:nth-child(1) > div:nth-child(4) > span:nth-child(1)",
32+
"div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > span:nth-child(1)",
33+
];
34+
let i = 0;
35+
while (date === "") {
36+
date = $(this).find(dateGetPath[i++]).text().trim();
37+
}
38+
39+
//Semantics differ for the next race
40+
if (eventTitle === "") eventTitle = $(this).find("div:nth-child(1) > div:nth-child(2) > span:nth-child(3)").text().trim();
41+
42+
//Semantics differ for the next race
43+
if (raceCountry === "") raceCountry = $(this).find("div:nth-child(1) > div:nth-child(2) > span:nth-child(2) > p:nth-child(2)").text().trim();
3544

3645
if (round !== undefined && raceCountry !== undefined && eventTitle !== undefined) {
3746
const singleSchedule = {
3847
round,
39-
date: dateDays.concat("", dateMonth),
48+
date: date.replaceAll(" ", ""),
4049
raceCountry,
4150
eventTitle,
4251
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
1010
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
1111
/* Language and Environment */
12-
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
12+
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
1313
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
1414
// "jsx": "preserve", /* Specify what JSX code is generated. */
1515
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */

0 commit comments

Comments
 (0)