Skip to content

Instantly share code, notes, and snippets.

@theracingapi
Last active February 22, 2024 16:28
Show Gist options
  • Save theracingapi/4d492dbdba58c2072fe4f98ee090126f to your computer and use it in GitHub Desktop.
Save theracingapi/4d492dbdba58c2072fe4f98ee090126f to your computer and use it in GitHub Desktop.
Python script to efficiently extract results from the API for a given date range (Standard Plan)
import requests
from requests.auth import HTTPBasicAuth
from datetime import datetime, timedelta
import time
# Define API credentials
username = "USERNAME"
password = "PASSWORD"
def generate_date_list(start_date, end_date):
# Convert start_date and end_date from string to datetime objects
start = datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.strptime(end_date, "%Y-%m-%d")
# Calculate the number of days between start_date and end_date
delta = end - start
# Generate a list of dates from start_date to end_date
date_list = [(start + timedelta(days=i)).strftime("%Y-%m-%d") for i in range(delta.days + 1)]
return date_list
if __name__ == '__main__':
# Set date range for which you would like to request results and get list of individual dates
start_date = (datetime.today() - timedelta(days=365)).strftime("%Y-%m-%d")
end_date = datetime.today().strftime("%Y-%m-%d")
dates = generate_date_list(start_date, end_date)
for date in dates:
# Define limit, skip and total variables
limit = 50
skip = 0
total = 0
params = {
"start_date": date,
"end_date": date,
"limit": limit,
"skip": skip
}
# Adding sleep to abide by rate limits
time.sleep(0.5)
resp = requests.get("https://api.theracingapi.com/v1/results", auth=HTTPBasicAuth(username, password), params=params)
if resp.ok:
data = resp.json()
# Process data['results'] here ->
# E.g import into personal database
# End process results
# Continue fetching additional results using pagination, while the skip is less than total
total = data.get("total", 0)
skip += limit
while skip < total:
params = {
"start_date": date,
"end_date": date,
"limit": limit,
"skip": skip
}
# Adding sleep to abide by rate limits
time.sleep(0.5)
resp = requests.get("https://api.theracingapi.com/v1/results", auth=HTTPBasicAuth(username, password), params=params)
if resp.ok:
data = resp.json()
# Process data['results'] here ->
# E.g import into personal database
# End process results
skip += limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment