jimmyday12 / fitzRoy

A set of functions to easily access AFL data
https://jimmyday12.github.io/fitzRoy
Other
129 stars 28 forks source link

Add AFLTables Score Progression Data #203

Open thecruncherau opened 1 year ago

thecruncherau commented 1 year ago

There doesn't appear to be any score progression data within {fitzRoy} currently, so it might be a welcomed feature if it were to be included. Contributing this myself would be something I'd be interested in working on.

# sample function call
fetch_score_progression_afltables(year = 2023, round = 15)
# returns `tibble` with col names `match_id`, `event_type`, `team`, `player`, `quarter_number`, `minutes`, `seconds`,  `Home.Team`, `Home.Goals`, `Home.Behinds`, `Home.Points`, `Away.Team`, `Away.Goals`, `Away.Behinds`, `Away.Points`

A possible problem with this is efficiency - replicating AFLTables' own match id system for scraping relies on knowing all teams involved in the season in question. Additionally, it's only possible to scrape one at a time and therefore, discouraging scraping of many matches in some way would be ideal.

Another concern is matching ids. The ids in the big list, scraped in fetch_results_afltables(), use a simple chronological index and it's not possible to derive this index from an individual match centre page alone.

Looking forward to hearing your thoughts on this.

lachlanpcasey commented 1 year ago

This would be great.

jimmyday12 commented 1 year ago

Thanks for the message and it's something I've looked into in the past but have always put to the side for similar reasons you suggest above.

I'd be more than open to a PR if you had a working example. Even if it's just a starting point of getting the actual results for one match, we could then look at some kind of general approach for thinking about finding a specific match

t-gummer commented 1 year ago

I believe there is also an API for the AFL Website that has all the required information to do a score progression worm as well.

library(httr)
library(jsonlite)

headers = c(
  'x-media-mis-token' = fitzRoy:::get_afl_cookie()
)

res <- GET(url = "https://api.afl.com.au/cfs/afl/matchItem/CD_M20230141903", add_headers(headers))

match_info <- parse_json(content(res, 'text', encoding = "UTF-8"))

With some further wrangling the information contained in match_info$score$scoreWorm and match_info$score$matchClock would be sufficient to construct score worm data (the latter for providing the length of quarters if that is part of your logic).

This api also has a bunch of other handy information about matches such as weather.