Bulk and spreadsheets › A month of results into CSV
A month of results into CSV
Every settled race in Great Britain last month with its winner, page by page, into one CSV file you can open in any spreadsheet.
# A month of results into CSV: page by page with the cursor
# needs: AHR_KEY in your environment, and jq
FROM=$(date -d "$(date +%Y-%m-01) -1 month" +%F); TO=$(date -d "$(date +%Y-%m-01) -1 day" +%F)
CURSOR=""; echo "date,time,course,name,winner,sp,trainer" > results.csv
while :; do
PAGE=$(curl -s "https://api.apihorseracing.com/v1/races/search?region=GB&date_from=$FROM&date_to=$TO&result_available=1&include=winner&limit=200&cursor=$CURSOR" \
-H "X-API-Key: $AHR_KEY")
echo "$PAGE" | jq -r '.data[] | [.date, .time, .course, .name, .winner.horse, .winner.sp, .winner.trainer] | @csv' >> results.csv
[ "$(echo "$PAGE" | jq -r '.meta.has_more')" = "true" ] || break
CURSOR=$(echo "$PAGE" | jq -r '.meta.next_cursor')
done
What the reply holds
- data[]
- One row per race.
- winner
- The winner, its SP and trainer.
- meta.has_more
- Whether there is another page.
- meta.next_cursor
- Pass it as cursor for the next page.
When it says no
401no_key The key is missing or mistyped. Every sample reads it from AHR_KEY.403plan_needed Your plan does not reach this. The reply names the plan that does.429rate_limited Too fast. Wait the seconds in Retry-After, then try again.