F1 circuit layouts with year-by-year SVGs — manually traced track variations
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

42 行
1.6KB

  1. from typing import Optional, TYPE_CHECKING
  2. from fastf1.ergast import Ergast
  3. import slugify
  4. # Import FastF1 for dynamic circuit data
  5. import fastf1
  6. import logging
  7. if TYPE_CHECKING:
  8. from circuits_service import CircuitService
  9. class ErgastService:
  10. country_map = {
  11. "united-kingdom": "uk",
  12. }
  13. def __init__(self, circuits_service: 'CircuitService' = None):
  14. self.circuits_service = circuits_service
  15. # Configure logging
  16. logging.basicConfig(level=logging.INFO)
  17. self.logger = logging.getLogger(__name__)
  18. # Initialize FastF1 cache
  19. fastf1.Cache.enable_cache('cache')
  20. def find_slugs_for_grand_prix(self, grand_prix_name: str, season: int) -> Optional[tuple[str, str, str]]:
  21. """
  22. Find and return the circuit layout file path based on Ergast F1 API race data format.
  23. """
  24. try:
  25. ergast = Ergast()
  26. f1_event = fastf1.events.get_event(int(season), grand_prix_name)
  27. f1_seasons = ergast.get_race_schedule(season=season)
  28. f1_season = f1_seasons[f1_seasons["raceName"] == grand_prix_name]
  29. country_slug = slugify.slugify(f1_event["Country"])
  30. city_slug = slugify.slugify(f1_season["locality"].values[0])
  31. circuit_slug = slugify.slugify(f1_season["circuitName"].values[0])
  32. return self.country_map[country_slug] if country_slug in self.country_map else country_slug, city_slug, circuit_slug
  33. except Exception as e:
  34. self.logger.error(
  35. f"Error finding circuit layout in Ergast for grand prix {grand_prix_name} in season {season}: {str(e)}")
  36. return None