summaryrefslogtreecommitdiff
path: root/src/retrieve.rs
blob: 3b891384a6cf03366be586dd67eef2f1efc54db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use futures::{StreamExt, stream};
use isahc::AsyncReadResponseExt;
use rss::Channel;

pub async fn retrieve<'a, I>(urls: I) -> impl StreamExt<Item = Channel>
    where I : IntoIterator<Item = &'a str>
{
    stream::iter(urls)
        .flat_map(|url| stream::once(retrieve_one_or_warn(url)))
        .filter_map(async |result| result.ok())
}

async fn retrieve_one_or_warn(url: &str) -> anyhow::Result<Channel>
{
    retrieve_one(url).await
        .inspect_err(|err| eprintln!("Failed to fetch {url}: {:#?}", err))
}

async fn retrieve_one(url: &str) -> anyhow::Result<Channel>
{
    let content = isahc::get_async(url).await?.bytes().await?;
    Ok(Channel::read_from(&content[..])?)
}