pub mod fetch;
pub mod sanitize;
use crate::Config;
use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SanitizedContent {
pub id: String,
pub url: String,
pub fetched_at: chrono::DateTime<chrono::Utc>,
pub sanitized_text: String,
pub removed_patterns: Vec<String>,
pub log_path: PathBuf,
}
pub async fn fetch_and_sanitize(cfg: &Config, url: &str) -> Result<SanitizedContent> {
let raw = fetch::get(url, cfg.web.timeout_secs).await?;
let (cleaned_text, removed) = sanitize::run(&cfg.llm, url, &raw).await?;
let mut content = SanitizedContent {
id: Uuid::new_v4().to_string(),
url: url.to_string(),
fetched_at: chrono::Utc::now(),
sanitized_text: cleaned_text,
removed_patterns: removed,
log_path: PathBuf::new(),
};
persist_log(&cfg.paths, &mut content)?;
Ok(content)
}
fn persist_log(paths: &crate::Paths, content: &mut SanitizedContent) -> Result<()> {
let dir = paths.web_log();
fs::create_dir_all(&dir)?;
let stamp = content.fetched_at.format("%Y-%m-%d-%H%M%S");
let path = dir.join(format!("{stamp}-{}.json", &content.id[..8]));
content.log_path.clone_from(&path);
let body = serde_json::to_string_pretty(content)?;
fs::write(&path, body)?;
Ok(())
}