refactor: use reqwest client

This commit is contained in:
Moritz Böhme 2025-09-04 08:02:02 +02:00
parent f87d74f780
commit fd646281b7
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9

View file

@ -1,29 +1,32 @@
use anyhow::Result;
use llm_readability::extractor;
use reqwest::Url;
use reqwest::{Client, Url};
use rss::Channel;
use tokio::task::JoinSet;
use warp::Filter;
async fn get_feed(url: String) -> Result<Channel> {
async fn get_feed(url: String, client: &Client) -> Result<Channel> {
let url = urlencoding::decode(&url)?.into_owned();
let content = reqwest::get(url).await?.bytes().await?;
let content = client.get(url).send().await?.bytes().await?;
let channel = Channel::read_from(&content[..])?;
Ok(channel)
}
async fn complete(channel: Channel) -> Result<Box<Channel>> {
async fn complete(channel: Channel, client: &Client) -> Result<Box<Channel>> {
let items: Vec<rss::Item> = channel.items().into_iter().cloned().collect();
let mut set = JoinSet::new();
for mut item in items {
set.spawn(async move {
set.spawn({
let client = client.clone();
async move {
if let Some(link) = item.link.clone() {
if let Ok(content) = get_content(link).await {
if let Ok(content) = get_content(link, &client.clone()).await {
item.set_description(content);
};
}
item
}
});
}
@ -34,8 +37,8 @@ async fn complete(channel: Channel) -> Result<Box<Channel>> {
Ok(Box::new(new_channel))
}
async fn get_content(link: String) -> Result<String> {
let response = reqwest::get(&link).await?;
async fn get_content(link: String, client: &Client) -> Result<String> {
let response = client.get(&link).send().await?;
let content = extractor::extract(
&mut response.bytes().await?.as_ref(),
&Url::parse(link.as_str())?,
@ -56,12 +59,16 @@ pub(crate) fn custom_reject(error: impl Into<anyhow::Error>) -> warp::Rejection
#[tokio::main]
async fn main() {
let client = Client::new();
let path = warp::path!(String)
.and_then(|url| async move {
let feed = get_feed(url).await.map_err(custom_reject)?;
let updated = complete(feed).await.map_err(custom_reject)?;
Ok::<String, warp::Rejection>(format!("{}", updated))
.and_then(move |url| {
let client = client.clone();
async move {
let feed = get_feed(url, &client).await.map_err(custom_reject)?;
let updated = complete(feed, &client).await.map_err(custom_reject)?;
Ok::<String, warp::Rejection>(format!("{}", updated))
}
})
.map(|reply| warp::reply::with_header(reply, "Content-Type", "application/rss+xml"));
.map(|reply| warp::reply::with_header(reply, "Content-Type", "application/rss+xml"));
warp::serve(path).run(([127, 0, 0, 1], 3030)).await;
}