Commit Diff


commit - 0468e48dcfca8923ccf77f08584567b4f3730553
commit + 7ff982331e416f9f29de900c1a5bfcd561b92fed
blob - 2c2336a684b75dc631aafa03a7ed6bdfac663997
blob + ab34aff9e612ca78b39aab7c6084f2b7b1e2778e
--- Cargo.lock
+++ Cargo.lock
@@ -1138,7 +1138,7 @@ dependencies = [
 
 [[package]]
 name = "rss-email"
-version = "0.1.0"
+version = "0.2.0"
 dependencies = [
  "anyhow",
  "chrono",
blob - ad8c2c8efada1900e403700941516b271040e5b7
blob + d217fc506c8db494b1eae9f74b48c4fc1969dd1e
--- src/db.rs
+++ src/db.rs
@@ -5,9 +5,11 @@ use sqlx::Sqlite;
 
 // inserts a new post or updates an old one with the same guid
 pub async fn insert_item(mut conn: PoolConnection<Sqlite>, item: &Item) -> anyhow::Result<()> {
-    let time = item
-        .pub_date()
-        .map(|date| DateTime::parse_from_rfc2822(date).unwrap().timestamp());
+    let time = item.pub_date().map(|date| {
+        DateTime::parse_from_rfc2822(date)
+            .unwrap_or_else(|_| DateTime::default())
+            .timestamp()
+    });
 
     let guid = item.guid().ok_or_else(|| anyhow!("No guid found"))?.value();
     let title = item.title();
@@ -17,7 +19,7 @@ pub async fn insert_item(mut conn: PoolConnection<Sqli
     let pub_date = time;
     let content = item.content().or_else(|| item.description());
 
-    sqlx::query!("insert into posts (guid, title, author, url, feedurl, pub_date, content) values (?, ?, ?, ?, ?, ?, ?)", guid, title, author, url, feedurl, pub_date, content).execute(&mut conn).await?;
+    sqlx::query!("insert or ignore into posts (guid, title, author, url, feedurl, pub_date, content) values (?, ?, ?, ?, ?, ?, ?)", guid, title, author, url, feedurl, pub_date, content).execute(&mut conn).await?;
 
     Ok(())
 }
blob - 1836148a4ca1c6ab66b6919a2a5690618556d1e0
blob + 688f1b9c81624ce7802c714110d98fb0dc42255f
--- src/main.rs
+++ src/main.rs
@@ -60,7 +60,11 @@ async fn main() -> anyhow::Result<()> {
 
         for i in items {
             let conn = pool.acquire().await?;
-            db::insert_item(conn, i).await?;
+            db::insert_item(conn, i).await.context(format!(
+                "Unable to insert item from {:?} with GUID {:?}",
+                i.link(),
+                i.guid()
+            ))?;
         }
     }
 
blob - af377d139f71bc562e75be4281aecfad59e9702f
blob + 3141cb80595ac1326c51b00c0d95e9814ada7654
--- src/rss.rs
+++ src/rss.rs
@@ -1,11 +1,14 @@
 use reqwest;
 use rss;
 
+use crate::anyhow::Context;
+
 pub async fn fetch_new<S: AsRef<str>>(url: S) -> anyhow::Result<rss::Channel> {
     debug!("Fetching feed for {}", url.as_ref());
 
     let content = reqwest::get(url.as_ref()).await?.bytes().await?;
-    let channel = rss::Channel::read_from(&content[..])?;
+    let channel = rss::Channel::read_from(&content[..])
+        .context(format!("Unable to read from RSS feed {}", url.as_ref()))?;
 
     Ok(channel)
 }