mod channel; mod config; mod retrieve; use std::{fs::File, future, io::{BufRead, BufReader}, path::{Path, PathBuf}}; use anyhow::anyhow; use clap::Parser; use futures::{StreamExt, executor::block_on}; use rss::{Channel, Item, Source}; use crate::{channel::make_new_empty_channel, config::Config, retrieve::retrieve}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { /// Path to the config file. If blank, searches default config locations. #[arg(short = 'c', long = "config", default_value = "")] config_file: String, } fn main() { let args = Args::parse(); let (urls, config) = load_config(&args.config_file).unwrap(); let channel = generate_channel(urls, config); let _ = channel.pretty_write_to(std::io::stdout(), b' ', 4).unwrap(); } fn load_config(config_file_arg: &str) -> anyhow::Result<(Vec, Config)> { let (config, config_path) = match config_file_arg { "" => Config::find_config().ok_or_else(|| anyhow!("Failed to find config. Paths searched: {:#?}", config::default_config_file_locations())), path => Config::parse_config_file_at_path(path).map(|config| (config, String::from(path))).map_err(|err| anyhow!("Failed to parse config at {}. Error: {}", path, err)), }?; let subscriptions_path: PathBuf = config.subscriptions_path.as_ref() .map(|p| Path::new(&p).to_path_buf()) .unwrap_or_else( || Path::new(&config_path) .parent() .unwrap_or_else(|| Path::new("/")) .join("subscriptions.txt")); let urls: Vec = if subscriptions_path.is_file() { let subscriptions_file = File::open(subscriptions_path)?; BufReader::new(subscriptions_file).lines() .filter(|line| line.is_err() || line.as_ref().is_ok_and(|line| line.trim().len() > 0 && !line.trim().starts_with("#"))) .collect::, std::io::Error>>()? } else { Vec::new() }; Ok((urls, config)) } fn generate_channel(urls: Vec, config: Config) -> Channel { let mut channel = make_new_empty_channel(config); block_on(async { retrieve(urls.iter().map(AsRef::as_ref)).await.for_each(|c| { for (prefix, uri) in c.namespace { let existing_namespace = channel.namespace.iter().find(|(p, u)| p == prefix); if let Some(existing_namespace) = existing_namespace { if existing_namespace.1.trim().len() == 0 { existing_namespace.1 = uri; } else if uri.len() > 0 { eprintln!( "Warning: channel {0} has namespace xmlns:{1}=\"{2}\", but the aggregated feed already has xmlns:{1}=\"{3}\". Ignoring.", c.link, prefix, existing_namespace.1, uri); } else { eprintln!( "Appending namespace xmlns:{0}=\"{1}\" from channel {2}.", prefix, uri, c.link); channel.namespace.insert(prefix, uri); } } channel.items.append(&mut items_with_source(c)); future::ready(()) }).await; }); channel } fn items_with_source(c: Channel) -> Vec { let source = Source { url: c.link.clone(), title: Some(c.title.clone()), }; c.into_items().into_iter() .map(|mut item| { item.source = item.source.or_else(|| Some(source.clone())); item }).collect() }