Skip to content

Commit

Permalink
Update for post-season API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
k5cents committed May 3, 2024
1 parent 48c2115 commit b149cec
Show file tree
Hide file tree
Showing 26 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Version: 2.2.0.9002
Authors@R:
person("Kiernan", "Nicholls", , "[email protected]", role = c("aut", "cre", "cph"))
Description: Format the raw data from the ESPN fantasy football API
<https://fantasy.espn.com/apis/v3/games/ffl/> as data frames.
<https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl> as data frames.
Retrieve data on public leagues, rosters, athletes, and matches.
License: MIT + file LICENSE
Depends:
Expand Down
4 changes: 2 additions & 2 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' @keywords internal
#' @export
ffl_api <- function(leagueId = ffl_id(), view = NULL, leagueHistory = FALSE,
seasonId = 2023, scoringPeriodId = NULL, ...) {
seasonId = 2024, scoringPeriodId = NULL, ...) {
dots <- list(..., scoringPeriodId = scoringPeriodId)
age_path <- ifelse(
test = isTRUE(leagueHistory),
Expand All @@ -42,7 +42,7 @@ ffl_api <- function(leagueId = ffl_id(), view = NULL, leagueHistory = FALSE,
view <- c(view, dots)
}
try_json(
url = "https://fantasy.espn.com",
url = "https://lm-api-reads.fantasy.espn.com",
path = paste("apis/v3/games/ffl", age_path, leagueId, sep = "/"),
query = view,
leagueHistory = leagueHistory
Expand Down
3 changes: 3 additions & 0 deletions R/best.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ best_roster <- function(leagueId = ffl_id(),
scoringPeriodId = scoringPeriodId,
...
)
if (is_predraft(dat)) {
return(data.frame())
}
set <- out_roster_set(dat)
slot_count <- set$lineupSlotCounts[[1]]
do_slot <- as.integer(slot_count$position[slot_count$limit > 0])
Expand Down
4 changes: 2 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#' \item{conference}{NFL conference}
#' ...
#' }
#' @source \url{https://fantasy.espn.com/apis/v3/games/ffl/seasons/2023?view=proTeamSchedules_wl}
#' @source \url{https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons/2023?view=proTeamSchedules_wl}
"nfl_teams"

#' 2023 NFL Schedule
Expand All @@ -54,5 +54,5 @@
#' \item{date}{Matchup start date and time}
#' ...
#' }
#' @source \url{https://fantasy.espn.com/apis/v3/games/ffl/seasons/2023?view=proTeamSchedules_wl}
#' @source \url{https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons/2023?view=proTeamSchedules_wl}
"nfl_schedule"
6 changes: 3 additions & 3 deletions R/info.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @family Game information
#' @export
ffl_info <- function() {
x <- try_json("https://fantasy.espn.com/apis/v3/games/ffl")
x <- try_json(url = "https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl")
list(
abbrev = x$abbrev,
active = x$active,
Expand Down Expand Up @@ -47,7 +47,7 @@ ffl_week <- function(offset = 0) {
#' @family Game information
#' @export
ffl_seasons <- function() {
s <- try_json("https://fantasy.espn.com/apis/v3/games/ffl/seasons")
s <- try_json("https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons")
s$startDate <- ffl_date(s$startDate)
s$endDate <- ffl_date(s$endDate)
s$currentScoringPeriod <- s$currentScoringPeriod$id
Expand All @@ -63,7 +63,7 @@ ffl_seasons <- function() {
#' @family Game information
#' @export
espn_games <- function() {
g <- try_json("https://fantasy.espn.com/apis/v3/games")
g <- try_json("https://lm-api-reads.fantasy.espn.com/apis/v3/games/")
g$currentSeason$startDate <- ffl_date(g$currentSeason$startDate)
g$currentSeason$endDate <- ffl_date(g$currentSeason$endDate)
g$currentSeason <- NULL
Expand Down
3 changes: 3 additions & 0 deletions R/opponents.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
opponent_ranks <- function(leagueId = ffl_id()) {
dat <- ffl_api(leagueId, view = "kona_player_info")
p <- dat$positionAgainstOpponent$positionalRatings
if (is.null(p)) {
return(data.frame())
}
oprk <- rep(
times = 32,
x = list(list(
Expand Down
2 changes: 1 addition & 1 deletion R/outlook.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ player_outlook <- function(leagueId = ffl_id(), limit = 50) {
all_get <- httr::RETRY(
verb = "GET",
url = paste0(
"https://fantasy.espn.com/apis/v3/games/ffl/seasons/2023/segments/0/leagues/",
"https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons/2024/segments/0/leagues/",
leagueId
),
query = list(view = "kona_player_info"),
Expand Down
2 changes: 1 addition & 1 deletion R/players.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ list_players <- function(leagueId = ffl_id(),
resp <- httr::RETRY(
verb = "GET",
url = paste0(
"https://fantasy.espn.com",
"https://lm-api-reads.fantasy.espn.com",
"/apis/v3/games/ffl/seasons/2023/segments/0/leagues/",
leagueId
),
Expand Down
8 changes: 8 additions & 0 deletions R/predraft.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ skip_predraft <- function(dat) {
}
}
}

skip_empty <- function(dat) {
if (is_installed("testthat") & is.data.frame(dat)) {
if (nrow(dat) == 0) {
testthat::skip(message = "Function returned no data, likely predraft")
}
}
}
2 changes: 1 addition & 1 deletion R/pro-schedule.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
pro_schedule <- function(seasonId = ffl_year()) {
dat <- try_json(
url = sprintf(
fmt = "https://fantasy.espn.com/apis/v3/games/ffl/seasons/%i", seasonId
fmt = "https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons/%i", seasonId
),
query = list(view = "proTeamSchedules_wl")
)
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ contributing, you agree to abide by its terms.
[cov_badge]: https://img.shields.io/codecov/c/github/kiernann/fflr/master.svg
[cov_link]: https://app.codecov.io/gh/kiernann/fflr?branch=master'
[dl_badge]: https://cranlogs.r-pkg.org/badges/grand-total/fflr
[api]: https://fantasy.espn.com/apis/v3/games/ffl/
[api]: https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/
[iss]: https://github.com/kiernann/fflr/issues
[cran]: https://cran.r-project.org/package=fflr
[gh]: https://github.com/kiernann/fflr
Expand Down
2 changes: 1 addition & 1 deletion data-raw/nfl-teams.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## code to prepare `nfl_teams` dataset goes here
dat <- try_json(
url = "https://fantasy.espn.com/apis/v3/games/ffl/seasons/2022",
url = "https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons/2024",
query = list(view = "proTeamSchedules_wl")
)

Expand Down
2 changes: 1 addition & 1 deletion man/ffl_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/nfl_schedule.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/nfl_teams.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test-acquisition.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("player acquisition", {
a <- player_acquire(leagueId = "42654852")
skip_if(nrow(a) == 0)
expect_type(a, "list")
expect_true("acquisitionType" %in% names(a[[1]]))
})
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-best.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ test_that("calculate best possible past roster", {
useScore = "actualScore",
scoringPeriodId = 1
)
skip_empty(b)
b1 <- b[[1]]
expect_s3_class(b1, "data.frame")
expect_length(b1, 17)
Expand All @@ -19,6 +20,7 @@ test_that("calculate best possible future roster", {
useScore = "projectedScore",
scoringPeriodId = ffl_week()
)
skip_empty(b)
b2 <- b[[2]]
expect_s3_class(b2, "data.frame")
expect_length(b2, 17)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-events.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
test_that("get NFL game schedule times", {
e <- pro_events()
skip_empty(e)
expect_s3_class(e, "data.frame")
expect_type(e$competitors, "list")
expect_length(e, 13)
})

test_that("get NFL game scores", {
s <- pro_scores()
skip_empty(s)
expect_s3_class(s, "data.frame")
expect_type(s$lineup, "list")
expect_length(s, 9)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-history.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ test_that("historical data can be combined", {
leagueId = "42654852"
)
expect_s3_class(h, "data.frame")
expect_length(h, 12)
expect_length(h, 6)
})
1 change: 1 addition & 0 deletions tests/testthat/test-live.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("live scoring returns 1 row per team", {
l <- live_scoring(leagueId = "42654852", yetToPlay = TRUE, bonusWin = TRUE)
skip_empty(l)
expect_s3_class(l, "data.frame")
expect_true("toPlay" %in% names(l))
expect_length(l, 8)
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-opponents.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("opponent ranks", {
o <- opponent_ranks(leagueId = "42654852")
skip_empty(o)
expect_s3_class(o, "data.frame")
expect_length(o, 4)
expect_length(unique(o$opponentProTeam), 32)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-outlook.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ test_that("get individual player outlook", {
limit = 1
)
expect_s3_class(o, "data.frame")
expect_length(o, 6)
expect_length(o, 5)
})
2 changes: 2 additions & 0 deletions tests/testthat/test-roster.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("All current team rosters as list", {
r <- team_roster(leagueId = "42654852", scoringPeriodId = 1)
skip_empty(r)
expect_type(r, "list")
expect_length(r, 4)
expect_s3_class(r[[1]], "data.frame")
Expand All @@ -9,6 +10,7 @@ test_that("All current team rosters as list", {

test_that("All final period team rosters as list of list", {
rh <- team_roster(leagueId = "252353", leagueHistory = TRUE)
skip_empty(rh)
expect_gt(length(rh), 4)
expect_length(rh[[1]], 8)
expect_length(rh[[2]], 10)
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Sys.sleep(runif(1, 1, 2))
test_that("acquisition settings for a single season", {
w <- acquisition_settings("42654852", leagueHistory = FALSE)
expect_s3_class(w, "data.frame")
expect_length(w, 10)
expect_length(w, 12)
expect_type(w$acquisitionBudget, "integer")
expect_type(w$waiverProcessDays, "list")
expect_type(w$waiverOrderReset, "logical")
Expand Down Expand Up @@ -80,7 +80,7 @@ Sys.sleep(runif(1, 1, 2))
test_that("schedule settings for a single season", {
s <- schedule_settings("42654852", leagueHistory = FALSE)
expect_s3_class(s, "data.frame")
expect_length(s, 10)
expect_length(s, 12)
expect_s3_class(s$matchupPeriods[[1]], "data.frame")
expect_s3_class(s$divisions[[1]], "data.frame")
})
Expand All @@ -103,3 +103,4 @@ test_that("trade settings for a single season", {
expect_type(t$allowOutOfUniverse, "logical")
expect_false(t$allowOutOfUniverse)
})

2 changes: 1 addition & 1 deletion tests/testthat/test-simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ test_that("get simulated season projections", {
leagueId = "42654852"
)
expect_s3_class(s, "data.frame")
expect_length(s, 11)
expect_length(s, 5)
})
7 changes: 0 additions & 7 deletions tests/testthat/test-standings.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,3 @@ test_that("combine all historical standings", {
expect_gt(nrow(s), 10)
expect_gt(length(unique(s$seasonId)), 1)
})

test_that("simulations returns data frame", {
s <- league_simulation("42654852")
expect_s3_class(s, "data.frame")
expect_length(s, 11)
expect_true("playoffPct" %in% names(s))
})

0 comments on commit b149cec

Please sign in to comment.