This repository has been archived on 2022-08-24. You can view files and clone it, but cannot push or open issues or pull requests.
ergast-py/ergast_py/requester.py

299 lines
10 KiB
Python
Raw Normal View History

""" Requester class """
2022-06-01 15:31:52 +02:00
import json
from typing import Callable
2022-06-01 15:31:52 +02:00
import requests
2022-06-01 15:31:52 +02:00
from uritemplate import URITemplate
2022-06-05 20:46:37 +02:00
HOST = 'https://ergast.com/api'
SERIES = 'f1'
2022-06-01 15:31:52 +02:00
class Requester():
"""
Perform requests to the Ergast API
2022-06-01 15:31:52 +02:00
"""
def _get_race_results_params(self, param: dict) -> dict:
return {
"season": param["season"],
"round": param["round"],
"filters": {
"drivers": param["driver"],
"constructors": param["constructor"],
"grid": param["grid"],
"qualifying": param["qualifying"],
"sprint": param["sprint"],
"fastest": param["fastest"],
"circuits": param["circuit"],
"status": param["status"],
"results": param["result"],
"races": param["races"],
"seasons": param["seasons"],
},
"paging": {
"limit": param["limit"],
"offset": param["offset"],
}
}
def _get_race_results_criteria(self, params: dict, resource: str) -> dict:
criteria = []
for key, value in params["filters"].items():
if (key != resource and value is not None):
2022-06-01 15:31:52 +02:00
criteria.append(key)
criteria.append(value)
value = params["filters"][resource]
return {
"resource": resource,
"value": value,
"criteria": criteria
}
def _get_standings_params(self, param: dict) -> dict:
return {
"season": param["season"],
"round": param["round"],
"filters": {
"standing": param["standing"],
"drivers": param["driver"],
"constructors": param["constructor"]
},
"paging": {
"limit": param["limit"],
"offset": param["offset"],
}
}
def _get_standings_criteria(self, params: dict, resource: str) -> dict:
criteria = []
for key, value in params["filters"].items():
if (key != "standing" and value is not None):
2022-06-01 15:31:52 +02:00
criteria.append(key)
criteria.append(value)
value = params["filters"]["standing"]
return {
"resource": resource,
"value": value,
"criteria": criteria
}
def _get_laps_pit_stops_params(self, param: dict) -> dict:
return {
"season": param["season"],
"round": param["round"],
"filters": {
"pitstops": param["pit_stop"],
"laps": param["lap"],
"drivers": param["driver"],
},
"paging": {
"limit": param["limit"],
"offset": param["offset"],
}
}
def _get_laps_pit_stops_criteria(self, params: dict, resource: str) -> dict:
criteria = []
for key, value in params["filters"].items():
if (key != resource and value is not None):
2022-06-01 15:31:52 +02:00
criteria.append(key)
criteria.append(value)
value = params["filters"][resource]
return {
"resource": resource,
"value": value,
"criteria": criteria
}
2022-06-09 01:20:47 +02:00
def run_request(self, season, round_no, criteria, resource, value=None, limit=None,
offset=None) -> dict:
2022-06-09 01:20:47 +02:00
"""
Run a request against the API and return the JSON dictionary result
"""
2022-06-01 15:31:52 +02:00
url_tmpl = URITemplate('https://ergast.com/api{/series}{/season}{/round}'
'{/criteria*}{/resource}{/value}.json{?limit,offset}')
url = url_tmpl.expand(host=HOST, series=SERIES,
season=season, round=round_no,
2022-06-01 15:31:52 +02:00
criteria=criteria, resource=resource,
value=value, limit=limit, offset=offset)
2022-06-01 19:31:06 +02:00
response = requests.get(url)
if response.status_code == 200:
return json.loads(response.text)
raise Exception(f"Failed with status code {response.status_code}. Error: {response.reason}")
2022-06-01 15:31:52 +02:00
def _get_api_json(self, get_params: Callable, get_criteria: Callable,
resource: str, param: dict) -> dict:
params = get_params(param)
filters = get_criteria(params, resource)
2022-06-09 01:20:47 +02:00
return self.run_request(season=params["season"], round_no=params["round"],
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
2022-06-01 15:31:52 +02:00
#
# Race and Results
#
def get_circuits(self, param: dict) -> dict:
"""
Get the Circuits JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"circuits",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["CircuitTable"]["Circuits"]
2022-06-01 15:31:52 +02:00
def get_constructors(self, param: dict) -> dict:
"""
Get the Constructors JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"constructors",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["ConstructorTable"]["Constructors"]
2022-06-01 15:31:52 +02:00
def get_drivers(self, param: dict) -> dict:
"""
Get the Drivers JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"drivers",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["DriverTable"]["Drivers"]
2022-06-01 15:31:52 +02:00
def get_qualifying(self, param: dict) -> dict:
"""
Get the Qualifying JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"qualifying",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["RaceTable"]["Races"]
2022-06-01 15:31:52 +02:00
def get_sprints(self, param: dict) -> dict:
"""
Get the Sprints JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"sprint",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["RaceTable"]["Races"]
2022-06-01 15:31:52 +02:00
def get_results(self, param: dict) -> dict:
"""
Get the Results JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"results",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["RaceTable"]["Races"]
2022-06-01 15:31:52 +02:00
def get_races(self, param: dict) -> dict:
"""
Get the Races JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"races",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["RaceTable"]["Races"]
2022-06-01 15:31:52 +02:00
def get_seasons(self, param: dict) -> dict:
"""
Get the Seasons JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"seasons",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["SeasonTable"]["Seasons"]
2022-06-01 15:31:52 +02:00
def get_statuses(self, param: dict) -> dict:
"""
Get the Statuses JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_race_results_params,
self._get_race_results_criteria,
"status",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["StatusTable"]["Status"]
2022-06-01 15:31:52 +02:00
#
# Standings
#
def get_driver_standings(self, param: dict) -> dict:
"""
Get the Driver Standings JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_standings_params,
self._get_standings_criteria,
"driverStandings",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["StandingsTable"]["StandingsLists"]
2022-06-01 15:31:52 +02:00
def get_constructor_standings(self, param: dict) -> dict:
"""
Get the Constructor Standings JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_standings_params,
self._get_standings_criteria,
"constructorStandings",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["StandingsTable"]["StandingsLists"]
2022-06-01 15:31:52 +02:00
#
# Laps and Pit Stops
#
def get_laps(self, param: dict) -> dict:
"""
Get the Laps JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_laps_pit_stops_params,
self._get_laps_pit_stops_criteria,
"laps",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["RaceTable"]["Races"]
2022-06-01 15:31:52 +02:00
def get_pit_stops(self, param: dict) -> dict:
"""
Get the Pit Stops JSON from the Ergast API
"""
api_json = self._get_api_json(self._get_laps_pit_stops_params,
self._get_laps_pit_stops_criteria,
"pitstops",
param)
2022-06-01 15:31:52 +02:00
return api_json["MRData"]["RaceTable"]["Races"]