Created
January 15, 2025 07:07
-
-
Save theracingapi/78f2d7dc7a865a5d88acd57ac5cc022b to your computer and use it in GitHub Desktop.
Python script to pull North America add-on data from today up to a week in advance, including entries data and results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from requests.auth import HTTPBasicAuth | |
| import json | |
| import pandas | |
| from datetime import datetime, timedelta | |
| import time | |
| user = "USERNAME" | |
| password = "PASSWORD" | |
| now = datetime.now() | |
| now_str = now.strftime("%Y-%m-%d") | |
| now_p6 = now + timedelta(days=6) | |
| now_p6_str = now_p6.strftime("%Y-%m-%d") | |
| def get_dates(start, end): | |
| start_dt = datetime.strptime(start, "%Y-%m-%d") | |
| end_dt = datetime.strptime(end, "%Y-%m-%d") | |
| return pandas.date_range(start_dt, end_dt-timedelta(days=1),freq='d').strftime("%Y-%m-%d").tolist() | |
| # Get range of dates for which to pull US data (tracking began July 2023) | |
| dates = get_dates(now_str, now_p6_str) | |
| for date in dates: | |
| params={"start_date": date, "end_date": date} | |
| time.sleep(0.5) | |
| resp = requests.get("https://api.theracingapi.com/v1/north-america/meets", auth=HTTPBasicAuth(user, password), params=params) | |
| meet_entries = [] | |
| meet_results = [] | |
| print(resp.json()) | |
| for meet in resp.json()["meets"]: | |
| time.sleep(0.5) | |
| resp_entry = requests.get(f"https://api.theracingapi.com/v1/north-america/meets/{meet['meet_id']}/entries", auth=HTTPBasicAuth(user, password)) | |
| meet_entries.append(resp_entry.json()) | |
| time.sleep(0.5) | |
| resp_results = requests.get(f"https://api.theracingapi.com/v1/north-america/meets/{meet['meet_id']}/results", auth=HTTPBasicAuth(user, password)) | |
| meet_results.append(resp_results.json()) | |
| # Process entries/results before moving on to next date in loop, e.g import to db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment