@@ -10,29 +10,60 @@ library(httr2)
1010Sys.setenv(VROOM_CONNECTION_SIZE = 100000000 )
1111
1212
13- # Robust download with retry and optional content-length validation
14- robust_download_httr2 <- function (url , dest , max_tries = 5 , timeout_secs = 120 ) {
13+
14+ robust_download_httr2 <- function (url , dest ,
15+ max_tries = 5 ,
16+ timeout_secs = 1500 ) {
17+ # browser-style User-Agent
18+ message(" Downloading: " , url )
19+ ua <- " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
20+
1521 req <- request(url ) | >
22+ req_headers(
23+ `User-Agent` = ua ,
24+ Accept = " application/octet-stream"
25+ ) | >
1626 req_timeout(timeout_secs ) | >
17- req_retry(max_tries = max_tries , retry_on_failure = TRUE )
18-
19- resp <- req | > req_perform(path = dest ) # streams to dest; errors on 4xx/5xx automatically
27+ req_retry(max_tries = max_tries , retry_on_failure = TRUE ) | >
28+ req_verbose() # ← turn on full curl logging
29+
30+ resp <- tryCatch(
31+ {
32+ req | > req_perform(path = dest )
33+ },
34+ error = function (e ) {
35+ message(" 🚨 Download failed for: " , url )
36+ message(" • curl error: " , e $ message )
37+ if (file.exists(dest )) {
38+ message(" • partial file size: " ,
39+ file.info(dest )$ size , " bytes" )
40+ }
41+ stop(e ) # re-throw so your script still aborts
42+ }
43+ )
2044
21- # Validate content length if provided
45+ # if we get here, the download succeeded; sanity-check length
2246 hdrs <- resp | > resp_headers()
2347 if (! is.null(hdrs $ `content-length` )) {
2448 expected <- as.numeric(hdrs $ `content-length` )
25- actual <- file.info(dest )$ size
26- if (is.na(actual ) || actual != expected ) {
27- stop(sprintf(" Incomplete download for %s: expected %d bytes but got %d" , url , expected , actual ))
49+ actual <- file.info(dest )$ size
50+ if (actual != expected ) {
51+ stop(sprintf(
52+ " Incomplete download for %s: expected %d bytes but got %d" ,
53+ url , expected , actual
54+ ))
2855 }
2956 }
3057
3158 invisible (dest )
3259}
3360
61+
62+
63+
64+
3465# Helper to download a ZIP and extract it safely
35- download_and_extract_zip_httr2 <- function (url , dest_zip , extract_dir , max_tries = 5 , timeout_secs = 120 ) {
66+ download_and_extract_zip_httr2 <- function (url , dest_zip , extract_dir , max_tries = 5 , timeout_secs = 1500 ) {
3667 robust_download_httr2(url , dest_zip , max_tries = max_tries , timeout_secs = timeout_secs )
3768 if (! file.exists(dest_zip )) stop(sprintf(" Download failed, %s missing" , dest_zip ))
3869 tryCatch({
0 commit comments