Specify input and output file paths

This commit is contained in:
2025-01-05 14:11:46 +01:00
parent 17b85d11b2
commit d86df2a44b
3 changed files with 140 additions and 3 deletions

View File

@@ -2,6 +2,9 @@ use anyhow::{
Context,
Result,
};
use clap::{
Parser,
};
use feed_rs::{
parser::parse,
model::Feed,
@@ -12,6 +15,17 @@ use reqwest::{
Url,
};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Path to file containing feeds
#[arg(long, default_value_t = String::from("feeds.txt"))]
feeds: String,
/// Path to output file
#[arg(long, default_value_t = String::from("index.html"))]
out: String,
}
fn fetch_feed(url: impl IntoUrl) -> Result<Feed> {
let content = reqwest::blocking::get(url)?.bytes()?;
Ok(parse(&content[..])?)
@@ -38,10 +52,11 @@ fn read_feed_file(path: &str) -> Result<Vec<Url>> {
}
fn main() -> Result<()> {
let cli = Cli::parse();
let mut feed_items = Vec::new();
let feeds = read_feed_file("feeds.txt")?;
let feeds = read_feed_file(&cli.feeds)?;
for feed_url in feeds {
feed_items.extend(fetch_feed(feed_url)?.entries);
@@ -60,7 +75,7 @@ fn main() -> Result<()> {
out.push_str("<ul>\n");
for item in feed_items {
out.push_str(&format!("<li><a href=\"{}\">{}</a> ({})</li>\n", item.links.first().context("No link found")?.href, item.title.context("No title found")?.content, item.published.context("")?));
out.push_str(&format!("<li><a href=\"{}\">{}</a> ({})</li>\n", item.links.first().context("No link found")?.href, item.title.context("No title found")?.content, item.published.context("No publishing time")?));
}
out.push_str("</ul>\n");
@@ -68,7 +83,8 @@ fn main() -> Result<()> {
out.push_str("</body>\n");
out.push_str("</html>\n");
std::fs::write("index.html", out)?;
std::fs::write(&cli.out, out)
.context("Failed to write output to file")?;
Ok(())
}