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) => { Some(pub_date) => {
// update `index.gmi` on channel `pub_date` change // update `index.gmi` on channel `pub_date` change
{ {
let remote = chrono::DateTime::parse_from_rfc2822(pub_date)?; let remote_time = 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) if status.is_none() || status.is_some_and(|local_time| local_time != remote_time) {
*status = Some(remote); // 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 // build `index.gmi` members
let (mut file, mut data) = ( let (mut file, mut data) = (File::create(index_path.index())?, Vec::new());
File::create(Path::build(target, pub_date, true)?.index())?,
Vec::new(),
);
// collect `index.gmi` data // collect `index.gmi` data
for item in channel.items().iter() { for item in channel.items().iter() {
match item.pub_date() { match item.pub_date() {
Some(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() { if let Some(description) = item.description() {
data.push(description.to_string()); data.push(description.to_string());
@ -96,7 +103,7 @@ fn crawl(
let path = match item.pub_date() { let path = match item.pub_date() {
Some(pub_date) => { Some(pub_date) => {
let path = Path::build(target, pub_date, true)?; let path = Path::build(target, pub_date, true)?;
if metadata(path.filepath()).is_ok() { if metadata(path.item()).is_ok() {
exist += 1; exist += 1;
continue; // skip existing records continue; // skip existing records
} }
@ -142,7 +149,7 @@ fn crawl(
} }
// record new item file // 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"), 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; use std::path::MAIN_SEPARATOR;
pub struct Path { pub struct Path {
file: String, pub item: String,
path: String, pub path: String,
} }
impl Path { impl Path {
@ -12,27 +12,27 @@ impl Path {
pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result<Path, Box<dyn Error>> { pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result<Path, Box<dyn Error>> {
use chrono::{DateTime, Datelike, Timelike}; 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", "{:02}-{:02}-{:02}.gmi",
date_time.hour(), time.hour(),
date_time.minute(), time.minute(),
date_time.second() time.second()
); );
let path = format!( let path = format!(
"{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}", "{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
date_time.year(), time.year(),
date_time.month(), time.month(),
date_time.day() time.day()
); );
if mkdir { if mkdir {
std::fs::create_dir_all(&path)?; std::fs::create_dir_all(&path)?;
} }
Ok(Path { file, path }) Ok(Path { item, path })
} }
// Getters // Getters
@ -41,11 +41,7 @@ impl Path {
format!("{}{MAIN_SEPARATOR}index.gmi", self.path) format!("{}{MAIN_SEPARATOR}index.gmi", self.path)
} }
pub fn filepath(&self) -> String { pub fn item(&self) -> String {
format!("{}{MAIN_SEPARATOR}{}", self.path, self.file) format!("{}{MAIN_SEPARATOR}{}", self.path, self.item)
}
pub fn filename(&self) -> &str {
&self.file
} }
} }