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