diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 12 | ||||
| -rw-r--r-- | src/retrieve.rs | 24 |
2 files changed, 34 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index b7d5a4b..ae68477 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ mod config; +mod retrieve; use clap::Parser; +use futures::executor::block_on; -use crate::config::Config; +use crate::{config::Config, retrieve::retrieve}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -14,11 +16,17 @@ struct Args { fn main() { let args = Args::parse(); + let config = match args.config_file.as_str() { "" => Config::find_config().unwrap_or_else(|| panic!("Failed to find config. Paths searched: {:#?}", config::default_config_file_locations())), path => Config::parse_config_file_at_path(&path).unwrap_or_else(|err| panic!("Failed to parse config at {}. Error: {}", path, err)), }; - println!("URLs to fetch: {:#?}", config.urls); + + block_on(async { + for channel in retrieve(config.urls.iter().map(AsRef::as_ref)).await { + println!("{:#?}", channel); + } + }); } 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[..])?) +} |
