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