mirror of
https://github.com/YGGverse/pulsarss.git
synced 2026-04-01 17:45:33 +00:00
handle optional tags, add warning log level
This commit is contained in:
parent
2ef0c8e7ff
commit
3fefd038f1
4 changed files with 53 additions and 19 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
56
src/main.rs
56
src/main.rs
|
|
@ -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!(
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue