API/Scrape: Scrape starting grid data
This commit is contained in:
@ -1,14 +1,21 @@
|
|||||||
import {
|
import {
|
||||||
fetch_scraped_driverstandings,
|
fetch_scraped_driverstandings,
|
||||||
fetch_scraped_raceresults,
|
fetch_scraped_raceresults,
|
||||||
|
fetch_scraped_startinggrids,
|
||||||
fetch_scraped_teamstandings,
|
fetch_scraped_teamstandings,
|
||||||
} from "$lib/fetch";
|
} from "$lib/fetch";
|
||||||
import { pb } from "$lib/pocketbase";
|
import { pb } from "$lib/pocketbase";
|
||||||
import type { ScrapedDriverStanding, ScrapedRaceResult, ScrapedTeamStanding } from "$lib/schema";
|
import type {
|
||||||
|
ScrapedDriverStanding,
|
||||||
|
ScrapedRaceResult,
|
||||||
|
ScrapedStartingGrid,
|
||||||
|
ScrapedTeamStanding,
|
||||||
|
} from "$lib/schema";
|
||||||
import {
|
import {
|
||||||
scrape_driver_standings,
|
scrape_driver_standings,
|
||||||
scrape_race_links,
|
scrape_race_links,
|
||||||
scrape_race_results,
|
scrape_race_results,
|
||||||
|
scrape_starting_grids,
|
||||||
scrape_team_standings,
|
scrape_team_standings,
|
||||||
} from "$lib/server/scrape";
|
} from "$lib/server/scrape";
|
||||||
import type { RequestHandler } from "./$types";
|
import type { RequestHandler } from "./$types";
|
||||||
@ -24,12 +31,27 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
|
|
||||||
// Obtain the results for each race
|
// Obtain the results for each race
|
||||||
const racelinks: string[] = await scrape_race_links();
|
const racelinks: string[] = await scrape_race_links();
|
||||||
|
const startinggrids: ScrapedStartingGrid[] = await scrape_starting_grids(racelinks);
|
||||||
const raceresults: ScrapedRaceResult[] = await scrape_race_results(racelinks);
|
const raceresults: ScrapedRaceResult[] = await scrape_race_results(racelinks);
|
||||||
const driverstandings: ScrapedDriverStanding[] = await scrape_driver_standings();
|
const driverstandings: ScrapedDriverStanding[] = await scrape_driver_standings();
|
||||||
const teamstandings: ScrapedTeamStanding[] = await scrape_team_standings();
|
const teamstandings: ScrapedTeamStanding[] = await scrape_team_standings();
|
||||||
|
|
||||||
// Clear existing PocketBase data
|
// Clear existing PocketBase data
|
||||||
|
// TODO: Do I really have to fetch everything just to delete it???
|
||||||
let deleted: number = 0;
|
let deleted: number = 0;
|
||||||
|
const scraped_startinggrids: ScrapedStartingGrid[] = await fetch_scraped_startinggrids(fetch);
|
||||||
|
for (const grid of scraped_startinggrids) {
|
||||||
|
try {
|
||||||
|
await pb.collection("scraped_startinggrids").delete(grid.id);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
return new Response(); // TODO: Return error
|
||||||
|
}
|
||||||
|
deleted++;
|
||||||
|
}
|
||||||
|
console.log(`Deleted ${deleted}/${scraped_startinggrids.length} starting grids`);
|
||||||
|
|
||||||
|
deleted = 0;
|
||||||
const scraped_raceresults: ScrapedRaceResult[] = await fetch_scraped_raceresults(fetch);
|
const scraped_raceresults: ScrapedRaceResult[] = await fetch_scraped_raceresults(fetch);
|
||||||
for (const result of scraped_raceresults) {
|
for (const result of scraped_raceresults) {
|
||||||
try {
|
try {
|
||||||
@ -71,6 +93,23 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
|
|
||||||
// Submit new data to PocketBase
|
// Submit new data to PocketBase
|
||||||
let submissions: number = 0;
|
let submissions: number = 0;
|
||||||
|
for (const grid of startinggrids) {
|
||||||
|
try {
|
||||||
|
// TODO: Authenticate this
|
||||||
|
await pb.collection("scraped_startinggrids").create(grid);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Error occured while submitting scraped data to PocketBase:");
|
||||||
|
console.log(e);
|
||||||
|
console.log("Error occured for this starting grid:");
|
||||||
|
console.log(grid);
|
||||||
|
console.log("Aborting submissions...");
|
||||||
|
return new Response(); // TODO: Return error
|
||||||
|
}
|
||||||
|
submissions++;
|
||||||
|
}
|
||||||
|
console.log(`Submitted ${submissions}/${startinggrids.length} starting grids.`);
|
||||||
|
|
||||||
|
submissions = 0;
|
||||||
for (const result of raceresults) {
|
for (const result of raceresults) {
|
||||||
try {
|
try {
|
||||||
// TODO: Authenticate this
|
// TODO: Authenticate this
|
||||||
|
Reference in New Issue
Block a user