sleeper_getendpoint

Tan Ho

2020-11-15

Creating your own custom Sleeper API calls

library(ffscrapr)
library(dplyr)
library(purrr)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse

The Sleeper API is pretty extensive, and I haven’t written out a function for every single combination of the endpoints. If there is something you’d like to access, 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>

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.

We’ll need to fill out the other parameters as per the documentation: sport is NFL, type is either add or drop, lookback_hours is optional in hours, and limit is optional in number of rows. We can use the glue package to parameterise this, although you can also use base R’s paste function just as easily.


query <- glue::glue('players/nfl/trending/add')

query
#> players/nfl/trending/add

response_trending <- sleeper_getendpoint(query)

str(response_trending, max.level = 1)
#> List of 3
#>  $ content :List of 25
#>  $ query   : chr "https://api.sleeper.app/v1/players/nfl/trending/add/"
#>  $ response:List of 10
#>   ..- 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 debug, 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 x 2
#>   player_id  count
#>   <chr>      <int>
#> 1 4994      157599
#> 2 5163       94475
#> 3 1535       91276
#> 4 6012       75473
#> 5 6931       67759
#> 6 4150       58560

This isn’t very helpful without knowing who these players are - let’s pull the players endpoint in as well.


players <- sleeper_players() %>% 
  select(player_id, player_name, pos, team, age)

trending <- df_trending %>% 
  left_join(players, by = "player_id")

trending
#> # A tibble: 25 x 6
#>    player_id  count player_name           pos   team    age
#>    <chr>      <int> <chr>                 <chr> <chr> <dbl>
#>  1 4994      157599 Kalen Ballage         RB    LAC    24.9
#>  2 5163       94475 Ryan Nall             RB    CHI    24.9
#>  3 1535       91276 Cordarrelle Patterson WR    CHI    29.7
#>  4 6012       75473 Travis Homer          RB    SEA    22.3
#>  5 6931       67759 DeeJay Dallas         RB    SEA    22.2
#>  6 4150       58560 Wayne Gallman         RB    NYG    26.1
#>  7 6918       49667 Salvon Ahmed          RB    MIA    21.9
#>  8 3934       40049 Troymaine Pope        RB    LAC    27  
#>  9 1048       39989 Lamar Miller          RB    CHI    29.6
#> 10 MIN        39109 <NA>                  DEF   MIN    NA  
#> # ... with 15 more rows

There - this means something to us now! As of this writing, Kalen Ballage is the most added player. Haven’t we been bitten by this before?