Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working on players as source of truth #71

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/_archives/update-players.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
build_players <- function() {
cli::cli_alert_info("Scraping players...")

# thank you tan lmao

player_list <- purrr::map(LETTERS, \(search_term) {
httr::GET(
httr::modify_url(
"https://nextgenstats.nfl.com/api/league/player/search",
query = list(term = search_term)
),
httr::add_headers(.headers = ngsscrapR::default_headers),
httr::set_cookies(.cookies = ngsscrapR::default_cookies)
) |>
httr::content(as = "text", encoding = "UTF-8") |>
jsonlite::parse_json()
})

suppressWarnings({
# this throws a warning for filling NA columns like `suffix`
players <- player_list |>
purrr::map("players") |>
unlist(recursive = FALSE) |>
data.table::rbindlist(use.names = TRUE, fill = TRUE) |>
dplyr::distinct(gsisId, .keep_all = TRUE) |>
tibble::as_tibble(.name_repair = janitor::make_clean_names) |>
dplyr::arrange(display_name) |>
dplyr::mutate(headshot = gsub("\\{formatInstructions\\}", "f_auto,q_auto", headshot))
})

cli::cli_process_start("Uploading players to nflverse-data")

nflversedata::nflverse_save(
data_frame = players,
file_name = "players",
nflverse_type = "players",
release_tag = "players"
)

cli::cli_process_done()
}

build_players()
89 changes: 52 additions & 37 deletions src/update-players.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
#' nflverse players
#'
#' This function builds the nflverse players table. It uses `gsis_id` as primary
#' key and combines the following in priority order:
#'
#' - manual corrections (?), if available
#' - NGS API
#' - Shield API
#' - PFF
#' - ffverse player ids
#' - PFR
#' - OTC
#' - ...?
#' - existing/historical nflverse players

build_players <- function() {
cli::cli_alert_info("Scraping players...")

# thank you tan lmao

player_list <- purrr::map(LETTERS, \(search_term) {
httr::GET(
httr::modify_url(
"https://nextgenstats.nfl.com/api/league/player/search",
query = list(term = search_term)
),
httr::add_headers(.headers = ngsscrapR::default_headers),
httr::set_cookies(.cookies = ngsscrapR::default_cookies)
) |>
httr::content(as = "text", encoding = "UTF-8") |>
jsonlite::parse_json()
})

suppressWarnings({
# this throws a warning for filling NA columns like `suffix`
players <- player_list |>
purrr::map("players") |>
unlist(recursive = FALSE) |>
data.table::rbindlist(use.names = TRUE, fill = TRUE) |>
dplyr::distinct(gsisId, .keep_all = TRUE) |>
tibble::as_tibble(.name_repair = janitor::make_clean_names) |>
dplyr::arrange(display_name) |>
dplyr::mutate(headshot = gsub("\\{formatInstructions\\}", "f_auto,q_auto", headshot))
})

cli::cli_process_start("Uploading players to nflverse-data")

nflversedata::nflverse_save(
data_frame = players,
file_name = "players",
nflverse_type = "players",
release_tag = "players"
)

cli::cli_process_done()
## existing nflverse players
nflverse_players <- nflreadr::load_players()

## NGS rosters
ngs_current_rosters <- purrr::map(
2016:nflreadr::get_current_season(roster = TRUE),
purrr::possibly(~ngsscrapR::scrape_current_roster(teamId = "ALL", season = .x, status = "ALL"), otherwise = tibble::tibble()),
.progress = TRUE
) |>
data.table::rbindlist(use.names = TRUE, fill = TRUE) |>
dplyr::arrange(-season) |>
dplyr::group_by(gsis_id, display_name) |>
tidyr::fill(-c(season, week, season_type), .direction = "downup") |>
dplyr::distinct(gsis_id, display_name, .keep_all = TRUE)

## current PFF players
pff_players <- nflreadr::rds_from_url("https://github.com/nflverse/nflverse-data/releases/download/players_components/pff_players.rds")

## ffverse player ids
ffverse_player_ids <- nflreadr::load_ff_playerids()

## PFR ?
## Shield API ? (just use rosters?)
## Data Exchange ? (just use rosters?)
## OTC ? (load_contracts (?))

# cli::cli_process_start("Uploading players to nflverse-data")
#
# nflversedata::nflverse_save(
# data_frame = players,
# file_name = "players",
# nflverse_type = "players",
# release_tag = "players"
# )
#
# cli::cli_process_done()

}

build_players()