diff --git a/src/main.rs b/src/main.rs index 2361b40..df1744e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,11 @@ use itertools::Itertools; use llm_readability::extractor; use reqwest::{Client, Url}; use rss::Channel; -use tokio::task::JoinSet; +use tokio::{task::JoinSet, time::sleep}; use warp::Filter; +const REQUEST_DELAY: Duration = Duration::from_secs(1); + async fn get_feed(url: String, client: &Client) -> Result { let url = urlencoding::decode(&url)?.into_owned(); let content = client.get(url).send().await?.bytes().await?; @@ -21,7 +23,7 @@ fn get_domain(item: &rss::Item) -> Option { .and_then(|parsed| parsed.domain().map(|domain| domain.to_string())) } -async fn complete(channel: Channel, client: &Client) -> Result> { +async fn complete(mut channel: Channel, client: &Client) -> Result> { let grouped: Vec> = channel .items() .iter() @@ -34,15 +36,16 @@ async fn complete(channel: Channel, client: &Client) -> Result> { for mut items in grouped.into_iter() { let client = client.clone(); set.spawn(async move { - let mut wait_time = Duration::from_secs(0); - for item in &mut items { - tokio::time::sleep(wait_time).await; + for (index, item) in &mut items.iter_mut().enumerate() { + if index > 0 { + sleep(REQUEST_DELAY).await; + } + if let Some(ref link) = item.link && let Ok(content) = get_content(link, &client.clone()).await { item.set_description(content); } - wait_time = Duration::from_secs(1); } items }); @@ -50,9 +53,8 @@ async fn complete(channel: Channel, client: &Client) -> Result> { let items: Vec = set.join_all().await.concat(); - let mut new_channel = channel.clone(); - new_channel.set_items(items); - Ok(Box::new(new_channel)) + channel.set_items(items); + Ok(Box::new(channel)) } async fn get_content(link: &str, client: &Client) -> Result {