Support multiple feeds

This commit is contained in:
clerie 2024-11-29 21:38:30 +01:00
parent 62da50567e
commit bce81ef67b
3 changed files with 15 additions and 5 deletions

1
Cargo.lock generated
View File

@ -837,6 +837,7 @@ name = "rainbowrss"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono",
"feed-rs", "feed-rs",
"reqwest", "reqwest",
] ]

View File

@ -5,5 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.93" anyhow = "1.0.93"
chrono = "0.4.38"
feed-rs = "2.2.0" feed-rs = "2.2.0"
reqwest = { version = "0.12.9", features = ["blocking"] } reqwest = { version = "0.12.9", features = ["blocking"] }

View File

@ -7,13 +7,21 @@ use feed_rs::{
model::Feed, model::Feed,
}; };
fn fetch_feed() -> Result<Feed> { use reqwest::IntoUrl;
let content = reqwest::blocking::get("https://www.youtube.com/feeds/videos.xml?channel_id=UC0intLFzLaudFG-xAvUEO-A")?.bytes()?;
fn fetch_feed(url: impl IntoUrl) -> Result<Feed> {
let content = reqwest::blocking::get(url)?.bytes()?;
Ok(parse(&content[..])?) Ok(parse(&content[..])?)
} }
fn main() -> Result<()> { fn main() -> Result<()> {
let feed = fetch_feed()?;
let mut feed_items = Vec::new();
feed_items.extend(fetch_feed("https://www.youtube.com/feeds/videos.xml?channel_id=UC0intLFzLaudFG-xAvUEO-A")?.entries);
feed_items.extend(fetch_feed("https://feed.zugfunk-podcast.de/")?.entries);
feed_items.sort_by(|a, b| a.published.unwrap_or(chrono::DateTime::UNIX_EPOCH).cmp(&b.published.unwrap_or(chrono::DateTime::UNIX_EPOCH)).reverse());
let mut out = String::new(); let mut out = String::new();
@ -25,8 +33,8 @@ fn main() -> Result<()> {
out.push_str("<ul>\n"); out.push_str("<ul>\n");
for item in feed.entries { 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)); 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("</ul>\n"); out.push_str("</ul>\n");