refactor refresh

This commit is contained in:
2023-11-11 20:35:46 +01:00
parent 461abf1872
commit 6ca15c8ab7
+15 -9
View File
@@ -7,8 +7,9 @@ use anyhow::{anyhow, Context};
use chrono::{DateTime, NaiveDateTime, TimeZone};
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use lazy_regex::regex;
use log::{debug, info};
use log::{debug, error, info};
use reqwest::IntoUrl;
use rss::Item;
use serenity::http::Http;
use serenity::json::Value;
use serenity::model::channel::Embed;
@@ -139,8 +140,6 @@ impl DisbahnClient {
}
pub async fn refresh(&mut self) -> anyhow::Result<()> {
use crate::database::schema::posts::{self, dsl};
debug!("Refreshing RSS feed ...");
let channel = Self::get_rss_channel(&self.rss_url)
.await
@@ -150,12 +149,22 @@ impl DisbahnClient {
let items = channel.items();
for item in items {
if let Err(err) = self.refresh_item(item).await {
error!("Error refreshing item: {err}");
}
}
debug!("Done.");
Ok(())
}
async fn refresh_item(&mut self, item: &Item) -> anyhow::Result<()> {
use crate::database::schema::posts::{self, dsl};
let guid = item.guid().ok_or(anyhow!("Missing GUID"))?.value();
let pub_date_str = item.pub_date().ok_or(anyhow!("Missing publication date"))?;
let pub_datetime = DateTime::parse_from_rfc2822(pub_date_str)
.with_context(|| {
format!("Unable to parse publication date string {pub_date_str:?}")
})?
.with_context(|| format!("Unable to parse publication date string {pub_date_str:?}"))?
.naive_utc()
.and_utc();
@@ -204,9 +213,6 @@ impl DisbahnClient {
.execute(self.database.conn())
.with_context(|| "Error inserting new post into database")?;
}
}
debug!("Done.");
Ok(())
}
}