1use psl::{List, Psl};
4use scraper::Selector;
5use std::fs;
6use std::path::Path;
7use url::Url;
8
9use crate::error::SpiderError;
10use crate::request::Request;
11
12pub fn is_same_site(a: &Url, b: &Url) -> bool {
14 a.host_str().and_then(|h| List.domain(h.as_bytes()))
15 == b.host_str().and_then(|h| List.domain(h.as_bytes()))
16}
17
18pub fn normalize_origin(request: &Request) -> String {
20 let url = &request.url;
21 let scheme = url.scheme();
22 let host = url.host_str().unwrap_or("");
23 let port = url.port_or_known_default().unwrap_or(0);
24
25 format!("{scheme}://{host}:{port}")
26}
27
28pub fn validate_output_dir(file_path: impl AsRef<Path>) -> Result<(), SpiderError> {
34 let Some(parent_dir) = file_path.as_ref().parent() else {
35 return Ok(());
36 };
37
38 if !parent_dir.as_os_str().is_empty() && !parent_dir.exists() {
39 fs::create_dir_all(parent_dir)?;
40 }
41
42 Ok(())
43}
44
45pub fn create_dir(dir_path: impl AsRef<Path>) -> Result<(), SpiderError> {
51 fs::create_dir_all(dir_path)?;
52 Ok(())
53}
54
55pub trait ToSelector {
57 fn to_selector(&self) -> Result<Selector, SpiderError>;
63}
64
65impl ToSelector for &str {
66 fn to_selector(&self) -> Result<Selector, SpiderError> {
67 Selector::parse(self).map_err(|e| SpiderError::HtmlParseError(e.to_string()))
68 }
69}
70
71impl ToSelector for String {
72 fn to_selector(&self) -> Result<Selector, SpiderError> {
73 Selector::parse(self).map_err(|e| SpiderError::HtmlParseError(e.to_string()))
74 }
75}