mirror of
https://gitlab.com/news-flash/article_scraper.git
synced 2025-07-08 08:30:00 +02:00
initial commit
This commit is contained in:
commit
4b2e6a24eb
17 changed files with 2423 additions and 0 deletions
39
src/article.rs
Normal file
39
src/article.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use std;
|
||||
use url::Url;
|
||||
use std::path::PathBuf;
|
||||
use chrono::NaiveDateTime;
|
||||
use error::{
|
||||
ScraperError,
|
||||
ScraperErrorKind,
|
||||
};
|
||||
use std::io::Write;
|
||||
use failure::ResultExt;
|
||||
|
||||
pub struct Article {
|
||||
pub title: Option<String>,
|
||||
pub author: Option<String>,
|
||||
pub url: Url,
|
||||
pub date: Option<NaiveDateTime>,
|
||||
pub html: Option<String>,
|
||||
}
|
||||
|
||||
impl Article {
|
||||
pub fn save_html(&self, path: &PathBuf) -> Result<(), ScraperError> {
|
||||
|
||||
if let Some(ref html) = self.html {
|
||||
if let Ok(()) = std::fs::create_dir_all(&path) {
|
||||
let mut file_name = match self.title.clone() {
|
||||
Some(file_name) => file_name,
|
||||
None => "Unknown Title".to_owned(),
|
||||
};
|
||||
file_name.push_str(".html");
|
||||
let path = path.join(file_name);
|
||||
let mut html_file = std::fs::File::create(&path).context(ScraperErrorKind::IO)?;
|
||||
html_file.write_all(html.as_bytes()).context(ScraperErrorKind::IO)?;
|
||||
}
|
||||
}
|
||||
|
||||
Err(ScraperErrorKind::Unknown)?
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue