handle optional tags, add warning log level

This commit is contained in:
yggverse 2025-02-11 21:07:45 +02:00
parent 2ef0c8e7ff
commit 3fefd038f1
4 changed files with 53 additions and 19 deletions

View file

@ -28,8 +28,9 @@ pulsarss --source https://path/to/feed.rss
* `source`, `s` - RSS feed source (required) * `source`, `s` - RSS feed source (required)
* `target`, `t` - Destination directory (`public` by default) * `target`, `t` - Destination directory (`public` by default)
* `update`, `u` - Update timeout in seconds (`60` 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 * `d` - debug
* `w` - warning
* `n` - disable * `n` - disable
### Autostart ### Autostart

View file

@ -15,7 +15,7 @@ pub struct Argument {
#[arg(short, long, default_value_t = 60)] #[arg(short, long, default_value_t = 60)]
pub update: u64, pub update: u64,
/// Show output (`d` by default) /// Show output (`dw` by default)
#[arg(short, long, default_value_t = String::from("d"))] #[arg(short, long, default_value_t = String::from("dw"))]
pub output: String, pub output: String,
} }

View file

@ -32,27 +32,53 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box<dyn Erro
let mut total = 0; let mut total = 0;
let mut exist = 0; let mut exist = 0;
for item in Channel::read_from(&get(source)?.bytes()?[..])?.items() { for item in Channel::read_from(&get(source)?.bytes()?[..])?
.items()
.iter()
{
total += 1; total += 1;
let path = &path(target, item.pub_date().unwrap(), true)?; // handle item data
let mut data = Vec::new();
if metadata(path).is_ok() { let path = match item.pub_date() {
exist += 1; Some(pub_data) => {
continue; 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( /* @TODO local storage
format!( if let Some(enclosure) = item.enclosure() {
"# {}\n\n{}\n\n=> {}", match enclosure.mime_type.as_str() {
item.pub_date().unwrap(), "image/jpeg" => todo!(),
item.description().unwrap(), _ => todo!(),
item.link().unwrap() }
) } */
.as_bytes(),
)?; 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!( output.debug(&format!(

View file

@ -3,6 +3,7 @@ use std::time::SystemTime;
struct Level { struct Level {
debug: bool, debug: bool,
warning: bool,
} }
pub struct Output(Level); pub struct Output(Level);
@ -11,7 +12,7 @@ impl Output {
pub fn build(level: &str) -> Self { pub fn build(level: &str) -> Self {
Self(Level { Self(Level {
debug: level.contains("d"), debug: level.contains("d"),
// @TODO other flags warning: level.contains("w"),
}) })
} }
@ -20,6 +21,12 @@ impl Output {
eprintln!("[debug] [{}] {message}", time()); eprintln!("[debug] [{}] {message}", time());
} }
} }
pub fn warning(&self, message: &str) {
if self.0.warning {
eprintln!("[warning] [{}] {message}", time());
}
}
} }
fn time() -> String { fn time() -> String {