update get_response
This commit is contained in:
@@ -26,7 +26,7 @@ fn main() {
|
||||
web_parser::download_images(idx, &name);
|
||||
idx += 1;
|
||||
}
|
||||
file::write_file("cache/index", Bytes::from(idx.to_string())).expect(&format!("Write index error: {}", idx));
|
||||
thread::sleep(Duration::from_millis(300));
|
||||
}
|
||||
file::write_file("cache/index", Bytes::from(idx.to_string())).expect(&format!("Write index error: {}", idx));
|
||||
}
|
||||
@@ -3,14 +3,13 @@ use scraper::{Html, Selector};
|
||||
use crate::file;
|
||||
|
||||
|
||||
fn get_response(url: &str) -> Bytes {
|
||||
let response = reqwest::blocking::get(url).expect("Could not load url.");
|
||||
fn get_response(url: &str) -> Result<Bytes, reqwest::Error> {
|
||||
let response = reqwest::blocking::get(url).expect("Server error");
|
||||
let mut body = Bytes::new();
|
||||
if response.status() == 200 {
|
||||
let body = response.bytes().unwrap();
|
||||
return body;
|
||||
} else {
|
||||
return Bytes::new();
|
||||
body = response.bytes().unwrap()
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
pub(crate) fn get_html_title(body: String) -> String {
|
||||
@@ -24,11 +23,13 @@ pub(crate) fn download_image(url_path: &str, file_path: &str) -> bool {
|
||||
let url = format!("https://i.tuiimg.net/{}", url_path);
|
||||
println!("{}", url);
|
||||
let response = get_response(&url);
|
||||
if response.is_empty() {
|
||||
return false;
|
||||
let file_content;
|
||||
match response {
|
||||
Ok(value) => file_content = value,
|
||||
Err(_e) => return false,
|
||||
}
|
||||
let file_path = format!("images/{}", file_path);
|
||||
file::write_file(&file_path, response).expect("Write file error");
|
||||
file::write_file(&file_path, file_content).expect("Write file error");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user