diff options
| author | Mariot Tsitoara <[email protected]> | 2021-01-02 11:04:24 +0100 |
|---|---|---|
| committer | Mariot Tsitoara <[email protected]> | 2021-01-02 11:04:24 +0100 |
| commit | c4793a71f03b11e40ad0a1e0fec1cd44cc6bb34e (patch) | |
| tree | 432fe6ae1b0726679aa7128a6b34100e4e360e48 | |
| parent | Use reqwest blocking client (diff) | |
| download | chan-downloader-c4793a71f03b11e40ad0a1e0fec1cd44cc6bb34e.tar.xz chan-downloader-c4793a71f03b11e40ad0a1e0fec1cd44cc6bb34e.zip | |
Fix main return type
| -rw-r--r-- | src/bin.rs | 54 |
1 files changed, 29 insertions, 25 deletions
@@ -20,7 +20,7 @@ fn main() { download_thread(thread, &output).unwrap(); } -fn download_thread(thread_link: &str, output: &str) -> Result<(), Error> { +fn download_thread(thread_link: &str, output: &str) -> Result<String, Error> { let client = Client::builder().user_agent("reqwest").build()?; let workpath = env::current_dir().unwrap(); @@ -34,32 +34,36 @@ fn download_thread(thread_link: &str, output: &str) -> Result<(), Error> { } } - match get_page_content(thread_link, &client) { + let page_string = match get_page_content(thread_link, &client) { Ok(page_string) => { - let (links_iter, number_of_links) = get_image_links(page_string.as_str()); - let pb = ProgressBar::new(number_of_links as u64); - pb.set_style(ProgressStyle::default_bar() - .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg} ({eta})") - .progress_chars("#>-")); - pb.tick(); - for cap in links_iter.step_by(2) { - let img_path = directory.join(&cap[2]); - if !img_path.exists() { - match save_image( - format!("https:{}", &cap[1]).as_str(), - img_path.to_str().unwrap(), - &client, - ) { - Ok(_) => {} - Err(err) => eprintln!("Error: {}", err), - } - } - pb.set_message(&cap[2].to_string()); - pb.inc(1); - } - pb.finish_with_message("Done"); + page_string } Err(err) => eprintln!("Error: {}", err), + }; + let (links_iter, number_of_links) = get_image_links(page_string.as_str()); + let pb = ProgressBar::new(number_of_links as u64); + + pb.set_style(ProgressStyle::default_bar() + .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg} ({eta})") + .progress_chars("#>-")); + pb.tick(); + + for cap in links_iter.step_by(2) { + let img_path = directory.join(&cap[2]); + if !img_path.exists() { + match save_image( + format!("https:{}", &cap[1]).as_str(), + img_path.to_str().unwrap(), + &client, + ) { + Ok(_) => {} + Err(err) => eprintln!("Error: {}", err), + } + } + pb.set_message(&cap[2].to_string()); + pb.inc(1); } - Ok(()) + pb.finish_with_message("Done"); + + Ok(format!("Downloaded: {} in {}", thread_link, output)) } |