refactor: remove clone and simplify delay

This commit is contained in:
Moritz Böhme 2025-10-03 11:31:48 +02:00
parent 22c2387eb2
commit 73a490b170
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9

View file

@ -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<Channel> {
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<String> {
.and_then(|parsed| parsed.domain().map(|domain| domain.to_string()))
}
async fn complete(channel: Channel, client: &Client) -> Result<Box<Channel>> {
async fn complete(mut channel: Channel, client: &Client) -> Result<Box<Channel>> {
let grouped: Vec<Vec<rss::Item>> = channel
.items()
.iter()
@ -34,15 +36,16 @@ async fn complete(channel: Channel, client: &Client) -> Result<Box<Channel>> {
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<Box<Channel>> {
let items: Vec<rss::Item> = 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<String> {