added a function to download images to current directory

This commit is contained in:
danmax 2024-10-19 22:24:30 -04:00
parent 137378beb3
commit b5a70e3426

View File

@ -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<String, &'static str> {
}
}
}
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();
}
}
}