article_extractor/
article.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use chrono::{DateTime, Utc};
use std::fs::File;
use std::io::{Error, ErrorKind, Write};
use std::path::PathBuf;
use url::Url;

pub struct Article {
    pub title: Option<String>,
    pub author: Option<String>,
    pub url: Url,
    pub date: Option<DateTime<Utc>>,
    pub thumbnail_url: Option<String>,
    pub html: Option<String>,
}

impl Article {
    pub fn save_html(&self, path: &PathBuf) -> Result<(), Error> {
        if let Some(html) = self.html.as_deref() {
            if let Ok(()) = std::fs::create_dir_all(path) {
                let mut file_name = match self.title.clone() {
                    Some(file_name) => file_name.replace('/', "_"),
                    None => "Unknown Title".to_owned(),
                };
                file_name.push_str(".html");
                let path = path.join(file_name);
                let mut html_file = File::create(path)?;
                html_file.write_all(html.as_bytes())?;
                return Ok(());
            }
        }

        Err(Error::new(
            ErrorKind::NotFound,
            "Article does not contain HTML",
        ))
    }
}