update get_response

This commit is contained in:
lychang
2024-01-12 12:20:44 +08:00
parent 89237f3522
commit 6c594bc461
2 changed files with 11 additions and 10 deletions

View File

@@ -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;
}