Sleep for 5s if Content-Type does not start with image/

This commit is contained in:
blank X 2021-01-09 17:09:05 +07:00
parent f6ec76baf2
commit 4271a7bac4
4 changed files with 27 additions and 13 deletions

2
Cargo.lock generated
View File

@ -330,7 +330,7 @@ dependencies = [
[[package]]
name = "mangafetchi"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"quick-xml",
"reqwest",

View File

@ -1,6 +1,6 @@
[package]
name = "mangafetchi"
version = "0.1.0"
version = "0.1.1"
authors = ["blank X <theblankx@protonmail.com>"]
edition = "2018"
@ -14,4 +14,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = "0.11"
quick-xml = "0.20"
tokio = { version = "1.0", features = ["rt-multi-thread", "sync"] }
tokio = { version = "1.0", features = ["rt-multi-thread", "sync", "time"] }

View File

@ -7,10 +7,12 @@ use std::process::exit;
use std::path::{Path, PathBuf};
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tokio::time::{sleep, Duration};
extern crate tokio;
extern crate reqwest;
const DOWNLOAD_WORKERS: usize = 5;
const NON_IMAGE_WAIT_TIME: u64 = 5000;
pub async fn run(mut args: env::Args) {
let manga_id = match args.next() {
@ -90,7 +92,12 @@ async fn summon_handles(client: reqwest::Client, mutex: Arc<Mutex<Vec<(String, P
let (url, file_name, referer) = vec.remove(0);
drop(vec);
eprintln!("[DW{}] Downloading {} to {}", worker_id, &url, file_name.display());
utils::download_file(cloned_client, &url, &file_name, &referer).await.unwrap();
loop {
if utils::download_file(cloned_client.clone(), &url, &file_name, &referer).await.unwrap() {
break;
}
sleep(Duration::from_millis(NON_IMAGE_WAIT_TIME)).await;
}
eprintln!("[DW{}] Downloaded {} to {}", worker_id, &url, file_name.display());
}
eprintln!("[DW{}] Down!", worker_id);

View File

@ -543,17 +543,24 @@ fn parse_manganelo_pages(text: &str) -> Vec<String> {
pages
}
pub async fn download_file(client: reqwest::Client, url: &str, file_name: &PathBuf, referer: &str) -> Result<(), reqwest::Error> {
let bytes = client.get(url)
pub async fn download_file(client: reqwest::Client, url: &str, file_name: &PathBuf, referer: &str) -> Result<bool, reqwest::Error> {
let resp = client.get(url)
.header("Referer", referer)
.send()
.await?
.bytes()
.await?;
if !file_name.parent().unwrap().is_dir() {
create_dir(file_name.parent().unwrap()).unwrap();
match resp.headers().get("Content-Type") {
Some(header_value) => {
if header_value.to_str().unwrap().starts_with("image/") {
let bytes = resp.bytes().await?;
if !file_name.parent().unwrap().is_dir() {
create_dir(file_name.parent().unwrap()).unwrap();
}
let mut file = File::create(&file_name).unwrap();
file.write_all(&bytes).unwrap();
return Ok(true);
}
return Ok(false);
},
None => Ok(false)
}
let mut file = File::create(&file_name).unwrap();
file.write_all(&bytes).unwrap();
Ok(())
}