hanimers/src/commands/view.rs

81 lines
2.2 KiB
Rust
Raw Normal View History

2021-02-02 16:52:15 +00:00
use crate::utils;
use clap::ArgMatches;
use reqwest::redirect::Policy;
2022-04-30 11:30:36 +00:00
use reqwest::Client;
use std::process::exit;
2021-02-02 16:52:15 +00:00
extern crate tokio;
2022-04-30 11:30:36 +00:00
pub async fn view(arg_m: &ArgMatches) {
2021-02-02 16:52:15 +00:00
let policy = Policy::custom(|attempt| {
if attempt.previous().len() > 10 {
attempt.error("too many redirects")
} else if attempt.url().path() == "/404" {
attempt.stop()
} else {
attempt.follow()
}
});
let client = Client::builder().redirect(policy).build().unwrap();
2022-04-30 11:30:36 +00:00
let handles = arg_m
.values_of("id")
.unwrap()
.map(|id| {
let cloned_client = client.clone();
let id = id.to_string();
let cid = id.clone();
(
tokio::spawn(async move { utils::get_hentai(cloned_client, &cid).await }),
id,
)
})
.collect::<Vec<_>>();
2021-02-02 16:52:15 +00:00
let mut fail = false;
let mut one_done = false;
for handle in handles {
2021-02-04 03:43:32 +00:00
let (handle, id) = handle;
let hentai = match handle.await {
Ok(hentai) => hentai,
Err(err) => {
if one_done {
eprintln!("");
}
eprintln!("ID: {}\nError: {}", id, err);
fail = true;
one_done = true;
continue;
}
};
2021-02-02 16:52:15 +00:00
match hentai {
Ok(hentai) => {
match hentai {
Some(hentai) => {
if one_done {
println!("");
}
println!("{}", &hentai);
2022-04-30 11:30:36 +00:00
}
2021-02-02 16:52:15 +00:00
None => {
if one_done {
eprintln!("");
}
eprintln!("ID: {}\nError: does not exist", id);
fail = true;
}
};
2022-04-30 11:30:36 +00:00
}
2021-02-02 16:52:15 +00:00
Err(err) => {
if one_done {
eprintln!("");
}
eprintln!("ID: {}\nError: {}", id, err);
fail = true;
}
};
one_done = true;
}
if fail {
exit(1);
}
}