This commit is contained in:
lychang
2024-01-12 03:27:04 +08:00
parent daa46336f1
commit eb2547e1c6
2 changed files with 82 additions and 6 deletions

View File

@@ -1,5 +1,17 @@
use bytes::Bytes;
use scraper::{Html, Selector};
use crate::{file, get_response};
use crate::file;
fn get_response(url: &str) -> Bytes {
let response = reqwest::blocking::get(url).expect("Could not load url.");
if response.status() == 200 {
let body = response.bytes().unwrap();
return body;
} else {
return Bytes::new();
}
}
pub(crate) fn get_html_title(body: String) -> String {
let document = Html::parse_document(&body);
@@ -10,13 +22,51 @@ pub(crate) fn get_html_title(body: String) -> String {
pub(crate) fn download_image(url_path: &str, file_path: &str) -> bool {
let url = format!("https://i.tuiimg.net/{}", url_path);
println!("{}",url);
println!("{}", url);
let response = get_response(&url);
if response.is_empty() {
return false
return false;
}
let file_path = format!("images/{}", file_path);
println!("{}",file_path);
file::write_file(&file_path, response).expect("Write file error");
return true
}
return true;
}
pub(crate) fn get_title(uid: i32) -> String {
let body;
let url = "https://www.tuiimg.com/meinv/";
let temp_url = format!("{}{}/", url, uid);
let response = reqwest::blocking::get(temp_url).expect("Could not load url.");
let status_code = response.status();
return if status_code != 200 {
"".parse().unwrap()
} else {
body = response.text().unwrap();
let name = get_html_title(body);
name
};
}
pub(crate) fn download_images(uid: i32, title: &str) {
let page: i32 = uid / 500 + 1;
let page_str;
if page < 10 {
page_str = format!("00{}", page);
} else if page < 100 {
page_str = format!("0{}", page);
} else {
page_str = format!("{}", page);
}
let mut continue_flag = true;
let mut image_path;
let mut file_path;
let mut pic_num = 1;
while continue_flag {
image_path = format!("{}/{}/{}.jpg", page_str, uid, pic_num);
file_path = format!("{}/{}.jpg", title, pic_num);
if !download_image(&image_path, &file_path) {
continue_flag = false;
}
pic_num += 1;
}
}