From 72229bf0738e9067286084b41e8f7972c2273b24 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 12 Dec 2025 22:50:31 -0800 Subject: [PATCH] start removing the feed entry channel --- src/lib.rs | 68 +++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6b71b0c..f019e45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ pub struct BlogdorTheAggregator { #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct FeedEntry { url: String, + feed_id: i64, title: String, published: DateTime, received: DateTime, @@ -111,49 +112,52 @@ async fn check_feeds(db: &SqlitePool, client: &reqwest::Client) { async fn check_feed( db: SqlitePool, - id: i64, + feed_id: i64, client: reqwest::Client, url: String, tx: tokio::sync::mpsc::UnboundedSender, -) { - if let Ok(rec) = sqlx::query!( +) -> Result, String> { + let rec = sqlx::query!( "select date_time from runs where succeeded = true and feed = ? order by id desc limit 1", - id + feed_id ) .fetch_optional(&db) .await - { - let last_fetched = rec.map(|d| d.date_time.and_utc()).unwrap_or(LAST_FETCHED); - let now = Utc::now(); + .map_err(|e| format!("Could not fetch runs for {url} from DB, got {e}"))?; - let feed = client.get(&url).send().await; - if let Ok(feed) = feed - && let Ok(feed) = feed.bytes().await - && let Ok(feed) = parse(feed.reader()) - { - for post in feed.entries { - let last_year = now - ONE_YEAR; - if post.published.unwrap_or(last_year) > last_fetched { - let entry = FeedEntry { - url: url.clone(), - title: post - .title - .map(|t| t.content) - .unwrap_or("Blogdor Says: NO POST TITLE".to_string()), - published: post.published.unwrap_or(now), - received: now, - feed_description: feed.description.to_owned().map(|d| d.content), - body: post.content.and_then(|c| c.body), - }; - if let Err(e) = tx.send(entry) { - tracing::error!("error sending feed entry: {e}"); - continue; - }; - // update DB - } + let last_fetched = rec.map(|d| d.date_time.and_utc()).unwrap_or(LAST_FETCHED); + let now = Utc::now(); + let mut out = Vec::new(); + let feed = client.get(&url).send().await; + if let Ok(feed) = feed + && let Ok(feed) = feed.bytes().await + && let Ok(feed) = parse(feed.reader()) + { + for post in feed.entries { + let last_year = now - ONE_YEAR; + if post.published.unwrap_or(last_year) > last_fetched { + let entry = FeedEntry { + url: post + .links + .first() + .cloned() + .map(|l| l.href) + .unwrap_or("".to_string()), + feed_id, + title: post + .title + .map(|t| t.content) + .unwrap_or("Blogdor Says: NO POST TITLE".to_string()), + published: post.published.unwrap_or(now), + received: now, + feed_description: feed.description.to_owned().map(|d| d.content), + body: post.content.and_then(|c| c.body), + }; + out.push(entry); } } } + todo!() } async fn get_db_pool() -> SqlitePool {