Skip to content

Commit 39a9eea

Browse files
committed
Making individual scrapes continue if a pos fails
1 parent 16fe861 commit 39a9eea

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ffanalytics
22
Type: Package
33
Title: Scrape Data For Fantasy Football
4-
Version: 3.1.7.0000
4+
Version: 3.1.8.0000
55
Authors@R: c(person("Dennis", "Andersen",
66
email = "andersen.dennis@outlook.com",
77
role = c("aut")),

R/helper_funcs.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,20 @@ actual_points_scoring = function(season = NULL,
441441
}
442442
nflf_out_df
443443
}
444+
445+
lapply_safe = function(X, FUN, if_error = NULL) {
446+
lapply(X, function(x) {
447+
tryCatch(
448+
expr = FUN(x),
449+
error = function(e) {
450+
print(e)
451+
if_error
452+
}
453+
)
454+
})
455+
}
456+
457+
444458
# devtools::load_all()
445459
# df_out = actual_points_scoring(
446460
# season = 2019,

R/source_scrapes.R

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ scrape_cbs = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
2222
base_link = paste0("https://www.cbssports.com/fantasy/football/")
2323
site_session = rvest::session(base_link)
2424

25-
l_pos = lapply(pos, function(pos) {
26-
scrape_link = paste0("https://www.cbssports.com/fantasy/football/stats/", pos, "/",
25+
l_pos = lapply_safe(pos, function(position) {
26+
scrape_link = paste0("https://www.cbssports.com/fantasy/football/stats/", position, "/",
2727
season, "/", scrape_week, "/projections/nonppr/")
2828

29-
Sys.sleep(2L) # temporary, until I get an argument for honoring the crawl delay
30-
cat(paste0("Scraping ", pos, " projections from"), scrape_link, sep = "\n ")
29+
if(position != pos[1]) {
30+
Sys.sleep(2L)
31+
}
32+
33+
cat(paste0("Scraping ", position, " projections from"), scrape_link, sep = "\n ")
3134

3235
html_page = site_session %>%
3336
session_jump_to(scrape_link) %>%
@@ -43,7 +46,7 @@ scrape_cbs = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
4346
col_names = rename_vec(col_names, cbs_columns)
4447

4548
# Get PID
46-
if(pos == "DST") {
49+
if(position == "DST") {
4750
cbs_id = html_page %>%
4851
rvest::html_elements("span.TeamName a") %>%
4952
rvest::html_attr("href") %>%
@@ -61,7 +64,7 @@ scrape_cbs = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
6164
rvest::html_table() %>%
6265
`names<-`(col_names)
6366

64-
if(pos != "DST") {
67+
if(position != "DST") {
6568
out_df = out_df %>%
6669
tidyr::extract(player, c("player", "pos", "team"),
6770
".*?\\s{2,}[A-Z]{1,3}\\s{2,}[A-Z]{2,3}\\s{2,}(.*?)\\s{2,}(.*?)\\s{2,}(.*)") %>%
@@ -80,7 +83,7 @@ scrape_cbs = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
8083
dst_ids = ff_player_data[ff_player_data$position == "Def", c("id", "team")]
8184
dst_ids$team[dst_ids$team == "OAK"] = "LV"
8285
out_df$id = dst_ids$id[match(cbs_id, dst_ids$team)]
83-
out_df$pos = pos
86+
out_df$pos = position
8487
out_df$src_id = player_ids$cbs_id[match(out_df$id, player_ids$id)]
8588
}
8689

@@ -117,7 +120,7 @@ scrape_nfl = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
117120

118121
site_session = session(base_link)
119122

120-
l_pos = lapply(pos, function(pos) {
123+
l_pos = lapply_safe(pos, function(pos) {
121124
pos_scrape = nfl_pos_idx[pos]
122125

123126
n_records = case_when(
@@ -250,7 +253,7 @@ scrape_fantasysharks <- function(pos = c("QB", "RB", "WR", "TE", "K", "DST", "DL
250253
segment <- 813
251254
}
252255

253-
l_pos <- lapply(pos, function(pos){
256+
l_pos <- lapply_safe(pos, function(pos){
254257

255258
position = dplyr::case_when(
256259
pos %in% "QB" ~ 1,
@@ -328,7 +331,7 @@ scrape_numberfire <- function(pos = c("QB", "RB", "WR", "TE", "K", "DST", "LB",
328331
}
329332

330333

331-
l_pos <- lapply(site_pos, function(pos){
334+
l_pos <- lapply_safe(site_pos, function(pos){
332335

333336
position <- dplyr::case_when(
334337
pos %in% "QB" ~ "qb",
@@ -478,7 +481,7 @@ scrape_walterfootball <- function(pos = c("QB", "RB", "WR", "TE", "K"),
478481
xl_download <- download.file(url = url, destfile = xlsx_file, mode = "wb", quiet = TRUE)
479482

480483

481-
l_pos <- lapply(pos, function(pos){
484+
l_pos <- lapply_safe(pos, function(pos){
482485

483486
cat(paste0("Scraping ", pos, " projections from"), url, sep = "\n ")
484487

@@ -573,7 +576,7 @@ scrape_fleaflicker <- function(pos = c("QB", "RB", "WR", "TE", "K", "DST", "DL",
573576
site_session <- session(base_link)
574577

575578

576-
l_pos <- lapply(pos, function(pos){
579+
l_pos <- lapply_safe(pos, function(pos){
577580

578581
position <- case_when(pos %in% "QB" ~ 4,
579582
pos %in% "RB" ~ 1,
@@ -803,7 +806,7 @@ scrape_fftoday <- function(pos = c("QB", "RB", "WR", "TE", "K", "DST", "DL", "LB
803806
}
804807

805808

806-
l_pos <- lapply(pos, function(pos){
809+
l_pos <- lapply_safe(pos, function(pos){
807810

808811
position = dplyr::case_when(
809812
pos == "QB" ~ 10,
@@ -967,7 +970,7 @@ scrape_fantasypros = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"),
967970
base_link = paste0("https://www.fantasypros.com/nfl/projections")
968971
site_session = rvest::session(base_link)
969972

970-
l_pos = lapply(pos, function(pos) {
973+
l_pos = lapply_safe(pos, function(pos) {
971974
scrape_link = paste0("https://www.fantasypros.com/nfl/projections/",
972975
tolower(pos), scrape_week)
973976

@@ -1057,7 +1060,7 @@ scrape_rtsports = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"),
10571060

10581061
base_url = "https://www.freedraftguide.com/football/draft-guide-rankings-provider.php"
10591062

1060-
l_pos = lapply(pos, function(x) {
1063+
l_pos = lapply_safe(pos, function(x) {
10611064
if(x != pos[1]) {
10621065
Sys.sleep(5)
10631066
}
@@ -1134,7 +1137,7 @@ scrape_espn = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NUL
11341137
slot_nums = c("QB" = 0, "RB" = 2, "WR" = 4, "TE" = 6, "K" = 17, "DST" = 16)
11351138
position = pos
11361139

1137-
l_pos = lapply(position, function(pos){
1140+
l_pos = lapply_safe(position, function(pos){
11381141

11391142
if(pos != position[1]) {
11401143
Sys.sleep(2)
@@ -1280,7 +1283,7 @@ scrape_fantasydata = function(pos = NULL, season = NULL, week = NULL,
12801283

12811284
# FanDuel ----
12821285
scrape_fanduel <- function(pos = c("QB", "RB", "WR", "TE", "K", "DST"),
1283-
season = NULL, week = 0, draft = TRUE, weekly = FALSE) {
1286+
season = NULL, week = NULL, draft = TRUE, weekly = TRUE) {
12841287

12851288
if(is.null(week)) {
12861289
season = get_scrape_year()

0 commit comments

Comments
 (0)