-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance race and sprint data processing with new utility functi…
…ons and components
- Loading branch information
Showing
8 changed files
with
390 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//import { db } from "@/db/index.js" | ||
import { generateRaceId, formatDriver, formatTeam } from "../utils.js" | ||
|
||
export const getSprintQualyResults = async (year, race, page) => { | ||
//const raceId = generateRaceId(race, year) | ||
const raceId = "qatar_2024" | ||
const raceNumber = 1251 // Refers to the race number of the grand prix, its the id of the f1 page | ||
const url = `https://www.formula1.com/en/results/${year}/races/${raceNumber}/${race}/sprint-qualifying` | ||
|
||
await page.goto(url) | ||
|
||
await page.waitForSelector( | ||
'table[class="f1-table f1-table-with-data w-full"]' | ||
) | ||
|
||
const results = await page.evaluate(() => { | ||
const table = document.querySelector( | ||
'table[class="f1-table f1-table-with-data w-full"]' | ||
) | ||
const rows = table?.querySelectorAll("tbody tr") | ||
const data = [] | ||
|
||
rows?.forEach((row) => { | ||
const columns = row.querySelectorAll("td p") | ||
const gridPosition = columns[0]?.innerText.split("\n")[0].trim() || null | ||
const driver = columns[2]?.innerText.trim().toLowerCase() || null | ||
const team = columns[3]?.innerText.trim().toLowerCase() || null | ||
const sq1 = columns[4]?.innerText.split("\n")[0].trim() || null | ||
const sq2 = columns[5]?.innerText.split("\n")[0].trim() || null | ||
const sq3 = columns[6]?.innerText.split("\n")[0].trim() || null | ||
|
||
data.push({ | ||
gridPosition, | ||
driver, | ||
team, | ||
sq1, | ||
sq2, | ||
sq3, | ||
}) | ||
}) | ||
|
||
return data | ||
}) | ||
|
||
const formattedResults = results.map((result) => { | ||
return { | ||
Race_ID: raceId, | ||
Driver_ID: formatDriver(result.driver), | ||
Team_ID: formatTeam(result.team, result.driver), | ||
Grid_Position: result.gridPosition, | ||
SQ1: result.sq1, | ||
SQ2: result.sq2, | ||
SQ3: result.sq3, | ||
} | ||
}) | ||
|
||
console.log(formattedResults) | ||
// ;async () => { | ||
// const sprintRaceResults = await mergedResults | ||
|
||
// if (Array.isArray(sprintRaceResults) && sprintRaceResults.length > 0) { | ||
// for (const result of sprintRaceResults) { | ||
// try { | ||
// await db | ||
// .insert(Sprint_Race) | ||
// .values({ | ||
// Race_ID: raceId, | ||
// Driver_ID: result.Driver_ID, | ||
// Team_ID: result.Team_ID, | ||
// Finishing_Position: result.Finishing_Position, | ||
// laps: result.laps, | ||
// Race_Time: result.Race_Time, | ||
// Points_Obtained: result.Points_Obtained, | ||
// Grid_Position: result.Grid_Position, | ||
// }) | ||
// .onConflictDoNothing() | ||
|
||
// console.log("Sprint Race inserted correctly") | ||
// } catch (error) { | ||
// console.error(error) | ||
// } | ||
// } | ||
// } | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//import { db } from "@/db/index.js" | ||
import { generateRaceId, formatDriver, formatTeam } from "../utils.js" | ||
|
||
export const getSprintRaceResults = async (year, race, page) => { | ||
//const raceId = generateRaceId(race, year) | ||
const raceId = "qatar_2024" | ||
const raceNumber = 1251 // Refers to the race number of the grand prix, its the id of the f1 page | ||
const url = `https://www.formula1.com/en/results/${year}/races/${raceNumber}/${race}/sprint-results` | ||
|
||
await page.goto(url) | ||
|
||
await page.waitForSelector( | ||
'table[class="f1-table f1-table-with-data w-full"]' | ||
) | ||
|
||
const results = await page.evaluate(() => { | ||
const table = document.querySelector( | ||
'table[class="f1-table f1-table-with-data w-full"]' | ||
) | ||
const rows = table?.querySelectorAll("tbody tr") | ||
const data = [] | ||
|
||
rows?.forEach((row) => { | ||
const columns = row.querySelectorAll("td p") | ||
const position = columns[0]?.innerText.split("\n")[0].trim() || null | ||
const driver = columns[2]?.innerText.trim().toLowerCase() || null | ||
const team = columns[3]?.innerText.trim().toLowerCase() || null | ||
const laps = columns[4]?.innerText.trim().toLowerCase() || null | ||
const raceTime = columns[5]?.innerText.split("\n")[0].trim() || null | ||
const points = columns[6]?.innerText.split("\n")[0].trim() || null | ||
|
||
data.push({ | ||
position, | ||
driver, | ||
team, | ||
laps, | ||
raceTime, | ||
points, | ||
}) | ||
}) | ||
|
||
return data | ||
}) | ||
|
||
// now we need to get the sprint grid position | ||
const sprintUrl = `https://www.formula1.com/en/results/${year}/races/${raceNumber}/${race}/sprint-grid` | ||
await page.goto(sprintUrl) | ||
|
||
await page.waitForSelector( | ||
'table[class="f1-table f1-table-with-data w-full"]' | ||
) | ||
|
||
const qualyResults = await page.evaluate(() => { | ||
const table = document.querySelector( | ||
'table[class="f1-table f1-table-with-data w-full"]' | ||
) | ||
const rows = table?.querySelectorAll("tbody tr") | ||
const data = [] | ||
|
||
rows?.forEach((row) => { | ||
const columns = row.querySelectorAll("td p") | ||
const gridPosition = columns[0]?.innerText.split("\n")[0].trim() || null | ||
const driver = columns[2]?.innerText.trim().toLowerCase() || null | ||
|
||
data.push({ | ||
gridPosition, | ||
driver, | ||
}) | ||
}) | ||
|
||
return data | ||
}) | ||
|
||
const sprintQualyResults = qualyResults.map((result) => { | ||
return { | ||
driverId: formatDriver(result.driver), | ||
gridPosition: result.gridPosition, | ||
} | ||
}) | ||
|
||
const formattedResults = results.map((result) => { | ||
return { | ||
Race_ID: raceId, | ||
Driver_ID: formatDriver(result.driver), | ||
Team_ID: formatTeam(result.team, result.driver), | ||
Finishing_Position: result.position, | ||
laps: result.laps, | ||
Race_Time: result.raceTime, | ||
Points_Obtained: parseInt(result.points), // Asumiendo que los puntos siempre son numéricos y en el formato "X" | ||
} | ||
}) | ||
|
||
// now, with the sprint grid and the sprint race results, we can merge them in one | ||
|
||
const mergedResults = formattedResults.map((result) => { | ||
// Find the matching sprint grid result for the driver | ||
const sprintQualy = sprintQualyResults.find( | ||
(qualy) => qualy.driverId === result.Driver_ID | ||
) | ||
|
||
// Return a new object with the merged data | ||
return { | ||
...result, // Keep all existing properties | ||
Grid_Position: sprintQualy?.gridPosition || null, // Add grid position if found, else null | ||
} | ||
}) | ||
|
||
console.log(mergedResults) | ||
// ;async () => { | ||
// const sprintRaceResults = await mergedResults | ||
|
||
// if (Array.isArray(sprintRaceResults) && sprintRaceResults.length > 0) { | ||
// for (const result of sprintRaceResults) { | ||
// try { | ||
// await db | ||
// .insert(Sprint_Race) | ||
// .values({ | ||
// Race_ID: raceId, | ||
// Driver_ID: result.Driver_ID, | ||
// Team_ID: result.Team_ID, | ||
// Finishing_Position: result.Finishing_Position, | ||
// laps: result.laps, | ||
// Race_Time: result.Race_Time, | ||
// Points_Obtained: result.Points_Obtained, | ||
// Grid_Position: result.Grid_Position, | ||
// }) | ||
// .onConflictDoNothing() | ||
|
||
// also, we need to update the standings with the new points | ||
|
||
// console.log("Sprint Race inserted correctly") | ||
// } catch (error) { | ||
// console.error(error) | ||
// } | ||
// } | ||
// } | ||
// } | ||
} |
Oops, something went wrong.