library(ffscrapr)
library(dplyr)
library(purrr)
library(glue)
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!)
<- "NFL"
sport <- 206154
league_id <- 2020
season <- 5
week
<- fleaflicker_getendpoint("FetchLeagueScoreboard",
response_scoreboard sport = sport,
league_id = league_id,
season = season,
scoring_period = week)
#> Using request.R from "ffscrapr"
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 9
#> ..- 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.
<- response_scoreboard %>%
df_scoreboard ::pluck("content","games") %>%
purrr::tibble() %>%
tibble::unnest_wider(1) %>%
tidyr::mutate_at(c("away","home"),purrr::map_chr,purrr::pluck,"name") %>%
dplyr::mutate_at(c("homeScore","awayScore"),purrr::map_dbl,purrr::pluck,"score","value")
dplyr
head(df_scoreboard)
#> # A tibble: 6 × 8
#> id away home awayS…¹ homeS…² homeR…³ awayR…⁴ isFin…⁵
#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <lgl>
#> 1 46301923 Winterfell Dire Wolves Top … 207. 162. LOSE WIN TRUE
#> 2 46301919 Goldenrod City Nightma… Wint… 145. 157. WIN LOSE TRUE
#> 3 46301921 Winterthur Angry Ducks Clut… 153. 192. WIN LOSE TRUE
#> 4 46301922 Manitoba Marmots Bame… 182. 183. WIN LOSE TRUE
#> 5 46301920 Springfield Isotopes Shan… 176. 129. LOSE WIN TRUE
#> 6 46301926 Boomtown Sly Foxes Phil… 199. 200. WIN LOSE TRUE
#> # … with abbreviated variable names ¹awayScore, ²homeScore, ³homeResult,
#> # ⁴awayResult, ⁵isFinalScore
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!
<- fleaflicker_getendpoint(
onegame_lineups "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) %>%
::pluck('content','lineups') %>%
purrr::tibble() %>%
tibble::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')
tidyr
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!