Skip to content

Commit

Permalink
Some extraction and better print
Browse files Browse the repository at this point in the history
  • Loading branch information
holzhey committed Apr 30, 2024
1 parent d8111af commit e8b1d80
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
use std::error::Error;
use std::{error::Error, fmt::Display};

use rss::Channel;
use scraper::{Html, Selector};

const _RSS_SEVERE: &str = "https://www.spc.noaa.gov/products/spcwwrss.xml";
const RSS_ALL: &str = "https://www.spc.noaa.gov/products/spcrss.xml";

struct Warning {
title: String,
content: String,
}

impl Display for Warning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut c = self.content.clone();
c.truncate(150);
write!(f, "** {} **\n{}\n-------------", self.title, c)
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let channel = get_feed().await?;
println!("Loaded channel: {}", channel.title());
let warnings = get_warnings(get_feed().await?).await?;
for w in warnings {
println!("{}", w);
}
Ok(())
}

async fn get_warnings(channel: Channel) -> Result<Vec<Warning>, Box<dyn Error>> {
let mut warnings = Vec::new();
for item in channel.items() {
let title = item.title().unwrap_or("(no title)");
let desc = item.description().unwrap_or("(nothing)");
let d = Html::parse_fragment(desc);
let sel = Selector::parse("pre").unwrap();
let mut pre = d.select(&sel);
let dom = Html::parse_fragment(desc);
let sel = Selector::parse("pre")?;
let mut pre = dom.select(&sel);

println!("Item title: {}", title);
if let Some(n) = pre.next() {
println!("{}", n.text().next().unwrap());
warnings.push(Warning {
title: title.to_string(),
content: n.text().next().unwrap().to_string(),
})
}
}
Ok(())

Ok(warnings)
}

async fn get_feed() -> Result<Channel, Box<dyn Error>> {
Expand Down

0 comments on commit e8b1d80

Please sign in to comment.