Creating custom MFL API calls
The MFL
API is extensive. If there is something you’d like to access beyond
the current scope of ffscrapr, you can use the lower-level
“mfl_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 try searching for SFBX leagues through the leagueSearch endpoint.
We’ll start by opening up this page, https://api.myfantasyleague.com/2020/api_info?STATE=test&CCAT=export&TYPE=leagueSearch, which is the “test” page for this particular endpoint. From here, we can see that the only parameter required is “SEARCH”.
We need a connection object to pass into the mfl_getendpoint function, although in this example we don’t need to give it much since the endpoint doesn’t need a leagueID or username/password or APIKEY.
conn <- mfl_connect(season = 2020)
conn
#> <MFL connection 2020_>
#> List of 5
#> $ platform : chr "MFL"
#> $ season : num 2020
#> $ league_id : chr(0)
#> $ APIKEY : NULL
#> $ auth_cookie: NULL
#> - attr(*, "class")= chr "mfl_conn"
The parameters of the mfl_getendpoint function are conn
,
endpoint
, and any other optional parameters required by the
API.
The function will automatically insert the league_id, API key, and/or authentication cookies from the connection object, and will request JSON for you - so you do not need to add any of these parameters.
It is safest to assume that everything is case-sensitive: the endpoint must match the case displayed by MFL (“leagueSearch”) and the SEARCH argument name must be provided in upper-case.
sfb_search <- mfl_getendpoint(conn,endpoint = "leagueSearch", SEARCH = "sfbx conference")
#> Using request.R from "ffscrapr"
str(sfb_search, max.level = 1)
#> List of 3
#> $ content :List of 3
#> $ query : chr "https://api.myfantasyleague.com/2020/export?TYPE=leagueSearch&SEARCH=sfbx%20conference&JSON=1"
#> $ response:List of 9
#> ..- attr(*, "class")= chr "response"
#> - attr(*, "class")= chr "mfl_api"
The function returns a list with the query
that was
sent, the response
that was received, and the
content
that was parsed - this helps you debug the result
of the function later, by inspecting the query that was sent and the
response that was received.
I like to extract the content
with
purrr::pluck
and then convert it into a tibble and unnest
the content from there, but you can use base R subsetting or
magrittr::extract2
for the same purpose.
search_results <- sfb_search %>%
purrr::pluck("content","leagues","league") %>%
tibble::tibble() %>%
tidyr::unnest_wider(1)
head(search_results)
#> # A tibble: 6 × 4
#> id name year homeURL
#> <chr> <chr> <chr> <chr>
#> 1 13411 #SFBX Conference 12 2020 https//www43.myfantasyleague.com/2020/home/13…
#> 2 16428 #SFBX Conference 6 2020 https//www43.myfantasyleague.com/2020/home/16…
#> 3 17910 #SFBX Conference 11 2020 https//www43.myfantasyleague.com/2020/home/17…
#> 4 26453 #SFBX Conference 8 2020 https//www43.myfantasyleague.com/2020/home/26…
#> 5 27495 #SFBX Conference 2 2020 https//www43.myfantasyleague.com/2020/home/27…
#> 6 31492 #SFBX Conference 14 2020 https//www43.myfantasyleague.com/2020/home/31…
Another Example: Trade Bait
Here’s another example, this time with the trade bait endpoint: https://api.myfantasyleague.com/2020/api_info?STATE=test&CCAT=export&TYPE=tradeBait
fog <- mfl_connect(season = 2019, league_id = 12608)
fog_tradebait <- mfl_getendpoint(fog, "tradeBait", INCLUDE_DRAFT_PICKS = 1) %>%
purrr::pluck("content","tradeBaits","tradeBait") %>%
tibble::tibble() %>%
tidyr::unnest_wider(1) %>%
tidyr::separate_rows("willGiveUp",sep = ",") %>%
dplyr::left_join(
ff_franchises(fog) %>% dplyr::select("franchise_id","franchise_name"),
by = c("franchise_id")
) %>%
dplyr::left_join(
mfl_players(fog) %>% dplyr::select("player_id","player_name","pos","age","team"),
by = c("willGiveUp" = "player_id")
)
head(fog_tradebait)
#> # A tibble: 6 × 9
#> timestamp willGiveUp franchise_id inExchangeFor franchise_name player_name
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 1574520555 7394 0001 "anyone need a … @JohnBoschFF Rivers, Ph…
#> 2 1574520555 13128 0001 "anyone need a … @JohnBoschFF Cook, Dalv…
#> 3 1580535809 13319 0003 "" The Accountant Jones, Aar…
#> 4 1580535809 13139 0003 "" The Accountant Williams, …
#> 5 1580535809 11675 0003 "" The Accountant Adams, Dav…
#> 6 1580572165 12171 0004 "" Kevin Cutillo Johnson, D…
#> # ℹ 3 more variables: pos <chr>, age <dbl>, team <chr>