Skip to content

Commit

Permalink
Merge pull request #346 from ffverse/dev
Browse files Browse the repository at this point in the history
v1.4.7 to CRAN - minor fixes and new tidyr support
  • Loading branch information
tanho63 authored Nov 10, 2021
2 parents 56a60bb + e96a26c commit 3c6a9ae
Show file tree
Hide file tree
Showing 39 changed files with 139 additions and 109 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: ffscrapr
Title: API Client for Fantasy Football League Platforms
Version: 1.4.6
Version: 1.4.7
Authors@R:
c(person(given = "Tan",
family = "Ho",
Expand Down Expand Up @@ -38,7 +38,6 @@ Imports:
httr (>= 1.4.0),
jsonlite (>= 1.6.0),
lifecycle,
lubridate (>= 1.5.0),
magrittr (>= 1.5.0),
nflreadr (>= 1.1.0),
memoise (>= 2.0.0),
Expand All @@ -61,5 +60,5 @@ LazyData: true
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
Roxygen: list(markdown = TRUE)
19 changes: 19 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# ffscrapr 1.4.7

The main goal of v1.4.7 is to patch some minor bugs and to pre-emptively correct issues related to upstream dependency `tidyr`.

## Changes
- Fix `ff_scoringhistory.flea_conn()` kicking bug.
- Fix `ff_schedule.mfl_conn()` NA bug.
- Remove lubridate dep
- Update `.espn_stat_map()` to add threshold-based scoring and fix a missing defensive scoring setting
- Update `ff_scoringhistory.espn_conn()` to add threshold calcs. (thank you, @scottfrechette!)
- Fix `espn_potentialpoints()` to move regular flex ahead of superflex
- `ff_scoring()` for MFL gets some tweaks to support changes to tidyr (thanks, @DavisVaughan!)

## Thanks

Grateful to [@davisderodes](https://github.com/davisderodes),[@DavisVaughan](https://github.com/DavisVaughan), [@draftydesert277](https://github.com/draftydesert277),[@scottfrechette](https://github.com/scottfrechette), and [@TheMathNinja](https://github.com/TheMathNinja) for their contributions to this release - feedback, issues, and PRs!

---

# ffscrapr 1.4.6

The main goal of v1.4.6 is to patch some minor bugs, introduce a unified dependency on nflreadr, and add some support for kicker scoring.
Expand Down
2 changes: 1 addition & 1 deletion R/0_generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ ff_connect <- function(platform = "mfl", league_id = NULL, ...) {
"fleaflicker" = ,
"flea" = fleaflicker_connect(league_id = league_id, ...),
"espn" = espn_connect(league_id = league_id, ...),
# 'yahoo' = yahoo_connect(league_id = league_id,...)
"sleeper" = sleeper_connect(league_id = league_id, ...),
"mfl" = mfl_connect(league_id = league_id, ...)
# 'yahoo' = stop("Y YOU YAHOO, YOU YAHOO?")
)

if (is.null(x)) stop("We can't connect to that platform yet!")
Expand Down
13 changes: 13 additions & 0 deletions R/0_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ NULL
#' @export
#' @importFrom magrittr %>%
NULL

#' date utils
#' @keywords internal
.as_datetime <- function(x){
origin <- structure(0, class = c("POSIXct", "POSIXt"), tzone = "UTC")
as.POSIXct(x,origin = origin, tz = "UTC")
}

#' date utils
#' @keywords internal
.as_date <- function(x){
as.Date(x, tz = "UTC")
}
4 changes: 4 additions & 0 deletions R/espn__helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,21 @@
c(
"3" = "passingYards",
"4" = "passingTouchdowns",
"8" = "passing25Yards",
"19" = "passing2PtConversions",
"20" = "passingInterceptions",
"24" = "rushingYards",
"25" = "rushingTouchdowns",
"26" = "rushing2PtConversions",
"28" = "rushing10Yards",
"35" = "rushing40YardTD",
"37" = "rushing100YardGame",
"38" = "rushing200YardGame",
"42" = "receivingYards",
"43" = "receivingTouchdowns",
"44" = "receiving2PtConversions",
"45" = "receiving40YardTD",
"48" = "receiving10Yards",
"53" = "receivingReceptions",
"56" = "receiving100YardGame",
"57" = "receiving200YardGame",
Expand Down Expand Up @@ -229,6 +232,7 @@
"104" = "interceptionReturnTouchdown",
"114" = "kickoffReturnYards",
"115" = "puntReturnYards",
"122" = "defensive22To27PointsAllowed",
"123" = "defensive28To34PointsAllowed",
"124" = "defensive35To45PointsAllowed",
"125" = "defensive46+PointsAllowed",
Expand Down
2 changes: 1 addition & 1 deletion R/espn_draft.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ff_draft.espn_conn <- function(conn, ...) {
tidyr::unnest_wider("picks") %>%
dplyr::mutate_at(
dplyr::vars(dplyr::contains("completeDate")),
~ lubridate::as_datetime(.x / 1000)
~ .as_datetime(.x / 1000)
) %>%
dplyr::rename(
"player_id" = "playerId",
Expand Down
6 changes: 3 additions & 3 deletions R/espn_potentialpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#' @examples
#' \donttest{
#' try({ # try only shown here because sometimes CRAN checks are weird
#' conn <- espn_connect(season = 2020, league_id = 899513)
#' espn_potentialpoints(conn, weeks = 1:3)
#' conn <- espn_connect(season = 2021, league_id = 950665)
#' espn_potentialpoints(conn, weeks = 1)
#' }) # end try
#' }
#' @export
Expand Down Expand Up @@ -102,7 +102,7 @@ espn_potentialpoints <- function(conn, weeks = 1:17) {
.espn_pp_lineupkeys <- function() {
tibble::tibble(
lineup_id = c("0", "2", "3", "4", "5", "6", "7", "16", "17", "20", "21", "23", "8", "9", "10", "11", "24", "12", "13", "14", "15"),
priority = c(1, 2, 5, 3, 6, 4, 8, 9, 10, 0, 0, 11, 12, 13, 16, 14, 15, 17, 18, 19, 20)
priority = c(1, 2, 5, 3, 6, 4, 9, 10, 11, 0, 0, 8, 12, 13, 16, 14, 15, 17, 18, 19, 20)
) %>%
dplyr::arrange(.data$priority) %>%
dplyr::mutate(lineup_id = as.character(.data$lineup_id))
Expand Down
2 changes: 1 addition & 1 deletion R/espn_rosters.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ff_rosters.espn_conn <- function(conn, week = NULL, ...) {
eligible_pos = purrr::map(.data$eligible_pos, ~ .espn_lineupslot_map()[as.character(.x)] %>% unname()),
team = purrr::map_chr(as.character(.data$team), ~ .espn_team_map()[.x]),
player = NULL,
acquisition_date = lubridate::as_datetime(.data$acquisition_date / 1000)
acquisition_date = .as_datetime(.data$acquisition_date / 1000)
) %>%
dplyr::left_join(x = franchises, by = "franchise_id")

Expand Down
21 changes: 18 additions & 3 deletions R/espn_scoringhistory.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
ff_scoringhistory.espn_conn <- function(conn, season = 1999:2020, ...) {
checkmate::assert_numeric(season, lower = 1999, upper = as.integer(format(Sys.Date(), "%Y")))

# Pull in scoring rules for that league
league_rules <-
ff_scoring(conn) %>%
dplyr::left_join(
ffscrapr::nflfastr_stat_mapping %>% dplyr::filter(.data$platform == "espn"),
by = c("stat_name" = "ff_event")
)

## CHECK IF KICKING IS IN THE RULESET FOR THIS LEAGUE, IF SO, USE KICKER

ros <- .nflfastr_roster(season)

ps <- .nflfastr_offense_long(season)
Expand All @@ -40,6 +37,10 @@ ff_scoringhistory.espn_conn <- function(conn, season = 1999:2020, ...) {
.nflfastr_kicking_long(season))
}

if(any(c("passing25Yards","rushing10Yards","receiving10Yards") %in% league_rules$stat_name)){
ps <- .espn_threshold_scoring(ps)
}

ros %>%
dplyr::inner_join(ps, by = c("gsis_id"="player_id","season")) %>%
dplyr::inner_join(league_rules, by = c("metric"="nflfastr_event","pos")) %>%
Expand All @@ -56,3 +57,17 @@ ff_scoringhistory.espn_conn <- function(conn, season = 1999:2020, ...) {
)

}

.espn_threshold_scoring <- function(ps){

thresholds <- ps %>%
dplyr::filter(.data$metric %in% c("passing_yards","rushing_yards","receiving_yards")) %>%
dplyr::mutate(
threshold = ifelse(.data$metric == "passing_yards",25,10),
metric = paste(.data$metric,.data$threshold,sep = "_"),
value = .data$value %/% .data$threshold,
threshold = NULL
)

dplyr::bind_rows(ps,thresholds)
}
2 changes: 1 addition & 1 deletion R/espn_transactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ff_transactions.espn_conn <- function(conn, limit = 1000, ...) {
purrr::set_names("x") %>%
tidyr::hoist("x", "messages", "date") %>%
dplyr::mutate(
timestamp = lubridate::as_datetime(.data$date / 1000),
timestamp = .as_datetime(.data$date / 1000),
x = NULL
) %>%
tidyr::unnest_longer("messages") %>%
Expand Down
2 changes: 1 addition & 1 deletion R/flea_franchises.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ff_franchises.flea_conn <- function(conn) {
"user_avatar" = "avatarUrl",
"user_lastlogin" = "lastSeen"
) %>%
dplyr::mutate_at("user_lastlogin", ~ (as.numeric(.x) / 1000) %>% lubridate::as_datetime()) %>%
dplyr::mutate_at("user_lastlogin", ~ (as.numeric(.x) / 1000) %>% .as_datetime()) %>%
dplyr::select(dplyr::any_of(c(
dplyr::starts_with("division"),
dplyr::starts_with("franchise"),
Expand Down
2 changes: 1 addition & 1 deletion R/flea_scoringhistory.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ff_scoringhistory.flea_conn <- function(conn, season = 1999:2020, ...) {

ps <- .nflfastr_offense_long(season)

if("PK" %in% league_rules$pos){
if("K" %in% league_rules$pos){
ps <- dplyr::bind_rows(
ps,
.nflfastr_kicking_long(season))
Expand Down
4 changes: 2 additions & 2 deletions R/flea_transactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ff_transactions.flea_conn <- function(conn, franchise_id = NULL, ...) {
tidyr::unnest_wider("transactions") %>%
tidyr::hoist("transaction", "type", "franchise" = "team") %>%
dplyr::mutate(
timestamp = (as.numeric(.data$timeEpochMilli) / 1000) %>% lubridate::as_datetime(),
timestamp = (as.numeric(.data$timeEpochMilli) / 1000) %>% .as_datetime(),
timeEpochMilli = NULL,
type = stringr::str_remove(.data$type, "TRANSACTION_") %>% tidyr::replace_na("ADD")
)
Expand Down Expand Up @@ -253,7 +253,7 @@ ff_transactions.flea_conn <- function(conn, franchise_id = NULL, ...) {
tidyr::hoist("franchise", "franchise_id" = "id", "franchise_name" = "name") %>%
dplyr::mutate(
trade_id = .data$id,
timestamp = (as.numeric(.data$approvedOn) / 1000) %>% lubridate::as_datetime()
timestamp = (as.numeric(.data$approvedOn) / 1000) %>% .as_datetime()
) %>%
dplyr::select(-"franchise", -"approvedOn")

Expand Down
8 changes: 4 additions & 4 deletions R/mfl_draft.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ff_draft.mfl_conn <- function(conn, custom_players = deprecated(), ...) {
by = c("player_id")
) %>%
dplyr::transmute(
"timestamp" = lubridate::as_datetime(as.numeric(.data$timestamp)),
"timestamp" = .as_datetime(as.numeric(.data$timestamp)),
.data$round,
.data$pick,
.data$overall,
Expand All @@ -59,7 +59,7 @@ ff_draft.mfl_conn <- function(conn, custom_players = deprecated(), ...) {
.data$player_id,
.data$player_name,
.data$pos,
age = round(as.numeric(lubridate::as_date(.data$timestamp) - .data$birthdate) / 365.25, 1),
age = round(as.numeric(.as_date(.data$timestamp) - .data$birthdate) / 365.25, 1),
.data$team
)
} else {
Expand All @@ -80,7 +80,7 @@ ff_draft.mfl_conn <- function(conn, custom_players = deprecated(), ...) {
by = c("player_id")
) %>%
dplyr::transmute(
"timestamp" = lubridate::as_datetime(as.numeric(.data$timestamp)),
"timestamp" = .as_datetime(as.numeric(.data$timestamp)),
.data$division,
.data$division_name,
.data$round,
Expand All @@ -91,7 +91,7 @@ ff_draft.mfl_conn <- function(conn, custom_players = deprecated(), ...) {
.data$player_id,
.data$player_name,
.data$pos,
age = round(as.numeric(lubridate::as_date(.data$timestamp) - .data$birthdate) / 365.25, 1),
age = round(as.numeric(.as_date(.data$timestamp) - .data$birthdate) / 365.25, 1),
.data$team
)
}
Expand Down
4 changes: 2 additions & 2 deletions R/mfl_players.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mfl_players <- function(conn = NULL) {
tidyr::unnest_wider(1) %>%
dplyr::mutate_at(
"birthdate", ~ as.numeric(.x) %>%
lubridate::as_datetime() %>%
lubridate::as_date()
.as_datetime() %>%
.as_date()
) %>%
dplyr::mutate("age" = round(as.numeric(Sys.Date() - .data$birthdate) / 365.25, 1)) %>%
dplyr::select(
Expand Down
3 changes: 2 additions & 1 deletion R/mfl_schedule.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ff_schedule.mfl_conn <- function(conn, ...) {
"opponent_id",
"opponent_score"
)))

away <- schedule %>%
dplyr::rename_at(dplyr::vars(dplyr::contains("away")), ~ stringr::str_remove(.x, "away_")) %>%
dplyr::rename_at(dplyr::vars(dplyr::contains("home")), ~ stringr::str_replace(.x, "home_", "opponent_")) %>%
Expand All @@ -68,7 +69,7 @@ ff_schedule.mfl_conn <- function(conn, ...) {
dplyr::filter(!is.na(.data$franchise_id))

if("spread" %in% names(full_schedule)){
full_schedule$result[!is.na(full_schedule$spread) && full_schedule$result == "T"] <- NA
full_schedule$result[(!is.na(full_schedule$spread)|full_schedule$spread==0) && full_schedule$result == "T"] <- NA
}

return(full_schedule)
Expand Down
5 changes: 1 addition & 4 deletions R/mfl_scoring.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ ff_scoring.mfl_conn <- function(conn) {
.data$vec_depth == 3 ~ purrr::map_depth(.data$rule, 2, `[[`, 1),
.data$vec_depth == 4 ~ purrr::map_depth(.data$rule, -2, `[[`, 1)
),
rule = dplyr::case_when(
.data$vec_depth == 4 ~ purrr::map(.data$rule, dplyr::bind_rows),
TRUE ~ .data$rule
)
rule = purrr::map(.data$rule, dplyr::bind_rows)
) %>%
dplyr::select(-.data$vec_depth) %>%
tidyr::unnest_wider("rule") %>%
Expand Down
2 changes: 1 addition & 1 deletion R/mfl_transactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ff_transactions.mfl_conn <- function(conn, custom_players = deprecated(), ...) {
purrr::pluck("content", "transactions", "transaction") %>%
tibble::tibble() %>%
tidyr::unnest_wider(1) %>%
dplyr::mutate_at("timestamp", ~ as.numeric(.x) %>% lubridate::as_datetime())
dplyr::mutate_at("timestamp", ~ as.numeric(.x) %>% .as_datetime())

if (nrow(df_transactions) == 0) {
return(NULL)
Expand Down
2 changes: 1 addition & 1 deletion R/sleeper_players.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sleeper_players <- function() {
tibble::tibble() %>%
tidyr::unnest_wider(1) %>%
dplyr::mutate_at("fantasy_positions", ~ purrr::map(.x, as.character) %>% as.character()) %>%
dplyr::mutate_at("birth_date", lubridate::as_date) %>%
dplyr::mutate_at("birth_date", .as_date) %>%
dplyr::mutate(
age = round(as.numeric(Sys.Date() - .data$birth_date) / 365.25, 1),
gsis_id = stringr::str_squish(gsis_id)
Expand Down
6 changes: 3 additions & 3 deletions R/sleeper_transactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ff_transactions.sleeper_conn <- function(conn, week = 1:17, ...) {
dropped = purrr::map(.data$dropped, ~ replace(.x, is.null(.x), NA_character_)),
franchise_id = purrr::map_chr(.data$franchise_id, unlist),
timestamp = .data$timestamp / 1000,
timestamp = lubridate::as_datetime(.data$timestamp)
timestamp = .as_datetime(.data$timestamp)
) %>%
tidyr::pivot_longer(
cols = c("added", "dropped"),
Expand Down Expand Up @@ -167,7 +167,7 @@ ff_transactions.sleeper_conn <- function(conn, week = 1:17, ...) {
dropped = purrr::map(.data$dropped, ~ replace(.x, is.null(.x), NA_character_)),
franchise_id = purrr::map_chr(.data$franchise_id, unlist),
timestamp = .data$timestamp / 1000,
timestamp = lubridate::as_datetime(.data$timestamp),
timestamp = .as_datetime(.data$timestamp),
comment = purrr::map_chr(.data$metadata, `[[`, "notes")
) %>%
tidyr::pivot_longer(
Expand Down Expand Up @@ -238,7 +238,7 @@ ff_transactions.sleeper_conn <- function(conn, week = 1:17, ...) {
"waiver_budget"
))) %>%
dplyr::mutate(
timestamp = (.data$timestamp / 1000) %>% lubridate::as_datetime(),
timestamp = (.data$timestamp / 1000) %>% .as_datetime(),
adds = purrr::map(
.data$adds,
~ tibble::enframe(.x,
Expand Down
8 changes: 4 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ eval <- TRUE
tryCatch(expr = {
download.file("https://github.com/ffverse/ffscrapr-tests/archive/main.zip","f.zip")
download.file("https://github.com/ffverse/ffscrapr-tests/archive/1.4.7.zip","f.zip")
unzip('f.zip', exdir = ".")
httptest::.mockPaths(new = "ffscrapr-tests-main")},
httptest::.mockPaths(new = "ffscrapr-tests-1.4.7")},
warning = function(e) eval <<- FALSE,
error = function(e) eval <<- FALSE)
Expand All @@ -42,7 +42,7 @@ httptest::use_mock_api()
[![CRAN status](https://img.shields.io/cran/v/ffscrapr?style=flat-square&logo=R&label=CRAN)](https://CRAN.R-project.org/package=ffscrapr)
[![Dev status](https://img.shields.io/github/r-package/v/ffverse/ffscrapr/dev?label=dev&style=flat-square&logo=github)](https://ffscrapr.ffverse.com/dev/)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-green.svg?style=flat-square)](https://lifecycle.r-lib.org/articles/stages.html)
[![Codecov test coverage](https://img.shields.io/codecov/c/github/ffverse/ffscrapr?label=codecov&style=flat-square&logo=codecov)](https://codecov.io/gh/ffverse/ffscrapr?branch=main)
[![Codecov test coverage](https://img.shields.io/codecov/c/github/ffverse/ffscrapr?label=codecov&style=flat-square&logo=codecov)](https://app.codecov.io/gh/ffverse/ffscrapr?branch=main)
[![R build status](https://img.shields.io/github/workflow/status/ffverse/ffscrapr/R-CMD-check?label=R%20check&style=flat-square&logo=github)](https://github.com/ffverse/ffscrapr/actions)
[![API status](https://img.shields.io/github/workflow/status/ffverse/ffscrapr/Test%20APIs?label=API%20check&style=flat-square&logo=github)](https://github.com/ffverse/ffscrapr/actions)
[![nflverse discord](https://img.shields.io/discord/789805604076126219?color=7289da&label=nflverse%20discord&logo=discord&logoColor=fff&style=flat-square)](https://discord.com/invite/5Er2FBnnQa)
Expand Down Expand Up @@ -120,5 +120,5 @@ The APIs and data accessed by this package belong to their respective owners, an

```{r include = FALSE}
httptest::stop_mocking()
unlink(c("ffscrapr-tests-main","f.zip"), recursive = TRUE, force = TRUE)
unlink(c("ffscrapr-tests-1.4.7","f.zip"), recursive = TRUE, force = TRUE)
```
Loading

0 comments on commit 3c6a9ae

Please sign in to comment.