From 5b1d9368dd8f7038d074f5f1edcac673b75795a0 Mon Sep 17 00:00:00 2001 From: Tan Ho <38083823+tanho63@users.noreply.github.com> Date: Tue, 11 Jul 2023 05:07:30 -0400 Subject: [PATCH] add player_id pull (#8) * v0.2.0: add player id * redoc * add envvars to gha script * use bearer auth function --------- Co-authored-by: Sebastian Carl --- .github/workflows/update_otc_player_ids.yaml | 32 ++++++++++++++++++++ .gitignore | 1 + DESCRIPTION | 8 +++-- NAMESPACE | 4 ++- R/otc_player_ids.R | 29 ++++++++++++++++++ R/rotc-package.R | 1 - R/utils-pipe.R | 14 +++++++++ data-raw/release_otc_player_ids.R | 9 ++++++ man/otc_historical_contracts_all.Rd | 13 +++++++- man/otc_player_ids.Rd | 19 ++++++++++++ man/pipe.Rd | 20 ++++++++++++ rotc.Rproj | 2 ++ 12 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/update_otc_player_ids.yaml create mode 100644 R/otc_player_ids.R create mode 100644 R/utils-pipe.R create mode 100644 data-raw/release_otc_player_ids.R create mode 100644 man/otc_player_ids.Rd create mode 100644 man/pipe.Rd diff --git a/.github/workflows/update_otc_player_ids.yaml b/.github/workflows/update_otc_player_ids.yaml new file mode 100644 index 0000000..283beee --- /dev/null +++ b/.github/workflows/update_otc_player_ids.yaml @@ -0,0 +1,32 @@ +on: + schedule: + # runs every day at 7:00 AM UTC = 3AM ET + - cron: '0 7 * * *' + workflow_dispatch: + +name: update_otc + +jobs: + update: + name: update data + runs-on: ubuntu-latest + timeout-minutes: 40 + env: + GITHUB_PAT: ${{ secrets.NFLVERSE_GH_TOKEN }} + OTC_PLAYERID_ENDPOINT: ${{ secrets.OTC_PLAYERID_ENDPOINT }} + OTC_API_KEY: ${{ secrets.OTC_API_KEY }} + steps: + - uses: actions/checkout@v2 + - uses: r-lib/actions/setup-r@v2 + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + cache-version: 1 + extra-packages: | + nflverse/nflverse-data + nflverse/nflreadr + ropensci/piggyback + local::. + any::arrow + any::qs + - name: Run otc update script + run: Rscript data-raw/release_otc_player_ids.R diff --git a/.gitignore b/.gitignore index 565f2b6..a1f40ab 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .Rdata .httr-oauth .DS_Store +.Renviron diff --git a/DESCRIPTION b/DESCRIPTION index 98515b0..8b4bc15 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rotc Title: Functions to Access OTC Data -Version: 0.0.0.9004 +Version: 0.2.0 Authors@R: person("Carl", "Sebastian", , "mrcaseb@gmail.com", role = c("aut", "cre")) Description: A set of functions to access over the cap data. @@ -14,14 +14,16 @@ Imports: dplyr (>= 1.0.8), httr2 (>= 0.1.1), janitor (>= 2.1.0), + jsonlite (>= 1.8.0), + magrittr, purrr (>= 0.3.4), readr (>= 2.1.2), rlang (>= 1.0.2), rvest (>= 1.0.2), - stringi (>= 1.7.6), + stringi (>= 1.7.6), stringr (>= 1.4.0), tidyr (>= 1.2.0), xml2 (>= 1.3.3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.0 +RoxygenNote: 7.2.3 diff --git a/NAMESPACE b/NAMESPACE index f6e81e7..ce1cb36 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,8 @@ # Generated by roxygen2: do not edit by hand +export("%>%") export(otc_historical_contracts) export(otc_historical_contracts_all) export(otc_player_details) -import(dplyr) +export(otc_player_ids) +importFrom(magrittr,"%>%") diff --git a/R/otc_player_ids.R b/R/otc_player_ids.R new file mode 100644 index 0000000..b1e2902 --- /dev/null +++ b/R/otc_player_ids.R @@ -0,0 +1,29 @@ +#' Retrieve player ID mappings +#' +#' @param endpoint endpoint (defaults to environment variable OTC_PLAYERID_ENDPOINT) +#' @param api_key api key (defaults to environment variable OTC_API_KEY) +#' +#' @export +otc_player_ids <- function(endpoint = Sys.getenv("OTC_PLAYERID_ENDPOINT"), + api_key = Sys.getenv("OTC_API_KEY")){ + stopifnot( + length(endpoint) == 1 && nchar(endpoint) > 0, + length(api_key) == 1 && nchar(api_key) > 0 + ) + + resp <- httr2::request(endpoint) %>% + httr2::req_auth_bearer_token(api_key) %>% + httr2::req_retry(max_tries = 3) %>% + httr2::req_perform() + + player_ids <- resp %>% + httr2::resp_body_string() %>% + jsonlite::fromJSON() %>% + dplyr::mutate_all( ~replace(.x, .x %in% c("", 0), NA)) %>% + dplyr::rename( + gsis_it_id = gsis_id, + gsis_id = gsis_player_id + ) + + return(player_ids) +} diff --git a/R/rotc-package.R b/R/rotc-package.R index fea7845..a65cf64 100644 --- a/R/rotc-package.R +++ b/R/rotc-package.R @@ -2,6 +2,5 @@ "_PACKAGE" ## usethis namespace: start -#' @import dplyr ## usethis namespace: end NULL diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 0000000..fd0b1d1 --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,14 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. +NULL diff --git a/data-raw/release_otc_player_ids.R b/data-raw/release_otc_player_ids.R new file mode 100644 index 0000000..04d36c4 --- /dev/null +++ b/data-raw/release_otc_player_ids.R @@ -0,0 +1,9 @@ + +player_ids <- rotc::otc_player_ids() + +nflversedata::nflverse_save( + data_frame = player_ids, + file_name = "otc_players", + nflverse_type = "OverTheCap Player IDs", + release_tag = "players_components" +) diff --git a/man/otc_historical_contracts_all.Rd b/man/otc_historical_contracts_all.Rd index 28d2a62..bc851a5 100644 --- a/man/otc_historical_contracts_all.Rd +++ b/man/otc_historical_contracts_all.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/otc_historical_contracts.R +% Please edit documentation in R/otc_historical_contracts.R, +% R/otc_team_contracts.R \name{otc_historical_contracts_all} \alias{otc_historical_contracts_all} \title{Scrape Historical Contracts for Multiple Positions} \usage{ +otc_historical_contracts_all(positions = NULL) + otc_historical_contracts_all(positions = NULL) } \arguments{ @@ -11,9 +14,14 @@ otc_historical_contracts_all(positions = NULL) \code{\link[=otc_historical_contracts]{otc_historical_contracts()}}.} } \value{ +A tibble + A tibble } \description{ +This is a wrapper around \code{\link[=otc_historical_contracts]{otc_historical_contracts()}} that +scrapes and binds multiple positions. + This is a wrapper around \code{\link[=otc_historical_contracts]{otc_historical_contracts()}} that scrapes and binds multiple positions. } @@ -21,4 +29,7 @@ scrapes and binds multiple positions. \donttest{ # otc_historical_contracts_all() } +\donttest{ +# otc_historical_contracts_all() +} } diff --git a/man/otc_player_ids.Rd b/man/otc_player_ids.Rd new file mode 100644 index 0000000..805577f --- /dev/null +++ b/man/otc_player_ids.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/otc_player_ids.R +\name{otc_player_ids} +\alias{otc_player_ids} +\title{Retrieve player ID mappings} +\usage{ +otc_player_ids( + endpoint = Sys.getenv("OTC_PLAYERID_ENDPOINT"), + api_key = Sys.getenv("OTC_API_KEY") +) +} +\arguments{ +\item{endpoint}{endpoint (defaults to environment variable OTC_PLAYERID_ENDPOINT)} + +\item{api_key}{api key (defaults to environment variable OTC_API_KEY)} +} +\description{ +Retrieve player ID mappings +} diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 0000000..a648c29 --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling \code{rhs(lhs)}. +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal} diff --git a/rotc.Rproj b/rotc.Rproj index 69fafd4..e4e949a 100644 --- a/rotc.Rproj +++ b/rotc.Rproj @@ -20,3 +20,5 @@ BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source PackageRoxygenize: rd,collate,namespace + +UseNativePipeOperator: No