Guest User

Untitled

a guest
Aug 12th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | Source Code | 0 0
  1. def get_f1():
  2.  
  3. try:
  4. main_page_data = requests.get("https://www.formula1.com/en/racing/2025").text
  5.  
  6. # Find API Key
  7. api_key_pattern = r'\\\"NEXT_PUBLIC_GLOBAL_BROADCAST_APIKEY\\\":\\\"(.*?)\\\"'
  8. match = re.search(api_key_pattern, main_page_data)
  9. api_key = match.group(1) if match else None
  10.  
  11. # Grab relevant page data
  12. event_data = requests.get("https://api.formula1.com/v1/event-tracker", headers={"Apikey": api_key, "Locale": "en"}).json()
  13. race_page = requests.get(f"https://www.formula1.com{event_data["race"]["url"]}").text
  14.  
  15. # Find each section using Regex
  16. title_pattern = r'<title>(.*?)</title>'
  17. title_match = re.search(title_pattern, race_page)
  18. current_year = str(datetime.now().year)
  19. matches = title_match.group(1).split(current_year) if title_match else ""
  20. title = matches[0] if title_match and len(matches) > 0 else ""
  21. circuit_data_match = re.findall(r'<dd class="typography-module_display-l-bold__m1yaJ text-text-5 mt-px-4 lg:mt-px-12(.*?)<\/dd>', race_page, re.DOTALL)
  22.  
  23. # Only set lap/record data if matches found
  24. if circuit_data_match and len(circuit_data_match) > 2:
  25. event_data["laps"] = circuit_data_match[1].split(">")[1].split("<")[0]
  26. event_data["record"] = circuit_data_match[2].split(">")[1].split("<")[0]
  27.  
  28. event_data["customTitle"] = title
  29.  
  30. # Localise Times
  31. for session in event_data["seasonContext"]["timetables"]:
  32. session["start"] = convert_f1_date(session["startTime"] + session["gmtOffset"])
  33. session["end"] = convert_f1_date(session["endTime"] + session["gmtOffset"])
  34.  
  35. return event_data
  36.  
  37. except Exception as e:
  38. print(e)
  39. return None
Advertisement
Add Comment
Please, Sign In to add comment