diff --git a/DataAnalyser.py b/DataAnalyser.py index ca7e1405c881293fe2bd08eab4a30b6496b266d6..a05ede2a759f42087aeea26f0c0642feb250d501 100644 --- a/DataAnalyser.py +++ b/DataAnalyser.py @@ -1,11 +1,8 @@ import re -import string -from typing import Iterator import pandas as pandas -import json as json from fastf1.core import Session, Lap, Laps, DataNotLoadedError -from bs4 import BeautifulSoup, PageElement, NavigableString, Tag +from bs4 import BeautifulSoup from DataHandler import DataHandler @@ -14,8 +11,8 @@ class DataAnalyser(DataHandler): """ Analyses sessions by extrapolating existing or new data from them. - Any method of this class must be given a Session object or a list thereof. If the method does not require such an - object, it should not be part of this class. + Any public method of this class must be given a Session, Event or Lap object or a list thereof. If the method does not + require such an object, it should not be part of this class. """ @@ -122,7 +119,12 @@ class DataAnalyser(DataHandler): # ===== Weather ===== - def getWeatherFromHtml(self, rawHtml): + def extractWeatherFromHtml(self, rawHtml): + """ + Extract an events weather conditions from its respective wikipedia page. + :param rawHtml: HTML content of the wikipedia page from which to extract. + :return: The weather conditions of the event as specified in the wikipedia page's infobox. + """ parsedHtml = BeautifulSoup(rawHtml, features="html.parser") tableRows = parsedHtml.find_all("tr") # Get all table rows on wiki page for tableRow in tableRows: diff --git a/DataImporter.py b/DataImporter.py index b021da41b9d65c291f4c0791100e2c8a90358370..325ad35b7146a5e8e863032c5ce6a751ca20f42f 100644 --- a/DataImporter.py +++ b/DataImporter.py @@ -17,24 +17,33 @@ class DataImporter(DataHandler, ABC): """ Imports/loads sessions as specified by SessionIdentifiers and/or certain criteria such as rain. - Any method of this class must be given a SessionIdentifier, a time or a time period by which to select + Any public method of this class must be given an Event, Session, SessionIdentifier, time or time period by which to select sessions. If the method does not require one of these, it should not be part of this class. """ - def importWeatherFromWiki(self, event: Event | SessionIdentifier): + def fetchEventWeather(self, event: Event | SessionIdentifier): + """ + Fetch session weather from the wikipedia entry for the specified event. + :param event: The event or identifier for any session of the event to fetch weather for. + :return: Weather conditions as string + """ if isinstance(event, SessionIdentifier): event = self.importEvent(event.year, event.event) - wikiHtml = self.importWikiHtml(event) analyser = DataAnalyser() - weather = analyser.getWeatherFromHtml(wikiHtml) + weather = analyser.extractWeatherFromHtml(wikiHtml) return weather def importWikiHtml(self, event: Event): + """ + Fetch the HTML contents of the wikipedia page for the event given. + :param event: Event whose wikipedia page to fetch. + :return: HTML content of the wikipedia page as a string. + """ apiRootUrl: string = "https://en.wikipedia.org/wiki/" grandPrixName: string = f"{event.year} {event["EventName"]}" uri: string = re.sub(" ", "_", grandPrixName) @@ -43,11 +52,7 @@ class DataImporter(DataHandler, ABC): if not response.ok: raise ConnectionError(f"Could not fetch wikipedia article with URL {url}") return response.text - # URL example to get 2024 Sao Paulo GP: https://en.wikipedia.org/w/api.php?action=query&titles=2024_S%C3%A3o_Paulo_Grand_Prix&prop=extracts&format=json&exintro=1 - - - - + # URL example: https://en.wikipedia.org/w/api.php?action=query&titles=2024_S%C3%A3o_Paulo_Grand_Prix&prop=extracts&format=json&exintro=1 def importEvent(self, year: int, event: int | str): return fastf1.get_event(year, event) diff --git a/main.py b/main.py index e9d8b82442d1e1d87a18671f3fe854663b8582a4..a15573d17ad6eddd3413448f3a8080a2d5b163ad 100644 --- a/main.py +++ b/main.py @@ -37,7 +37,7 @@ class Main: def testNewRainRaceFetching(self): dataHandler: Main.DataHandlingPackage = Main.DataHandlingPackage() sessionIdentifier: SessionIdentifier = SessionIdentifier(2024, "Brazil", "R") - weather = dataHandler.importer.importWeatherFromWiki(sessionIdentifier) + weather = dataHandler.importer.fetchEventWeather(sessionIdentifier) print(weather)