Creating custom Fleaflicker API calls

The Fleaflicker API is fairly extensive. If there is something you’d like to access that’s beyond the current scope of ffscrapr, you can use the lower-level “fleaflicker_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 zoom in on the Fetch League Scoreboard endpoint, which is used inside a few different ffscrapr functions and summarised in a few ways.

We’ll start by opening up this page, https://www.fleaflicker.com/api-docs/index.html#operation--FetchLeagueScoreboard-get, which is the documentation page for this particular endpoint.

From here, we can see that Fleaflicker’s documentation says the endpoint and parameters are:

GET /FetchLeagueScoreboard

Parameters:

- sport: string NFL, MLB, NBA, NHL NFL
(in query)

- league_id: integer (int32)
(in query)

- season: integer (int32)
(in query)

- scoring_period: integer (int32)
(in query)

The fleaflicker_getendpoint function already has the base url encoded, so all we’ll need to do is pass in the endpoint without the /, and pass the HTTP parameters in as arguments to the function (these are case sensitive!)

sport <- "NFL"
league_id <- 206154
season <- 2020
week <- 5

response_scoreboard <- fleaflicker_getendpoint("FetchLeagueScoreboard",
                                               sport = sport, 
                                               league_id = league_id, 
                                               season = season, 
                                               scoring_period = week)

str(response_scoreboard, max.level = 1)
#> List of 3
#>  $ content :List of 3
#>  $ query   : chr "https://www.fleaflicker.com/api/FetchLeagueScoreboard?sport=NFL&league_id=206154&season=2020&scoring_period=5"
#>  $ response:List of 10
#>   ..- attr(*, "class")= chr "response"
#>  - attr(*, "class")= chr "fleaflicker_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_scoreboard <- response_scoreboard %>% 
  purrr::pluck("content","games") %>% 
  tibble::tibble() %>% 
  tidyr::unnest_wider(1) %>% 
  dplyr::mutate_at(c("away","home"),purrr::map_chr,purrr::pluck,"franchise_name"="name") %>% 
  dplyr::mutate_at(c("homeScore","awayScore"),purrr::map_dbl,purrr::pluck,"score","value")

head(df_scoreboard)
#> # A tibble: 6 x 8
#>   id     away     home    awayScore homeScore homeResult awayResult isFinalScore
#>   <chr>  <chr>    <chr>       <dbl>     <dbl> <chr>      <chr>      <lgl>       
#> 1 46301… Winterf… Top Ci…      207.      162. LOSE       WIN        TRUE        
#> 2 46301… Goldenr… Winter…      145.      157. WIN        LOSE       TRUE        
#> 3 46301… Wintert… Clutch…      153.      192. WIN        LOSE       TRUE        
#> 4 46301… Manitob… Bamend…      182.      183. WIN        LOSE       TRUE        
#> 5 46301… Springf… Shangh…      176.      129. LOSE       WIN        TRUE        
#> 6 46301… Boomtow… Philad…      199.      200. WIN        LOSE       TRUE

From here, we’ll be able to feed these IDs into the FetchLeagueBoxscore endpoint https://www.fleaflicker.com/api-docs/index.html#operation--FetchLeagueScoreboard-get as the fantasy_game_id - and from here you’ll be able to get to player-level points and actual stat data for each fantasy game!

# same variables as previous endpoint call!
onegame_lineups <- fleaflicker_getendpoint(
  "FetchLeagueBoxscore",
  sport = sport, 
  league_id = league_id, 
  # example for one call, but you can call this in a map or loop! 
  fantasy_game_id = df_scoreboard$id[[1]], 
  scoring_period = week) %>% 
  purrr::pluck('content','lineups') %>% 
  tibble::tibble() %>% 
  tidyr::unnest_wider(1) %>% 
  tidyr::unnest_longer('slots') %>% 
  tidyr::unnest_wider('slots') %>% 
  tidyr::pivot_longer(c("home","away"),names_to = "franchise",values_to = "player") %>% 
  tidyr::unnest_wider('player')

str(onegame_lineups,max.level = 2)
#> tibble [98 × 16] (S3: tbl_df/tbl/data.frame)
#>  $ group               : chr [1:98] "START" "START" "START" "START" ...
#>  $ position            :List of 98
#>  $ positionColor       :List of 98
#>  $ franchise           : chr [1:98] "home" "away" "home" "away" ...
#>  $ proPlayer           :List of 98
#>  $ requestedGames      :List of 98
#>  $ viewingActualPoints :List of 98
#>  $ viewingActualStats  :List of 98
#>  $ requestedGamesPeriod:List of 98
#>  $ viewingFormat       : chr [1:98] "TOTAL" "TOTAL" "TOTAL" "TOTAL" ...
#>  $ viewingRange        :List of 98
#>  $ owner               :List of 98
#>  $ displayGroup        : chr [1:98] "PASSER" "PASSER" "RUSHER" "RUSHER" ...
#>  $ rankFantasy         :List of 98
#>  $ rankDraft           :List of 98
#>  $ lastX               :List of 98

From here, you can keep unravelling - including the “viewingActualPoints” and “viewingActualStats” columns!