Skip to content

Commit 31f3db7

Browse files
committed
Starting cache for stats with CBS scrape
1 parent 03d9356 commit 31f3db7

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

R/caching_helpers.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,25 @@ ensure_cache_dir_exists = function() {
9393

9494
clear_cache_by_time = function() {
9595
cache_dir = tools::R_user_dir("ffanalytics", "cache")
96+
97+
# First checking for all files older than 8 hours
9698
file_names = list.files(cache_dir, full.names = TRUE)
9799

98100
if(length(file_names) == 0) {
99101
return(NULL)
100102
}
101-
102103
file_mtimes = file.mtime(file_names)
103-
files_to_clear = difftime(Sys.time(), file_mtimes, units = "hours") > 8
104+
all_files_to_clear = difftime(Sys.time(), file_mtimes, units = "hours") > 8
105+
scrapes_to_clear = grepl("_scrape", basename(file_names), fixed = TRUE) &
106+
difftime(Sys.time(), file_mtimes, units = "hours") > 1
107+
108+
files_to_clear = as.logical(pmax(all_files_to_clear, scrapes_to_clear))
109+
104110

105111
if(any(files_to_clear, na.rm = TRUE)) {
106112
file.remove(file_names[files_to_clear])
107113
}
114+
108115
}
109116

110117
cache_file_names = c(
@@ -188,7 +195,8 @@ cache_file_names = c(
188195
"ecr_draft_db_ppr.rds" = "ECR Draft DB PPR",
189196
"ecr_weekly_db_std.rds" = "ECR Weekly DB Std",
190197
"ecr_weekly_db_half.rds" = "ECR Weekly DB Half",
191-
"ecr_weekly_db_ppr.rds" = "ECR Weekly DB PPR"
198+
"ecr_weekly_db_ppr.rds" = "ECR Weekly DB PPR",
199+
"cbs_scrape.rds" = "CBS Scrape"
192200
)
193201

194202

R/ffanalytics.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
.onAttach <- function(libname, pkgname) {
2121
packageStartupMessage(
22-
"Note: the ffanalytics package locally caches ADP & ECR data scrapes. Cached scrapes",
23-
"\nolder than 8 hours are dropped (upon checking)",
22+
"Note: the ffanalytics package locally caches ADP & ECR data scrapes for 8 hours,
23+
and projected stats for 1 hour. Older cached scrapes are dropped (upon checking)",
2424
"\n - See ?clear_ffanalytics_cache() for how to manually clear the cache",
2525
"\n - Use list_ffanalytics_cache() to see what is currently cached"
2626
)

R/source_scrapes.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ scrape_cbs = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
1717
scrape_week = week
1818
}
1919

20+
curr_cache = list_ffanalytics_cache(quiet = TRUE)
21+
is_cached = "CBS Scrape" %in% curr_cache$object
22+
23+
if(is_cached) {
24+
l_pos = get_cached_object("cbs_scrape.rds")
25+
26+
pos_match = all(toupper(pos) %in% toupper(sort(names(l_pos))))
27+
28+
if(isTRUE(pos_match)) {
29+
30+
scrape_message = paste0(
31+
"\n",
32+
"Using the CBS scrape that was cached ",
33+
curr_cache$hr_min_since_cache[curr_cache$object == "CBS Scrape"],
34+
" ago:"
35+
)
36+
message(scrape_message)
37+
return(l_pos[pos])
38+
39+
} else {
40+
clear_ffanalytics_cache("CBS Scrape")
41+
}
42+
}
43+
44+
2045
message("\nThe CBS scrape uses a 2 second delay between pages")
2146

2247
base_link = paste0("https://www.cbssports.com/fantasy/football/")
@@ -97,6 +122,8 @@ scrape_cbs = function(pos = c("QB", "RB", "WR", "TE", "K", "DST"), season = NULL
97122
names(l_pos) = pos
98123
attr(l_pos, "season") = season
99124
attr(l_pos, "week") = week
125+
126+
cache_object(l_pos, "cbs_scrape.rds")
100127
l_pos
101128
}
102129

0 commit comments

Comments
 (0)