Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tanho63 authored Jul 11, 2023
0 parents commit aa10960
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
41 changes: 41 additions & 0 deletions .github/workflows/update_adp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
workflow_dispatch:
#schedule:
# - cron: 0,10,20,30,40,50 * * 7 *

name: update_adp

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2

- name: Run adp update
run: |
source("update_mfl.R")
source("update_sleeper.R")
shell: Rscript {0}
50 changes: 50 additions & 0 deletions .github/workflows/update_site.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
workflow_dispatch:
#schedule:
# - cron: 0,10,20,30,40,50 * * 7 *

name: update_site

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r-dependencies@v2

- name: Build site
run: |
rmarkdown::render_site()
shell: Rscript {0}

- name: Commit and push
run: |
git config --global user.email "actions@github.com"
git config --global user.name "Actions User"
git add docs
git commit -m "adp site update `date`"
git push origin gh-pages
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.Rdata
.httr-oauth
.DS_Store
.Renviron
output
20 changes: 20 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Package: data.sfb12
Title: SFB12 Data
Version: 1.0.0
Authors@R:
person("Tan", "Ho", , "tan@tanho.ca", role = c("aut", "cre","cph"))
Description: Functions and data for automatically updating SFB ADP and draft pick data.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
Imports:
data.table,
dplyr,
ffscrapr,
piggyback,
stringr,
cli,
tidyr,
purrr
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 data.sfb12 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.
21 changes: 21 additions & 0 deletions data-sfb12.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
29 changes: 29 additions & 0 deletions league_ids_mfl.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"league_name","league_id"
"#SFB12 23","12549"
"#SFB12 5","14074"
"#SFB12 4","14861"
"#SFB12 12","16404"
"#SFB12 14","20921"
"#SFB12 26","21984"
"#SFB12 10","25400"
"#SFB12 27","28405"
"#SFB12 22","28447"
"#SFB12 11","29019"
"#SFB12 17","43482"
"#SFB12 9","46048"
"#SFB12 21","46640"
"#SFB12 18","51676"
"#SFB12 3","52637"
"#SFB12 16","55021"
"#SFB12 20","55369"
"#SFB12 19","58678"
"#SFB12 2","59098"
"#SFB12 1","59464"
"#SFB12 13","59640"
"#SFB12 8","59711"
"#SFB12 15","62083"
"#SFB12 28","62858"
"#SFB12 6","66174"
"#SFB12 25","67703"
"#SFB12 24","68881"
"#SFB12 7","73143"
32 changes: 32 additions & 0 deletions league_ids_sleeper.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
league_id,league_name
851201790119612416,#SFB12 Denver
850083878629650432,"#SFB12 Titan, Marvel"
850083448797364224,#SFB12 New New York
850082872147718144,#SFB12 Bikini Bottom
850082477857976320,#SFB12 Eagan
850055201854459904,#SFB12 Los Angeles Live 4
850054059019837440,#SFB12 Los Angeles Live 2
850052814938935296,#SFB12 Los Angeles Live 1
850050813224157184,#SFB12 Boston Live 2
850049372166135808,#SFB12 Vancouver Live 2
850048770828787712,#SFB12 Vancouver Live 1
850046604609191936,#SFB12 Boston Live 1
850046076336615424,#SFB12 Orlando Live 2 Magic Kingdom Division
850045295726280704,#SFB12 Orlando Live 1 Epcot Division
849436298744807424,#SFB12 Canton Live 3
849435946746220544,#SFB12 Canton Live 2
849435240047968256,#SFB12 Canton Live 1
848677432117547008,#SFB12 Seattle Live
848670866949644288,#SFB12 Buffalo Live 2
848670056069066752,#SFB12 Buffalo Live 1
848661473927499776,#SFB12 Minneapolis Live 4
848661089368530944,#SFB12 Minneapolis Live 3
848659898794348544,#SFB12 Minneapolis Live 2
819245597386240000,#SFB12 Minneapolis Live 1
848322512864518144,#SFB12 San Francisco Live 2
848321834251300864,#SFB12 San Francisco Live 1
848316511184625664,#SFB12 Chicago Live 3
848316419237052416,#SFB12 Chicago Live 2
848316313632907264,#SFB12 Chicago Live 1
848311681724583936,#SFB12 Atlanta Live 2
848310554803179520,#SFB12 Atlanta Live 1
Empty file added output/.placeholder
Empty file.
76 changes: 76 additions & 0 deletions update_mfl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
library(ffscrapr)
library(dplyr)
library(data.table)
library(tidyr)
library(purrr)
library(stringr)
library(piggyback)
library(cli)

options(dplyr.summarise.inform = FALSE,
piggyback.verbose = FALSE)

mfl_user_agent <- function(league_id){
paste0(Sys.getenv("MFL_USER_AGENT"),league_id)
}

# leagues <- mfl_getendpoint(mfl_connect(2022),"leagueSearch",SEARCH="#SFB12") |>
# pluck("content","leagues","league") |>
# rbindlist() |>
# filter(str_detect(name,"\\#SFB12 [0-9]+")) |>
# select(-homeURL, league_name = name, league_id = id)
#
# fwrite(leagues,"mfl_league_ids.csv",quote = TRUE)

leagues <- fread("league_ids_mfl.csv")

get_draft <- function(league_id){
cli::cli_alert("League ID: {league_id}")
conn <- mfl_connect(2022, league_id, user_agent = mfl_user_agent(league_id), rate_limit = F)
ff_draft(conn)
}

drafts <- leagues |>
mutate(drafts = map(league_id, possibly(get_draft, otherwise = tibble()))) |>
unnest(drafts) |>
mutate(
division_name = case_when(
league_id == 58678 & division == "02" ~ "ATLANTIS, ATLANTIS",
TRUE ~ division_name
)
)

fwrite(drafts,"output/draft_picks_mfl.csv",quote = TRUE)
update_time <- format(Sys.time(), tz = "America/Toronto", usetz = TRUE)
writeLines(update_time, "output/timestamp.txt")
pb_upload("output/draft_picks_mfl.csv", repo = "dynastyprocess/data-sfb12", tag = "data-mfl")
pb_upload("output/timestamp.txt", repo = "dynastyprocess/data-sfb12", tag = "data-mfl")

drafts <- fread("output/draft_picks_mfl.csv")

adp <- drafts |>
filter(!is.na(player_id)) |>
group_by(league_id,division,pos) |>
mutate(pos_rank = rank(overall)) |>
group_by(player_id, player_name, pos, team) |>
summarise(
n = n(),
overall_avg = mean(overall, na.rm = TRUE) |> round(2),
overall_sd = sd(overall, na.rm = TRUE) |> round(2),
pos_avg = mean(pos_rank, na.rm = TRUE) |> round(2),
pos_sd = sd(pos_rank, na.rm = TRUE) |> round(2),
overall_min = min(overall, na.rm = TRUE),
overall_max = max(overall, na.rm = TRUE),
pos_min = min(pos_rank, na.rm = TRUE),
pos_max = max(pos_rank, na.rm = TRUE)
) |>
ungroup() |>
arrange(overall_avg,-n)

fwrite(adp,"output/adp_mfl.csv")
update_time <- format(Sys.time(), tz = "America/Toronto", usetz = TRUE)
writeLines(update_time, "output/timestamp.txt")
pb_upload("output/adp_mfl.csv", repo = "dynastyprocess/data-sfb12", tag = "data-mfl")
pb_upload("output/timestamp.txt", repo = "dynastyprocess/data-sfb12", tag = "data-mfl")

cli::cli_alert_success("Successfully got all picks and ADP!")
59 changes: 59 additions & 0 deletions update_sleeper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
library(ffscrapr)
library(dplyr)
library(data.table)
library(tidyr)
library(purrr)
library(stringr)
library(piggyback)
library(cli)

leagues <- sleeper_userleagues("ScottFishBowl", 2022) |>
select(league_id, league_name) |>
filter(str_detect(league_name,"\\#SFB12")) |>
mutate(league_id = as.character(league_id))

fwrite(leagues,"league_ids_sleeper.csv")

get_draft <- function(league_id){
cli::cli_alert("League ID: {league_id}")
conn <- sleeper_connect(2022, league_id, rate_limit = F)
suppressWarnings(ff_draft(conn))
}

drafts <- leagues |>
select(league_id, league_name) |>
mutate(drafts = map(league_id, possibly(get_draft, otherwise = tibble()))) |>
unnest(drafts) |>
group_by(league_id) |>
mutate(overall = row_number()) |>
ungroup()


adp <- drafts |>
filter(!is.na(player_id)) |>
group_by(league_id) |>
mutate(overall = row_number()) |>
group_by(league_id,pos) |>
mutate(pos_rank = rank(overall)) |>
group_by(player_id, player_name, pos, team) |>
summarise(
n = n(),
overall_avg = mean(overall, na.rm = TRUE) |> round(2),
overall_sd = sd(overall, na.rm = TRUE) |> round(2),
pos_avg = mean(pos_rank, na.rm = TRUE) |> round(2),
pos_sd = sd(pos_rank, na.rm = TRUE) |> round(2),
overall_min = min(overall, na.rm = TRUE),
overall_max = max(overall, na.rm = TRUE),
pos_min = min(pos_rank, na.rm = TRUE),
pos_max = max(pos_rank, na.rm = TRUE)
) |>
ungroup() |>
arrange(overall_avg,-n)

fwrite(drafts,"output/draft_picks_sleeper.csv",quote = TRUE)
fwrite(adp,"output/adp_sleeper.csv",quote = TRUE)
update_time <- format(Sys.time(), tz = "America/Toronto", usetz = TRUE)
writeLines(update_time, "output/timestamp.txt")
pb_upload("output/draft_picks_sleeper.csv", repo = "dynastyprocess/data-sfb12", tag = "data-sleeper")
pb_upload("output/adp_sleeper.csv", repo = "dynastyprocess/data-sfb12", tag = "data-sleeper")
pb_upload("output/timestamp.txt", repo = "dynastyprocess/data-sfb12", tag = "data-sleeper")

0 comments on commit aa10960

Please sign in to comment.