refactor: remove clone and simplify delay
This commit is contained in:
parent
22c2387eb2
commit
73a490b170
1 changed files with 11 additions and 9 deletions
20
src/main.rs
20
src/main.rs
|
|
@ -5,9 +5,11 @@ use itertools::Itertools;
|
||||||
use llm_readability::extractor;
|
use llm_readability::extractor;
|
||||||
use reqwest::{Client, Url};
|
use reqwest::{Client, Url};
|
||||||
use rss::Channel;
|
use rss::Channel;
|
||||||
use tokio::task::JoinSet;
|
use tokio::{task::JoinSet, time::sleep};
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
|
|
||||||
|
const REQUEST_DELAY: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
async fn get_feed(url: String, client: &Client) -> Result<Channel> {
|
async fn get_feed(url: String, client: &Client) -> Result<Channel> {
|
||||||
let url = urlencoding::decode(&url)?.into_owned();
|
let url = urlencoding::decode(&url)?.into_owned();
|
||||||
let content = client.get(url).send().await?.bytes().await?;
|
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()))
|
.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
|
let grouped: Vec<Vec<rss::Item>> = channel
|
||||||
.items()
|
.items()
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -34,15 +36,16 @@ async fn complete(channel: Channel, client: &Client) -> Result<Box<Channel>> {
|
||||||
for mut items in grouped.into_iter() {
|
for mut items in grouped.into_iter() {
|
||||||
let client = client.clone();
|
let client = client.clone();
|
||||||
set.spawn(async move {
|
set.spawn(async move {
|
||||||
let mut wait_time = Duration::from_secs(0);
|
for (index, item) in &mut items.iter_mut().enumerate() {
|
||||||
for item in &mut items {
|
if index > 0 {
|
||||||
tokio::time::sleep(wait_time).await;
|
sleep(REQUEST_DELAY).await;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(ref link) = item.link
|
if let Some(ref link) = item.link
|
||||||
&& let Ok(content) = get_content(link, &client.clone()).await
|
&& let Ok(content) = get_content(link, &client.clone()).await
|
||||||
{
|
{
|
||||||
item.set_description(content);
|
item.set_description(content);
|
||||||
}
|
}
|
||||||
wait_time = Duration::from_secs(1);
|
|
||||||
}
|
}
|
||||||
items
|
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 items: Vec<rss::Item> = set.join_all().await.concat();
|
||||||
|
|
||||||
let mut new_channel = channel.clone();
|
channel.set_items(items);
|
||||||
new_channel.set_items(items);
|
Ok(Box::new(channel))
|
||||||
Ok(Box::new(new_channel))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_content(link: &str, client: &Client) -> Result<String> {
|
async fn get_content(link: &str, client: &Client) -> Result<String> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue