Skip to content

Commit

Permalink
v1.4.8.17 - mfl_/sleeper_ players return more standardized column types
Browse files Browse the repository at this point in the history
  • Loading branch information
tanho63 committed Mar 9, 2024
1 parent d8a67b1 commit a9c35cb
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
2 changes: 1 addition & 1 deletion 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.8.16
Version: 1.4.8.17
Authors@R:
c(person(given = "Tan",
family = "Ho",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ row per player-team-season (v1.4.8.13) (thanks @john-b-edwards!)
- Bugfix espn `ff_starters()` to return handle multi-week formats (v1.4.8.15) (#421)
(h/t @tonyelhabr 🤠)
- Bugfix espn `ff_starters()` to return less than or equal to max week (v1.4.8.16)
- [BREAKING] `mfl_players()` and `sleeper_players()` outputs now try to return
more standardized column types (v1.4.8.17)

# ffscrapr 1.4.8

Expand Down
45 changes: 42 additions & 3 deletions R/mfl_players.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ mfl_players <- function(conn = NULL) {
conn <- mfl_connect(.fn_choose_season())
}

conn %>%
df_players <- conn %>%
mfl_getendpoint("players", DETAILS = 1) %>%
purrr::pluck("content", "players", "player") %>%
tibble::tibble() %>%
tidyr::unnest_wider(1) %>%
dplyr::mutate_at(
"birthdate", ~ as.numeric(.x) %>%
"birthdate",
~ as.numeric(.x) %>%
.as_datetime() %>%
.as_date()
) %>%
dplyr::mutate("age" = round(as.numeric(Sys.Date() - .data$birthdate) / 365.25, 1)) %>%
dplyr::mutate(
age = round(as.numeric(Sys.Date() - .data$birthdate) / 365.25, 1),
draft_year = as.integer(.data$draft_year)
) %>%
dplyr::select(
dplyr::any_of(c(
"player_id" = "id",
Expand All @@ -50,4 +54,39 @@ mfl_players <- function(conn = NULL) {
dplyr::ends_with("_id"),
dplyr::everything()
)

df_players <- dplyr::bind_rows(.mfl_players_template(), df_players)

return(df_players)
}


.mfl_players_template <- function(){
tibble::tibble(
player_id = character(0),
player_name = character(0),
pos = character(0),
age = numeric(0),
team = character(0),
status = character(0),
draft_year = integer(0),
draft_team = character(0),
draft_round = character(0),
draft_pick = character(0),
stats_global_id = character(0),
fleaflicker_id = character(0),
stats_id = character(0),
cbs_id = character(0),
sportsdata_id = character(0),
rotowire_id = character(0),
rotoworld_id = character(0),
espn_id = character(0),
nfl_id = character(0),
jersey = character(0),
height = character(0),
weight = character(0),
college = character(0),
twitter_username = character(0),
birthdate = as.Date(character(0))
)
}
68 changes: 65 additions & 3 deletions R/sleeper_players.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@ 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", .as_date) %>%
dplyr::mutate_at(c("birth_date", "injury_start_date"), .as_date) %>%
dplyr::mutate(
age = round(as.numeric(Sys.Date() - .data$birth_date) / 365.25, 1),
gsis_id = stringr::str_squish(gsis_id)
gsis_id = stringr::str_squish(gsis_id),
weight = as.integer(weight)
) %>%
dplyr::mutate_at(
c("injury_status", "injury_body_part", "injury_notes"),
as.character
) %>%
dplyr::mutate_at(
dplyr::vars(dplyr::contains("_id")),
as.character
) %>%
dplyr::select(
-dplyr::contains("search"),
-dplyr::contains("first_name"),
-dplyr::contains("last_name"),
-dplyr::contains("metadata")
) %>%
dplyr::select(-dplyr::contains("search"), -dplyr::contains("first_name"), -dplyr::contains("last_name"), -dplyr::contains("metadata")) %>%
dplyr::select(
"player_id",
"player_name" = "full_name",
Expand All @@ -39,5 +53,53 @@ sleeper_players <- function() {
dplyr::everything()
)

df_players <- dplyr::bind_rows(.sleeper_players_template(), df_players)

return(df_players)
}

.sleeper_players_template <- function(){
tibble::tibble(
player_id = character(0),
player_name = character(0),
pos = character(0),
age = numeric(0),
team = character(0),
status = character(0),
active = logical(0),
years_exp = integer(0),
swish_id = character(0),
espn_id = character(0),
fantasy_data_id = character(0),
pandascore_id = character(0),
rotowire_id = character(0),
sportradar_id = character(0),
yahoo_id = character(0),
oddsjam_id = character(0),
dl_trading_id = character(0),
rotoworld_id = character(0),
stats_id = character(0),
gsis_id = character(0),
height = character(0),
weight = integer(0),
fantasy_positions = character(0),
number = integer(0),
depth_chart_order = integer(0),
depth_chart_position = character(0),
practice_description = character(0),
birth_date = as.Date(character(0)),
birth_city = character(0),
birth_state = character(0),
birth_country = character(0),
high_school = character(0),
college = character(0),
practice_participation = character(0),
injury_status = character(0),
injury_start_date = as.Date(character(0)),
injury_body_part = character(0),
injury_notes = character(0),
news_updated = numeric(0),
hashtag = character(0),
sport = character(0)
)
}

0 comments on commit a9c35cb

Please sign in to comment.