@@ -48,45 +48,59 @@ SearchResults <- R6::R6Class("SearchResults",
4848 # ' @param force Optional; forces the download even if the file already exists in the specified output directory.
4949 # ' @param prompt Optional; enables all user prompts for decisions during file downloads. Defaults to true.
5050 # ' @return Nothing returned but downloaded files are saved at the specified location.
51+ # ' @importFrom progress progress_bar
5152 # ' @export
5253 download = function (output_dir , selected_indexes , stop_at_failure = TRUE , force = FALSE , prompt = TRUE ) {
5354 if (self $ total_count == 0 || (prompt && ! private $ prompt_user_confirmation(self $ total_size ))) {
5455 return (NULL )
5556 }
5657
57- message(" [Download] Start" )
58-
5958 if (! dir.exists(output_dir )) {
6059 dir.create(output_dir )
6160 }
6261
6362 resources_to_download <- if (missing(selected_indexes )) self $ results else self $ results [selected_indexes ]
63+
64+ proggress_bar <- progress :: progress_bar $ new(
65+ format = " [Download] :current/:total (:percent) :elapsed" ,
66+ total = length(resources_to_download ),
67+ clear = FALSE ,
68+ width = 60
69+ )
70+
6471 i <- 0
65- should_break <- FALSE
6672 for (r in resources_to_download ) {
67- if (should_break ) {
68- break
69- }
70-
7173 i <- i + 1
7274
73- message(paste0(" [Download] Downloading file " , i , " /" , length(resources_to_download )))
74-
7575 tryCatch(
7676 {
7777 download_id <- private $ get_download_id(r )
7878 is_ready <- private $ ensure_download_is_ready(download_id )
7979 if (is_ready ) {
8080 private $ download_resource(download_id , output_dir , force )
8181 }
82+ proggress_bar $ tick()
8283 },
8384 error = function (err ) {
84- warning(" Error during the downloading:" , err , sep = " \n " )
85- should_break <<- stop_at_failure
85+ if (stop_at_failure ) {
86+ proggress_bar $ terminate()
87+ stop(conditionMessage(err ))
88+ } else {
89+ err_msg <- conditionMessage(err )
90+ message(err_msg )
91+
92+ # Special handling for HTTP 429
93+ if (grepl(" \\ [HTTP 429\\ ]" , err_msg )) {
94+ proggress_bar $ terminate()
95+ stop(sprintf(" Operation halted due to rate limit (HTTP 429). Please slow down your requests." ))
96+ }
97+
98+ proggress_bar $ message(conditionMessage(err ))
99+ proggress_bar $ tick()
100+ }
86101 }
87102 )
88103 }
89- message(" [Download] DONE" )
90104 }
91105 ),
92106 private = list (
@@ -132,7 +146,7 @@ SearchResults <- R6::R6Class("SearchResults",
132146
133147 # Check if the file already exists and force flag is FALSE
134148 if (file.exists(file_path ) && ! force ) {
135- warning(" File already exists:" , file_path , " - Skipping download.\n " )
149+ # warning("File already exists:", file_path, "- Skipping download.\n")
136150 return (NA )
137151 }
138152
@@ -141,7 +155,12 @@ SearchResults <- R6::R6Class("SearchResults",
141155
142156 resp <- private $ client $ send_request(req , TRUE )
143157 if (resp $ status_code != 200 ) {
144- stop(paste(" Couldn't download: " , url ))
158+ stop(sprintf(
159+ " Failed to download data.\n URL: %s\n Status code: %s\n Reason: %s" ,
160+ url ,
161+ resp $ status_code ,
162+ resp $ status_text %|| % " Unknown"
163+ ))
145164 }
146165
147166 writeBin(httr2 :: resp_body_raw(resp ), file_path )
@@ -150,7 +169,7 @@ SearchResults <- R6::R6Class("SearchResults",
150169 prompt_user_confirmation = function (total_size ) {
151170 if (total_size > = private $ LARGE_DOWNLOAD_SIZE ) {
152171 repeat {
153- message(" The total size is " , humanize :: natural_size (total_size ), " . Do you want to proceed? (Y/N): " )
172+ message(" The total size is " , scales :: label_bytes() (total_size ), " . Do you want to proceed? (Y/N): " )
154173 answer <- tolower(readLines(n = 1 ))
155174 if (answer %in% c(" y" , " n" )) {
156175 return (answer == " y" )
@@ -169,7 +188,12 @@ SearchResults <- R6::R6Class("SearchResults",
169188
170189 resp <- private $ client $ send_request(req , TRUE )
171190 if (resp $ status_code != 200 ) {
172- stop(paste(" Couldn't download: " , url ))
191+ stop(sprintf(
192+ " Failed to download data.\n URL: %s\n Status code: %s\n Reason: %s" ,
193+ url ,
194+ resp $ status_code ,
195+ resp $ status_text %|| % " Unknown"
196+ ))
173197 }
174198
175199 # Extract the file name from the Content-Disposition header
0 commit comments