commit f931592c6893feb74ada3d43ecee5d4a2f3353d3 from: witcher date: Wed Dec 28 14:33:38 2022 UTC Add `--no-fetch` to skip fetching feeds Implements: https://todo.sr.ht/~witcher/rss-email/20 commit - 38d37de2f174a9f5f7bc79467d34f4fbe7a91e8d commit + f931592c6893feb74ada3d43ecee5d4a2f3353d3 blob - 6834e633d3ce472cb4e6a18b2bd75de2e6a9b34c blob + f5db540236093c106db82cc03096ed893dbd7b4d --- docs/rss-email.1.scd +++ docs/rss-email.1.scd @@ -33,6 +33,9 @@ The following options are recognized by *rss-email*: *--dry-run* Don't send any emails, just fetch new feed items and mark them as read +*--no-fetch* + Don't fetch any new feed items, just send emails of unsent posts + *--urls* _path_ Specifies a custom urls file to be used instead of the default one at *$XDG_CONFIG_HOME/rss-email/urls* blob - f7ea58260efe31bcbef82617a7183340f25972da blob + 15c82241556da71af597256b977c64a5895bb81d --- src/cli.rs +++ src/cli.rs @@ -17,6 +17,9 @@ pub struct Cli { /// Don't send emails #[clap(long, value_parser)] pub dry_run: bool, + /// Don't fetch feeds + #[clap(long, value_parser)] + pub no_fetch: bool, /// Be verbose. Pass multiple times to increase verbosity. #[clap(short, action = clap::ArgAction::Count)] pub verbose: u8, blob - 1563451cab920c6f5c7f26af86242855400b1b4a blob + d46d7749a93da15b759f443de302779d14f7635e --- src/config.rs +++ src/config.rs @@ -45,6 +45,7 @@ pub struct AppConfig { pub database_path: String, pub urls_path: String, pub dry_run: bool, + pub no_fetch: bool, pub mail_from: String, pub mail_to: String, pub smtp_user: String, @@ -60,6 +61,7 @@ impl AppConfig { database_path: cli.database_path, urls_path: cli.urls_path, dry_run: cli.dry_run, + no_fetch: cli.no_fetch, ..Default::default() }; blob - 6921a225e360fb04b65ee9b0143ffe15773917af blob + 94fa3d15a1396682a4eee9b2de0172d4d5315d3e --- src/main.rs +++ src/main.rs @@ -28,13 +28,14 @@ use tokio::task::JoinSet; #[tokio::main] async fn main() -> anyhow::Result<()> { let config = Arc::new(AppConfig::new()?); - let urls = BufReader::new( + let urls: Vec = BufReader::new( File::open(config.urls_path.as_str()) .context(format!("File {:?} does not exist", &config.urls_path))?, ) .lines() .map(|l| l.unwrap()) - .filter(|l| !l.starts_with('#')); + .filter(|l| !l.starts_with('#')) + .collect(); let db_path = &config.database_path; debug!("Establishing connection to database at {:?}", db_path); @@ -45,23 +46,12 @@ async fn main() -> anyhow::Result<()> { sqlx::migrate!("./migrations").run(&pool).await?; - let mut set = JoinSet::new(); - for u in urls { - set.spawn(async move { feed::fetch_new(u).await }); + if !config.no_fetch { + fetch_feeds(urls, &pool).await?; + } else { + info!("Not fetching any feeds as \"--no-fetch\" has been passed"); } - while let Some(new) = set.join_next().await { - let posts = new??; - - for i in posts.into_iter() { - let conn = pool.acquire().await?; - db::insert_item(conn, &i).await.context(format!( - "Unable to insert item from {:?} with GUID {:?}", - i.url, i.guid - ))?; - } - } - let results = sqlx::query_as!( models::Post, "select * from posts where sent != true order by pub_date desc" @@ -105,6 +95,27 @@ async fn main() -> anyhow::Result<()> { Ok(()) } +async fn fetch_feeds(urls: Vec, pool: &sqlx::Pool) -> anyhow::Result<()> { + let mut set = JoinSet::new(); + for u in urls { + set.spawn(async move { feed::fetch_new(u).await }); + } + + while let Some(new) = set.join_next().await { + let posts = new??; + + for i in posts.into_iter() { + let conn = pool.acquire().await?; + db::insert_item(conn, &i).await.context(format!( + "Unable to insert item from {:?} with GUID {:?}", + i.url, i.guid + ))?; + } + } + + Ok(()) +} + async fn send_post<'a, E>( conn: E, mailer: AsyncSmtpTransport, @@ -120,6 +131,8 @@ where Mail::new(post.title, post.content, post.url) .send_email(from, to, &mailer) .await?; + } else { + info!("Not sending any emails as \"--dry-run\" has been passed"); } sqlx::query!("update posts set sent = true where guid = ?", post.guid)