diff --git a/README.md b/README.md index deda772..3d2ccd9 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,9 @@ pulsarss --source https://path/to/feed.rss * `source`, `s` - RSS feed source (required) * `target`, `t` - Destination directory (`public` by default) * `update`, `u` - Update timeout in seconds (`60` by default) -* `output`, `o` - Print output (`d` by default): +* `output`, `o` - Print output (`dw` by default): * `d` - debug + * `w` - warning * `n` - disable ### Autostart diff --git a/src/argument.rs b/src/argument.rs index 9c5877f..a123e41 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -15,7 +15,7 @@ pub struct Argument { #[arg(short, long, default_value_t = 60)] pub update: u64, - /// Show output (`d` by default) - #[arg(short, long, default_value_t = String::from("d"))] + /// Show output (`dw` by default) + #[arg(short, long, default_value_t = String::from("dw"))] pub output: String, } diff --git a/src/main.rs b/src/main.rs index 881e494..e230598 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,27 +32,53 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box { + let path = path(target, pub_data, true)?; + if metadata(&path).is_ok() { + exist += 1; + continue; // skip existing records + } + data.push(format!("# {pub_data}")); + path + } + None => { + output.warning("item skipped as `pub_date` is required by application"); + continue; + } + }; + + if let Some(description) = item.description() { + data.push(description.to_string()); } - let mut file = File::create(path)?; + if let Some(content) = item.content() { + data.push(content.to_string()); + } - file.write_all( - format!( - "# {}\n\n{}\n\n=> {}", - item.pub_date().unwrap(), - item.description().unwrap(), - item.link().unwrap() - ) - .as_bytes(), - )?; + /* @TODO local storage + if let Some(enclosure) = item.enclosure() { + match enclosure.mime_type.as_str() { + "image/jpeg" => todo!(), + _ => todo!(), + } + } */ + + if let Some(link) = item.link() { + data.push(format!("=> {link}")); + } + + // record item to static file + File::create(&path)?.write_all(data.join("\n\n").as_bytes())?; } output.debug(&format!( diff --git a/src/output.rs b/src/output.rs index fd18102..4ab6ac6 100644 --- a/src/output.rs +++ b/src/output.rs @@ -3,6 +3,7 @@ use std::time::SystemTime; struct Level { debug: bool, + warning: bool, } pub struct Output(Level); @@ -11,7 +12,7 @@ impl Output { pub fn build(level: &str) -> Self { Self(Level { debug: level.contains("d"), - // @TODO other flags + warning: level.contains("w"), }) } @@ -20,6 +21,12 @@ impl Output { eprintln!("[debug] [{}] {message}", time()); } } + + pub fn warning(&self, message: &str) { + if self.0.warning { + eprintln!("[warning] [{}] {message}", time()); + } + } } fn time() -> String {