fflr icon indicating copy to clipboard operation
fflr copied to clipboard

Unable to pull historical results of team_rosters via leagueHistory = TRUE

Open KevinLeone opened this issue 1 year ago • 2 comments

Only latest scoringPeriodID by season is provided via leagueHistory = TRUE in most cases. Specifically looking for a way to utilize these functions to loop through the API for each Season Period ID /scoringPeriod ID, but unable to select parameters/add helper functions to environment which aren't defined.

Desired outcome example: scoring_history <- combine_history(team_roster, param such as: scoringPeriodId = ALL), provide lists of players for scoringPeriodIds 1-16, 1-17, 1-18, etc.

KevinLeone avatar Feb 25 '24 20:02 KevinLeone

Unfortunately, I think this is a limitation of the ESPN API.

The way to do what you're looking for is to not use leagueHistory = TRUE and instead do a separate call to team_roster() with seasonId and scoringPeriodId set separately for each year/week. A tricky thing is you can pass seasonId to these functions because of the ... argument.

Like a loop within a loop would work.

for (year in 2020:2023) {
  for (week in 1:15) {
    team_roster(
      seasonId = year,
      scoringPeriodId = week
    )
  }
}

In my limited testing that does seem to return the right roster for weeks in the middle of the season. Beware that maaaaybe some older seasons don't have this data saved. I've found that some things in the API are different for seasons before like 2018.

Let me know if that works?

k5cents avatar Feb 26 '24 00:02 k5cents

Ah bummer, but that makes sense via the API updates. The above didn't work for me as I received a 404 error, but based on:

my_team <- team_roster(scoringPeriodId = 1)[[1:1]] # select first roster

the below worked to provide me a comprehensive list of all 2023 roster spots from Week 1 to 17:

# Loop through each scoring period and team ID
for (scoring_period_id in 1:total_scoring_periods) {
  for (team_id in team_ids) {
    # Get the roster for the specified scoring period and team ID
    team_roster <- team_roster(leagueId = ffl_id(), scoringPeriodId = scoring_period_id)[[team_id]]
    # Convert the roster to a tibble and add columns for scoring period and team ID
    roster_tibble <- as_tibble(team_roster) %>%
      mutate(scoringPeriodId = scoring_period_id, teamId = team_id)
    # Append the roster tibble to the list
    all_rosters[[length(all_rosters) + 1]] <- roster_tibble
  }
}

I am trying to think of a way which this works through the back-end of our league_history (such as 2016-2022), which will probably throw errors like the above did for me if I try to loop it.

KevinLeone avatar Feb 26 '24 13:02 KevinLeone