Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_f1():
- try:
- main_page_data = requests.get("https://www.formula1.com/en/racing/2025").text
- # Find API Key
- api_key_pattern = r'\\\"NEXT_PUBLIC_GLOBAL_BROADCAST_APIKEY\\\":\\\"(.*?)\\\"'
- match = re.search(api_key_pattern, main_page_data)
- api_key = match.group(1) if match else None
- # Grab relevant page data
- event_data = requests.get("https://api.formula1.com/v1/event-tracker", headers={"Apikey": api_key, "Locale": "en"}).json()
- race_page = requests.get(f"https://www.formula1.com{event_data["race"]["url"]}").text
- # Find each section using Regex
- title_pattern = r'<title>(.*?)</title>'
- title_match = re.search(title_pattern, race_page)
- current_year = str(datetime.now().year)
- matches = title_match.group(1).split(current_year) if title_match else ""
- title = matches[0] if title_match and len(matches) > 0 else ""
- 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)
- # Only set lap/record data if matches found
- if circuit_data_match and len(circuit_data_match) > 2:
- event_data["laps"] = circuit_data_match[1].split(">")[1].split("<")[0]
- event_data["record"] = circuit_data_match[2].split(">")[1].split("<")[0]
- event_data["customTitle"] = title
- # Localise Times
- for session in event_data["seasonContext"]["timetables"]:
- session["start"] = convert_f1_date(session["startTime"] + session["gmtOffset"])
- session["end"] = convert_f1_date(session["endTime"] + session["gmtOffset"])
- return event_data
- except Exception as e:
- print(e)
- return None
Advertisement
Add Comment
Please, Sign In to add comment