generate a blogroll

This commit is contained in:
Thomas Koch 2025-01-12 11:23:31 +02:00
parent 477a94a374
commit d777694118
3 changed files with 30 additions and 10 deletions

View File

@ -13,6 +13,8 @@ use ureq::http::Response;
use ureq::Body;
use url::Url;
const ENTRIES_LEN: usize = 10;
#[derive(Deserialize, Serialize, Default)]
pub struct FetchData {
pub etag: String,
@ -132,8 +134,9 @@ impl FeedStore {
Ok(Some(parser.parse(BufReader::new(file))?))
}
pub fn collect(&self, feed_configs: &Vec<super::FeedConfig>) -> Vec<Entry> {
let mut entries = vec![];
pub fn collect(&self, feed_configs: &Vec<super::FeedConfig>) -> (Vec<Feed>, Vec<Entry>) {
let mut feeds = Vec::new();
let mut entries = Vec::new();
for feed_config in feed_configs {
let mut feed = match (|| {
@ -151,16 +154,20 @@ impl FeedStore {
Ok(Some(f)) => f,
};
entries.append(&mut feed.entries);
// todo also trim mid-way when length > something, trading cpu for memory
entries.append(&mut std::mem::take(&mut feed.entries));
feeds.push(feed);
// optimization to reduce memory usage
if entries.len() > 4 * ENTRIES_LEN {
entries = trim_entries(entries);
}
}
trim_entries(entries)
(feeds, trim_entries(entries))
}
}
fn trim_entries(mut entries: Vec<Entry>) -> Vec<Entry> {
entries.sort_by_key(|e| std::cmp::Reverse(e.updated.or(e.published).unwrap_or_default()));
entries.truncate(10);
entries.truncate(ENTRIES_LEN);
entries
}

View File

@ -2,7 +2,6 @@ use crate::feed_store::FeedStore;
use crate::to_checked_pathbuf;
use crate::Config;
use anyhow::Result;
use feed_rs::model::Entry;
use std::fs::File;
use tera::Tera;
@ -11,8 +10,9 @@ pub fn build(config: &Config, feed_store: &FeedStore) -> Result<()> {
let out_dir = to_checked_pathbuf(&config.out_dir);
let mut context = tera::Context::new();
let feed_entries: Vec<Entry> = feed_store.collect(&config.feeds);
context.insert("entries", &feed_entries);
let (feeds, entries) = feed_store.collect(&config.feeds);
context.insert("feeds", &feeds);
context.insert("entries", &entries);
context.insert("PKG_AUTHORS", env!("CARGO_PKG_AUTHORS"));
context.insert("PKG_HOMEPAGE", env!("CARGO_PKG_HOMEPAGE"));
context.insert("PKG_NAME", env!("CARGO_PKG_NAME"));

View File

@ -63,7 +63,20 @@
<aside>
<img src="logo.svg">
<p>Last updated: {{now()|date(format="%Y-%m-%d %H:%M")}}
<p>Last updated: {{now()|date(format="%Y-%m-%d %H:%M")}}</p>
<ul>
{% for feed in feeds %}
<li>
<a {% if feed.links.0 %}href="{{feed.links.0.href}}"{% endif -%}>
{% if feed.title -%}
{{feed.title.content|striptags}}
{% elif feed.authors.0 and feed.authors.0.name %}
{{ feed.authors.0.name }}
{% endif -%}
</a>
</li>
{% endfor %}
</ul>
</aside>
</div>
</body>