Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Apr 25, 2022
0 parents commit 6ecb564
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
^rotc\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^data-raw$
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.Rdata
.httr-oauth
.DS_Store
24 changes: 24 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Package: rotc
Title: Functions to Access OTC Data
Version: 0.0.0.9000
Authors@R:
person("Carl", "Sebastian", , "[email protected]", role = c("aut", "cre"))
Description: A set of functions to access over the cap data.
License: MIT + file LICENSE
Depends:
R (>= 3.6)
Imports:
cli (>= 3.2.0),
dplyr (>= 1.0.8),
httr2 (>= 0.1.1),
janitor (>= 2.1.0),
purrr (>= 0.3.4),
readr (>= 2.1.2),
rlang (>= 1.0.2),
rvest (>= 1.0.2),
stringr (>= 1.4.0),
tidyr (>= 1.2.0),
xml2 (>= 1.3.3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2022
COPYRIGHT HOLDER: rotc authors
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2022 rotc authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(otc_historical_contracts)
export(otc_historical_contracts_all)
import(dplyr)
82 changes: 82 additions & 0 deletions R/otc_historical_contracts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#' Scrape Historical Contracts By Position
#'
#' @param position A valid character string naming the position to scrape
#' historical contracts for.
#'
#' @return A tibble
#' @export
#'
#' @examples
#' \donttest{
#' otc_historical_contracts("QB")
#' }
otc_historical_contracts <- function(position = c("QB", "RB", "FB", "WR",
"TE", "LT", "LG", "C",
"RG", "RT", "IDL", "ED",
"LB", "CB", "S", "K",
"P", "LS")){
position <- rlang::arg_match(position)

cli::cli_progress_step("Scrape {.val {position}}")

html_scrape <- httr2::request("https://overthecap.com/contract-history/") %>%
httr2::req_url_path_append(available_positions[position]) %>%
httr2::req_retry(max_tries = 5) %>%
httr2::req_perform() %>%
httr2::resp_body_html()

hrefs <- xml2::xml_find_all(html_scrape, ".//a") %>%
xml2::xml_attrs() %>%
dplyr::bind_rows() %>%
dplyr::filter(stringr::str_detect(href, "/player/")) %>%
dplyr::pull(href)

contratct_status <- xml2::xml_find_all(html_scrape, ".//tr[.//td]") %>%
xml2::xml_attr("class")

tbl <- rvest::html_table(html_scrape)[[1]] %>%
janitor::remove_empty("cols") %>%
janitor::clean_names() %>%
dplyr::mutate(dplyr::across(
.cols = c(
dplyr::ends_with("value"),
dplyr::ends_with("apy"),
dplyr::starts_with("apy"),
dplyr::ends_with("guaranteed")
),
.fns = readr::parse_number
)) %>%
dplyr::rename(apy_cap_pct = apy_as_percent_of_cap_at_signing) %>%
dplyr::mutate(
apy_cap_pct = apy_cap_pct / 100,
position = position,
player_page = paste0("https://overthecap.com", hrefs),
otc_id = as.integer(stringr::str_extract(hrefs, "[:digit:]+")),
is_active = contratct_status == "active"
) %>%
dplyr::select(player, position, team, is_active, dplyr::everything()) %>%
tidyr::replace_na(list(is_active = FALSE))

tbl
}


#' Scrape Historical Contracts for Multiple Positions
#'
#' @description This is a wrapper around [otc_historical_contracts()] that
#' scrapes and binds multiple positions.
#'
#' @param positions A character vector with valid position names forwarded to
#' [otc_historical_contracts()].
#'
#' @return A tibble
#' @export
#'
#' @examples
#' \donttest{
#' # otc_historical_contracts_all()
#' }
otc_historical_contracts_all <- function(positions = NULL){
if(is.null(positions)) positions <- names(available_positions)
purrr::map_dfr(positions, otc_historical_contracts)
}
7 changes: 7 additions & 0 deletions R/rotc-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @import dplyr
## usethis namespace: end
NULL
11 changes: 11 additions & 0 deletions R/silence_tidy_eval_notes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
utils::globalVariables(
names = c(
"href",
"apy_as_percent_of_cap_at_signing",
"apy_cap_pct",
"player",
"team",
"is_active"
),
package = "rotc"
)
Binary file added R/sysdata.rda
Binary file not shown.
16 changes: 16 additions & 0 deletions data-raw/available_positions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## code to prepare `available_positions` dataset goes here

available_positions <- httr2::request("https://overthecap.com/contract-history/") |>
httr2::req_perform() |>
httr2::resp_body_html() |>
rvest::html_elements("ul") |>
rvest::html_elements(xpath = ".//a") |>
xml2::xml_attrs() |>
stringr::str_extract("(?<=/position/)[:graph:]+") |>
stats::na.omit() |>
as.character() |>
rlang::set_names(
"QB", "RB", "FB", "WR", "TE", "LT", "LG", "C", "RG", "RT", "IDL", "ED",
"LB", "CB", "S", "K", "P", "LS"
)
usethis::use_data(available_positions, overwrite = TRUE, internal = TRUE)
26 changes: 26 additions & 0 deletions man/otc_historical_contracts.Rd

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

24 changes: 24 additions & 0 deletions man/otc_historical_contracts_all.Rd

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

15 changes: 15 additions & 0 deletions man/rotc-package.Rd

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

22 changes: 22 additions & 0 deletions rotc.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

0 comments on commit 6ecb564

Please sign in to comment.