diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f965ef1 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,26 @@ +use std::thread; + +use std::time::Duration; + + +mod file; +mod web_parser; + + +fn main() { + let mut continue_flag = true; + let mut idx: i32 = 1; + while continue_flag { + let name = web_parser::get_title(idx); + if name == "" { + continue_flag = false; + } else { + web_parser::download_images(idx, &name); + } + thread::sleep(Duration::from_millis(300)); + idx += 1; + if idx > 3 { + continue_flag = false; + } + } +} \ No newline at end of file diff --git a/src/web_parser.rs b/src/web_parser.rs index 309f80d..8a8e9c6 100644 --- a/src/web_parser.rs +++ b/src/web_parser.rs @@ -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 -} \ No newline at end of file + 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; + } +}