From b5a70e3426704c118bfbe408b0801a4860cbee51 Mon Sep 17 00:00:00 2001 From: danmax Date: Sat, 19 Oct 2024 22:24:30 -0400 Subject: [PATCH] added a function to download images to current directory --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5449b39..40b7c39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,13 @@ pub mod args; use clap::Parser; use futures::{stream, StreamExt}; +use indicatif::ProgressBar; use regex::Regex; use reqwest::Client; use tokio::time::{sleep, Duration}; use std::process::ExitCode; +use std::io::Write; #[tokio::main] async fn main() -> ExitCode { @@ -98,11 +100,10 @@ async fn main() -> ExitCode { "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[1;31mimage url not found\x1b[0m" )); } else { - this_prog.finish_with_message(format!( - "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[32mfound image url: {img_url}\x1b[0m" - )); + download_file(&img_url, this_prog, i, urls_ammount).await; + } - break img_url; + break; } Err(_) => { this_prog @@ -147,3 +148,39 @@ fn extract_img_url(html: &str) -> Result { } } } + +async fn download_file(img_url: &str, this_prog: ProgressBar, i: usize, urls_ammount: usize) { + let args = args::Args::parse(); + + let file_name = Regex::new(r"[^/]+$") + .unwrap() + .find(img_url) + .map(|m| m.as_str()) + .unwrap(); + + let downl_img = Client::new() + .get(img_url) + .header("User-Agent", &args.user_agent) + .send() + .await + .unwrap() + .bytes() + .await + .unwrap(); + + match std::fs::File::open(file_name) { + Ok(_) => { + this_prog.finish_with_message(format!( + "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[32mfile exists, skipping...\x1b[0m" + )); + } + Err(_) => { + + this_prog.finish_with_message(format!( + "\x1b[30m[{i: >4}/{urls_ammount}] \x1b[32mdownloaded image: {img_url}\x1b[0m" + )); + let mut file = std::fs::File::create(file_name).unwrap(); + file.write_all(&downl_img).unwrap(); + } + } +}