-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
41 lines (38 loc) · 1.28 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
library(httr2)
library(stringr)
#Function to set up folder and get input for a given day
set_up_day <- function(day) {
#create folder
main_dir <- getwd()
day_folder <-
c(paste0("day_", stringr::str_pad(day, 2, "left", "0")))
task_folder <- c("00_input", "01_script")
directories <-
vapply(task_folder,
\(x) paste0(day_folder, "/", x),
character(1),
USE.NAMES = FALSE)
invisible(lapply(c(day_folder,directories), \(x) {
if (!file.exists(x)) {
dir.create(file.path(main_dir, x))
}
}))
message("Folder structure has been created")
# #Get the input file
url <- paste0("https://adventofcode.com/2023/day/", day, "/input")
session_cookie <- Sys.getenv("SESSION")
req <- httr2::request(url) |>
httr2::req_headers(Cookie = paste0("session=", session_cookie)) |>
httr2::req_cache(path = tempdir()) |>
httr2::req_retry(max_tries = 5)
withCallingHandlers(
file_content <- httr2::req_perform(req) |>
httr2::resp_body_string() |>
strsplit("\\n") |> unlist() |>
readr::write_lines(file.path(main_dir,
paste0(day_folder, "/00_input/input.txt"))),
httr2_http_404 = function(cnd) {
rlang::abort("Couldn't find input data for the day", parent = cnd)
}
)
}