From 5a69f81807b09257d5f728342497f03b7a9e3e8f Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 12 Feb 2025 18:12:29 +0200 Subject: [PATCH] update `index.gmi` renew method (now based on all items collected) --- src/{path.rs => destination.rs} | 18 +++------ src/main.rs | 69 +++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 37 deletions(-) rename src/{path.rs => destination.rs} (68%) diff --git a/src/path.rs b/src/destination.rs similarity index 68% rename from src/path.rs rename to src/destination.rs index d41937e..e073bc6 100644 --- a/src/path.rs +++ b/src/destination.rs @@ -1,17 +1,15 @@ -use chrono::{DateTime, FixedOffset}; use std::error::Error; use std::path::MAIN_SEPARATOR; -pub struct Path { +pub struct Destination { pub item: String, pub path: String, - pub time: DateTime, } -impl Path { +impl Destination { // Constructors - pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result> { + pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result> { use chrono::{DateTime, Datelike, Timelike}; let time = DateTime::parse_from_rfc2822(pub_date)?; @@ -24,7 +22,7 @@ impl Path { ); let path = format!( - "{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}", + "{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}", time.year(), time.month(), time.day() @@ -34,16 +32,12 @@ impl Path { std::fs::create_dir_all(&path)?; } - Ok(Path { item, path, time }) + Ok(Self { item, path }) } // Getters - pub fn index(&self) -> String { - format!("{}{MAIN_SEPARATOR}index.gmi", self.path) - } - pub fn item(&self) -> String { - format!("{}{MAIN_SEPARATOR}{}", self.path, self.item) + format!("{}{}", self.path, self.item) } } diff --git a/src/main.rs b/src/main.rs index 7c68571..9478aff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod argument; +mod destination; mod output; -mod path; use output::Output; use std::error::Error; @@ -22,21 +22,23 @@ fn main() -> Result<(), Box> { } fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box> { - use path::Path; + use destination::Destination; use reqwest::blocking::get; use rss::Channel; use std::{ - fs::{metadata, File, OpenOptions}, - io::Write, + collections::HashSet, + fs::{metadata, read_dir, File}, + io::{Read, Write}, }; use url::Url; - output.debug("update begin"); + output.debug("feed update begin"); let mut total = 0; let mut exist = 0; + let mut index = HashSet::new(); - // handle feed items + // collect feed items for item in Channel::read_from(&get(source)?.bytes()?[..])? .items() .iter() @@ -45,16 +47,16 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box { - let path = Path::build(target, pub_date, true)?; - if metadata(path.item()).is_ok() { + let destination = Destination::build(target, pub_date, true)?; + if metadata(destination.item()).is_ok() { exist += 1; continue; } data.push(format!("# {pub_date}")); - path + destination } None => { output.warning("item skipped as `pub_date` required by application"); @@ -62,24 +64,11 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box index, - Err(_) => { - let mut index = File::create_new(path.index())?; - index.write_all(format!("# {}\n", path.time.to_rfc2822()).as_bytes())?; - index - } - }; - - index.write_all(format!("\n=> {} {}\n", path.item, path.time).as_bytes())?; - if let Some(description) = item.description() { - index.write_all(format!("\n{description}\n").as_bytes())?; data.push(description.to_string()); } if let Some(content) = item.content() { - index.write_all(format!("\n{content}\n").as_bytes())?; data.push(content.to_string()); } @@ -99,11 +88,41 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box