Sleeper: Get Endpoint
Tan Ho
2022-06-22
Source:vignettes/sleeper_getendpoint.Rmd
sleeper_getendpoint.Rmd
Creating custom Sleeper API calls
The Sleeper API is pretty extensive. If there is something you’d like to access that’s beyond the current scope of ffscrapr, you can use the lower-level “sleeper_getendpoint
” function to create a GET request and access the data, while still using the authentication and rate-limiting features I’ve already created.
Here is an example of how you can call one of the endpoints - in this case, let’s pull Sleeper’s trending players data!
We’ll start by opening up this page, https://docs.sleeper.app/#trending-players, which is the documentation page for this particular endpoint. From here, we can see that Sleeper’s documentation says the endpoint is:
https://api.sleeper.app/v1/players/<sport>/trending/<type>?lookback_hours=<hours>&limit=<int>
On first glance, you can see that it takes two parameters within the endpoint call itself (sport
and type
) and we can further adjust the query with HTTP parameters lookback_hours
and limit
. The sleeper_getendpoint function already has the https://api.sleeper.app/v1/
part encoded, so all we’ll need to do is pass in the remaining part of the URL as the endpoint, and pass the HTTP parameters in as arguments to the function (these are case sensitive!)
We can use the glue
package to parameterise this, although you can also use base R’s paste function just as easily.
type <- "add"
query <- glue::glue('players/nfl/trending/{type}')
query
#> players/nfl/trending/add
response_trending <- sleeper_getendpoint(query,lookback_hours = 48, limit = 10)
#> Using request.R from "ffscrapr"
#> No encoding supplied: defaulting to UTF-8.
str(response_trending, max.level = 1)
#> List of 3
#> $ content :List of 10
#> $ query : chr "https://api.sleeper.app/v1/players/nfl/trending/add?lookback_hours=48&limit=10"
#> $ response:List of 8
#> ..- attr(*, "class")= chr "response"
#> - attr(*, "class")= chr "sleeper_api"
Along with the parsed content, the function also returns the query and the response that was sent by the server. These are helpful for debugging, but we can turn the content into a dataframe with some careful application of the tidyverse.
df_trending <- response_trending %>%
purrr::pluck("content") %>%
dplyr::bind_rows()
head(df_trending)
#> # A tibble: 6 × 2
#> player_id count
#> <chr> <int>
#> 1 6002 14287
#> 2 7904 11553
#> 3 7703 11403
#> 4 4622 8548
#> 5 2711 7165
#> 6 6271 5452
This isn’t very helpful without knowing who these players are, so let’s pull the players endpoint in as well - this one has a convenient function!
players <- sleeper_players() %>%
select(player_id, player_name, pos, team, age)
#> No encoding supplied: defaulting to UTF-8.
trending <- df_trending %>%
left_join(players, by = "player_id")
trending
#> # A tibble: 10 × 6
#> player_id count player_name pos team age
#> <chr> <int> <chr> <chr> <chr> <dbl>
#> 1 6002 14287 Qadree Ollison RB ATL 25.8
#> 2 7904 11553 Stevie Scott RB NO NA
#> 3 7703 11403 Jacob Harris TE LAR NA
#> 4 4622 8548 Keelan Cole WR NYJ 29.2
#> 5 2711 7165 Taylor Heinicke QB WAS 29.3
#> 6 6271 5452 Olamide Zaccheaus WR ATL 24.9
#> 7 5199 4462 Byron Pringle WR KC 28.6
#> 8 7622 4082 Sammis Reyes TE WAS 26.7
#> 9 7530 3646 Frank Darby WR ATL NA
#> 10 2583 3201 Tyrell Williams WR DET 30.4
There - this means something to us now! As of this writing (2020-11-10), Kalen Ballage was the most added player. Haven’t we been bitten by this before?