init
This commit is contained in:
18
src/file.rs
Normal file
18
src/file.rs
Normal file
@@ -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(())
|
||||
}
|
||||
22
src/web_parser.rs
Normal file
22
src/web_parser.rs
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user