skip not relevant records from index.gmi

This commit is contained in:
yggverse 2025-02-12 01:31:25 +02:00
parent 925ae08bf7
commit aebc850de1
2 changed files with 32 additions and 29 deletions

View file

@ -50,22 +50,29 @@ fn crawl(
Some(pub_date) => {
// update `index.gmi` on channel `pub_date` change
{
let remote = chrono::DateTime::parse_from_rfc2822(pub_date)?;
if status.is_none() || status.is_some_and(|local| local != remote) {
// update global state (to skip `index.gmi` overwrites without changes)
*status = Some(remote);
let remote_time = chrono::DateTime::parse_from_rfc2822(pub_date)?;
if status.is_none() || status.is_some_and(|local_time| local_time != remote_time) {
// update global state to skip `index.gmi` overwrites without changes
*status = Some(remote_time);
let index_path = Path::build(target, pub_date, true)?;
// build `index.gmi` members
let (mut file, mut data) = (
File::create(Path::build(target, pub_date, true)?.index())?,
Vec::new(),
);
let (mut file, mut data) = (File::create(index_path.index())?, Vec::new());
// collect `index.gmi` data
for item in channel.items().iter() {
match item.pub_date() {
Some(pub_date) => {
let path = Path::build(target, pub_date, true)?;
let item_path = Path::build(target, pub_date, true)?;
data.push(format!("=> {} {pub_date}", path.filename()));
// skip not relevant records from `index.gmi`
if item_path.path != index_path.path {
continue;
}
data.push(format!("=> {} {pub_date}", item_path.item));
if let Some(description) = item.description() {
data.push(description.to_string());
@ -96,7 +103,7 @@ fn crawl(
let path = match item.pub_date() {
Some(pub_date) => {
let path = Path::build(target, pub_date, true)?;
if metadata(path.filepath()).is_ok() {
if metadata(path.item()).is_ok() {
exist += 1;
continue; // skip existing records
}
@ -142,7 +149,7 @@ fn crawl(
}
// record new item file
File::create(path.filepath())?.write_all(data.join("\n\n").as_bytes())?;
File::create(path.item())?.write_all(data.join("\n\n").as_bytes())?;
}
}
None => output.warning("channel skipped as `pub_date` required by application"),

View file

@ -2,8 +2,8 @@ use std::error::Error;
use std::path::MAIN_SEPARATOR;
pub struct Path {
file: String,
path: String,
pub item: String,
pub path: String,
}
impl Path {
@ -12,27 +12,27 @@ impl Path {
pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result<Path, Box<dyn Error>> {
use chrono::{DateTime, Datelike, Timelike};
let date_time = DateTime::parse_from_rfc2822(pub_date)?;
let time = DateTime::parse_from_rfc2822(pub_date)?;
let file = format!(
let item = format!(
"{:02}-{:02}-{:02}.gmi",
date_time.hour(),
date_time.minute(),
date_time.second()
time.hour(),
time.minute(),
time.second()
);
let path = format!(
"{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
date_time.year(),
date_time.month(),
date_time.day()
time.year(),
time.month(),
time.day()
);
if mkdir {
std::fs::create_dir_all(&path)?;
}
Ok(Path { file, path })
Ok(Path { item, path })
}
// Getters
@ -41,11 +41,7 @@ impl Path {
format!("{}{MAIN_SEPARATOR}index.gmi", self.path)
}
pub fn filepath(&self) -> String {
format!("{}{MAIN_SEPARATOR}{}", self.path, self.file)
}
pub fn filename(&self) -> &str {
&self.file
pub fn item(&self) -> String {
format!("{}{MAIN_SEPARATOR}{}", self.path, self.item)
}
}