article_extractor/
article.rs

1use chrono::{DateTime, Utc};
2use std::fs::File;
3use std::io::{Error, ErrorKind, Write};
4use std::path::PathBuf;
5use url::Url;
6
7pub struct Article {
8    pub title: Option<String>,
9    pub author: Option<String>,
10    pub url: Url,
11    pub date: Option<DateTime<Utc>>,
12    pub thumbnail_url: Option<String>,
13    pub html: Option<String>,
14}
15
16impl Article {
17    pub fn save_html(&self, path: &PathBuf) -> Result<(), Error> {
18        if let Some(html) = self.html.as_deref() {
19            if let Ok(()) = std::fs::create_dir_all(path) {
20                let mut file_name = match self.title.clone() {
21                    Some(file_name) => file_name.replace('/', "_"),
22                    None => "Unknown Title".to_owned(),
23                };
24                file_name.push_str(".html");
25                let path = path.join(file_name);
26                let mut html_file = File::create(path)?;
27                html_file.write_all(html.as_bytes())?;
28                return Ok(());
29            }
30        }
31
32        Err(Error::new(
33            ErrorKind::NotFound,
34            "Article does not contain HTML",
35        ))
36    }
37}