commit daa46336f170d760aafc3ad80f6972a6518da776 Author: lychang <632945494@qq.com> Date: Fri Jan 12 03:23:51 2024 +0800 init diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..e969958 --- /dev/null +++ b/src/file.rs @@ -0,0 +1,18 @@ +use std::fs; +use std::fs::File; +use std::io::Write; +use std::path::Path; +use bytes::Bytes; +use bytes; + + +pub(crate) fn write_file(path: &str, content: Bytes) -> Result<(), std::io::Error> { + let path = Path::new(path); + let folder_path = path.parent(); + if !folder_path.unwrap().exists() { + fs::create_dir_all(folder_path.unwrap().as_os_str())?; + } + let mut file = File::create(path)?; + file.write_all(&content)?; + Ok(()) +} diff --git a/src/web_parser.rs b/src/web_parser.rs new file mode 100644 index 0000000..309f80d --- /dev/null +++ b/src/web_parser.rs @@ -0,0 +1,22 @@ +use scraper::{Html, Selector}; +use crate::{file, get_response}; + +pub(crate) fn get_html_title(body: String) -> String { + let document = Html::parse_document(&body); + let title_selector = Selector::parse("h1").unwrap(); + let title_element = document.select(&title_selector).next().unwrap(); + return title_element.text().next().unwrap().to_string(); +} + +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_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