refactor: use reqwest client
This commit is contained in:
parent
f87d74f780
commit
fd646281b7
1 changed files with 20 additions and 13 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -1,29 +1,32 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use llm_readability::extractor;
|
use llm_readability::extractor;
|
||||||
use reqwest::Url;
|
use reqwest::{Client, Url};
|
||||||
use rss::Channel;
|
use rss::Channel;
|
||||||
use tokio::task::JoinSet;
|
use tokio::task::JoinSet;
|
||||||
use warp::Filter;
|
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 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[..])?;
|
let channel = Channel::read_from(&content[..])?;
|
||||||
Ok(channel)
|
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 items: Vec<rss::Item> = channel.items().into_iter().cloned().collect();
|
||||||
|
|
||||||
let mut set = JoinSet::new();
|
let mut set = JoinSet::new();
|
||||||
for mut item in items {
|
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 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.set_description(content);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
item
|
item
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,8 +37,8 @@ async fn complete(channel: Channel) -> Result<Box<Channel>> {
|
||||||
Ok(Box::new(new_channel))
|
Ok(Box::new(new_channel))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_content(link: String) -> Result<String> {
|
async fn get_content(link: String, client: &Client) -> Result<String> {
|
||||||
let response = reqwest::get(&link).await?;
|
let response = client.get(&link).send().await?;
|
||||||
let content = extractor::extract(
|
let content = extractor::extract(
|
||||||
&mut response.bytes().await?.as_ref(),
|
&mut response.bytes().await?.as_ref(),
|
||||||
&Url::parse(link.as_str())?,
|
&Url::parse(link.as_str())?,
|
||||||
|
|
@ -56,12 +59,16 @@ pub(crate) fn custom_reject(error: impl Into<anyhow::Error>) -> warp::Rejection
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
let client = Client::new();
|
||||||
let path = warp::path!(String)
|
let path = warp::path!(String)
|
||||||
.and_then(|url| async move {
|
.and_then(move |url| {
|
||||||
let feed = get_feed(url).await.map_err(custom_reject)?;
|
let client = client.clone();
|
||||||
let updated = complete(feed).await.map_err(custom_reject)?;
|
async move {
|
||||||
Ok::<String, warp::Rejection>(format!("{}", updated))
|
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;
|
warp::serve(path).run(([127, 0, 0, 1], 3030)).await;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue