diff --git a/README.md b/README.md index 2560554..deda772 100644 --- a/README.md +++ b/README.md @@ -28,6 +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): + * `d` - debug + * `n` - disable ### Autostart diff --git a/src/argument.rs b/src/argument.rs index 8befa45..9c5877f 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -11,7 +11,11 @@ pub struct Argument { #[arg(short, long, default_value_t = String::from("public"))] pub target: String, - /// Update timeout in seconds (60 by default) + /// Update timeout in seconds (`60` by default) #[arg(short, long, default_value_t = 60)] pub update: u64, + + /// Show output (`d` by default) + #[arg(short, long, default_value_t = String::from("d"))] + pub output: String, } diff --git a/src/main.rs b/src/main.rs index 85231d5..881e494 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod argument; +mod output; +use output::Output; use std::error::Error; fn main() -> Result<(), Box> { @@ -8,16 +10,17 @@ fn main() -> Result<(), Box> { use std::{thread::sleep, time::Duration}; let argument = Argument::parse(); + let output = Output::build(&argument.output); - let mut index: Vec = Vec::new(); + output.debug("crawler started"); loop { - crawl(&mut index, &argument.source, &argument.target)?; + crawl(&argument.source, &argument.target, &output)?; sleep(Duration::from_secs(argument.update)); } } -fn crawl(index: &mut Vec, source: &str, target: &str) -> Result<(), Box> { +fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box> { use reqwest::blocking::get; use rss::Channel; use std::{ @@ -25,13 +28,18 @@ fn crawl(index: &mut Vec, source: &str, target: &str) -> Result<(), Box, source: &str, target: &str) -> Result<(), Box Result> { +fn path(base: &str, pub_date: &str, mkdir: bool) -> Result> { use chrono::{DateTime, Datelike, Timelike}; use std::{fs::create_dir_all, path::MAIN_SEPARATOR}; diff --git a/src/output.rs b/src/output.rs new file mode 100644 index 0000000..fd18102 --- /dev/null +++ b/src/output.rs @@ -0,0 +1,29 @@ +use chrono::{DateTime, Utc}; +use std::time::SystemTime; + +struct Level { + debug: bool, +} + +pub struct Output(Level); + +impl Output { + pub fn build(level: &str) -> Self { + Self(Level { + debug: level.contains("d"), + // @TODO other flags + }) + } + + pub fn debug(&self, message: &str) { + if self.0.debug { + eprintln!("[debug] [{}] {message}", time()); + } + } +} + +fn time() -> String { + let system_time = SystemTime::now(); + let datetime: DateTime = system_time.into(); + datetime.to_rfc3339() +}