diff options
Diffstat (limited to 'src/retrieve.rs')
| -rw-r--r-- | src/retrieve.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/retrieve.rs b/src/retrieve.rs new file mode 100644 index 0000000..ee904ab --- /dev/null +++ b/src/retrieve.rs @@ -0,0 +1,24 @@ +use futures::{StreamExt, stream}; +use isahc::AsyncReadResponseExt; +use rss::Channel; + +pub async fn retrieve<'a, I>(urls: I) -> Vec<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()) + .collect().await +} + +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[..])?) +} |
