init
This commit is contained in:
26
src/main.rs
Normal file
26
src/main.rs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,17 @@
|
|||||||
|
use bytes::Bytes;
|
||||||
use scraper::{Html, Selector};
|
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 {
|
pub(crate) fn get_html_title(body: String) -> String {
|
||||||
let document = Html::parse_document(&body);
|
let document = Html::parse_document(&body);
|
||||||
@@ -13,10 +25,48 @@ pub(crate) fn download_image(url_path: &str, file_path: &str) -> bool {
|
|||||||
println!("{}", url);
|
println!("{}", url);
|
||||||
let response = get_response(&url);
|
let response = get_response(&url);
|
||||||
if response.is_empty() {
|
if response.is_empty() {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
let file_path = format!("images/{}", file_path);
|
let file_path = format!("images/{}", file_path);
|
||||||
println!("{}",file_path);
|
|
||||||
file::write_file(&file_path, response).expect("Write file error");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user