update get_response
This commit is contained in:
@@ -14,65 +14,53 @@ struct Album {
|
||||
}
|
||||
|
||||
fn get_response(url: &str) -> Result<Bytes, reqwest::Error> {
|
||||
let client = reqwest::blocking::Client::builder().build()?;
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(Duration::from_secs(30))
|
||||
.build()?;
|
||||
|
||||
let response = client
|
||||
.get(url)
|
||||
.header(
|
||||
"User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
)
|
||||
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
|
||||
.send()?;
|
||||
|
||||
if response.status() == 200 {
|
||||
// 先获取头信息,再获取字节数据
|
||||
let content_encoding = response
|
||||
.headers()
|
||||
.get("content-encoding")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
if !response.status().is_success() {
|
||||
return Err(response.error_for_status().unwrap_err().into());
|
||||
}
|
||||
|
||||
let bytes = response.bytes()?;
|
||||
let content_encoding = response
|
||||
.headers()
|
||||
.get("content-encoding")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let result = if content_encoding.contains("gzip") {
|
||||
// 手动解压缩 gzip 数据
|
||||
use std::io::Read;
|
||||
let mut decoder = flate2::read::GzDecoder::new(&bytes[..]);
|
||||
let mut decompressed = Vec::new();
|
||||
match decoder.read_to_end(&mut decompressed) {
|
||||
Ok(_) => Ok(Bytes::from(decompressed)),
|
||||
Err(e) => {
|
||||
// 如果解压缩失败,返回原始字节数据
|
||||
println!("Gzip decompression failed: {}, using raw data", e);
|
||||
Ok(bytes)
|
||||
}
|
||||
let bytes = response.bytes()?;
|
||||
|
||||
if content_encoding.contains("gzip") {
|
||||
use std::io::Read;
|
||||
let mut decoder = flate2::read::GzDecoder::new(&bytes[..]);
|
||||
let mut decompressed = Vec::new();
|
||||
match decoder.read_to_end(&mut decompressed) {
|
||||
Ok(_) => Ok(Bytes::from(decompressed)),
|
||||
Err(e) => {
|
||||
println!("Gzip decompression failed: {}, using raw data", e);
|
||||
Ok(bytes)
|
||||
}
|
||||
} else {
|
||||
Ok(bytes)
|
||||
};
|
||||
result
|
||||
}
|
||||
} else {
|
||||
Err(reqwest::Error::from(
|
||||
response.error_for_status().unwrap_err(),
|
||||
))
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_title(uid: i32) -> String {
|
||||
let url = "https://www.meitu131.com/meinv/";
|
||||
let temp_url = format!("{}{}/", url, uid);
|
||||
let file_content = get_response(&temp_url);
|
||||
match file_content {
|
||||
Ok(value) => {
|
||||
if value.is_empty() {
|
||||
return "".to_string();
|
||||
}
|
||||
// 直接使用 lossy 转换避免 UTF-8 错误
|
||||
let url = format!("https://www.meitu131.com/meinv/{}/", uid);
|
||||
|
||||
match get_response(&url) {
|
||||
Ok(value) if !value.is_empty() => {
|
||||
let body = String::from_utf8_lossy(&value).to_string();
|
||||
get_html_title(body)
|
||||
}
|
||||
Err(_e) => "".to_string(),
|
||||
_ => "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,27 +72,21 @@ fn get_html_title(body: String) -> String {
|
||||
}
|
||||
|
||||
fn download_image(url: &str, file_path: &str, retry_times: i8) -> i8 {
|
||||
let mut flag = 1;
|
||||
let response = get_response(url);
|
||||
match response {
|
||||
Ok(value) => {
|
||||
if value.is_empty() {
|
||||
flag = 0
|
||||
} else {
|
||||
file::write_file(&file_path, value).expect("Write file error");
|
||||
}
|
||||
match get_response(url) {
|
||||
Ok(value) if !value.is_empty() => {
|
||||
file::write_file(file_path, value).expect("Write file error");
|
||||
1
|
||||
}
|
||||
Err(_e) => flag = 2,
|
||||
Ok(_) => 0, // 空内容
|
||||
Err(_) if retry_times > 1 => {
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
download_image(url, file_path, retry_times - 1)
|
||||
}
|
||||
Err(_) => 2, // 网络错误
|
||||
}
|
||||
if flag == 2 && retry_times > 1 {
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
return download_image(url, file_path, retry_times - 1);
|
||||
}
|
||||
flag
|
||||
}
|
||||
|
||||
pub(crate) fn download_images(uid: i32, title: &str) {
|
||||
println!("Downloading images for {} {}...", uid, title);
|
||||
pub(crate) fn download_images(uid: i32, title: &str) -> bool {
|
||||
let base_url = format!("https://www.meitu131.com/meinv/{}/", uid);
|
||||
let file_content = get_response(&base_url);
|
||||
let body = match file_content {
|
||||
@@ -271,4 +253,7 @@ pub(crate) fn download_images(uid: i32, title: &str) {
|
||||
"下载完成:成功 {} 张,失败 {} 张",
|
||||
successful_downloads, failed_downloads
|
||||
);
|
||||
}
|
||||
|
||||
// 返回执行结果:至少成功下载一张图片返回 true,否则返回 false
|
||||
failed_downloads == 0
|
||||
}
|
||||
Reference in New Issue
Block a user