initial commit

This commit is contained in:
yggverse 2025-02-11 18:37:02 +02:00
parent 6f41fdc38d
commit 4280f9cb96
6 changed files with 167 additions and 0 deletions

18
src/argument.rs Normal file
View file

@ -0,0 +1,18 @@
use clap::Parser;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Argument {
/// RSS feed source (required)
#[arg(short, long)]
pub source: String,
/// Destination directory (`public` by default)
#[arg(short, long, default_value_t = String::from("public"))]
pub target: String,
/// Update timeout in seconds (60 by default)
#[arg(short, long, default_value_t = 60)]
pub update: u64,
}

80
src/main.rs Normal file
View file

@ -0,0 +1,80 @@
mod argument;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
use argument::Argument;
use clap::Parser;
use std::{thread::sleep, time::Duration};
let argument = Argument::parse();
let mut index: Vec<usize> = Vec::new();
loop {
crawl(&mut index, &argument.source, &argument.target)?;
sleep(Duration::from_secs(argument.update));
}
}
fn crawl(index: &mut Vec<usize>, source: &str, target: &str) -> Result<(), Box<dyn Error>> {
use reqwest::blocking::get;
use rss::Channel;
use std::{
fs::{metadata, File},
io::Write,
};
for item in Channel::read_from(&get(source)?.bytes()?[..])?.items() {
let id = index.len() + 1;
let path = &path(id, target, item.pub_date().unwrap(), true)?;
if metadata(path).is_ok() {
continue; // skip existing records
}
let mut file = File::create(path)?;
file.write_all(
format!(
"# {}\n\n{}\n\n=> {}",
item.pub_date().unwrap(),
item.description().unwrap(),
item.link().unwrap()
)
.as_bytes(),
)?;
index.push(id);
}
Ok(())
}
fn path(_id: usize, base: &str, pub_date: &str, mkdir: bool) -> Result<String, Box<dyn Error>> {
use chrono::{DateTime, Datelike, Timelike};
use std::{fs::create_dir_all, path::MAIN_SEPARATOR};
let date_time = DateTime::parse_from_rfc2822(pub_date)?;
let dir = format!(
"{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
date_time.year(),
date_time.month(),
date_time.day()
);
let file = format!(
"{:02}-{:02}-{:02}.gmi",
date_time.hour(),
date_time.minute(),
date_time.second()
);
if mkdir {
create_dir_all(&dir)?;
}
Ok(format!("{dir}{MAIN_SEPARATOR}{file}"))
}