forked from danmax/r34-scraper
added a function to download images to current directory
This commit is contained in:
parent
137378beb3
commit
b5a70e3426
45
src/main.rs
45
src/main.rs
@ -3,11 +3,13 @@ pub mod args;
|
|||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use futures::{stream, StreamExt};
|
use futures::{stream, StreamExt};
|
||||||
|
use indicatif::ProgressBar;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
|
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> ExitCode {
|
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"
|
"\x1b[30m[{i: >4}/{urls_ammount}] \x1b[1;31mimage url not found\x1b[0m"
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
this_prog.finish_with_message(format!(
|
download_file(&img_url, this_prog, i, urls_ammount).await;
|
||||||
"\x1b[30m[{i: >4}/{urls_ammount}] \x1b[32mfound image url: {img_url}\x1b[0m"
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
break img_url;
|
break;
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
this_prog
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user