Compare commits
4 Commits
cd590b73fe
...
d0ac11ea89
Author | SHA1 | Date | |
---|---|---|---|
d0ac11ea89 | |||
b93f921bcb | |||
148d4e7e43 | |||
50f0bcd3c7 |
@ -56,7 +56,7 @@ pub async fn fetch_and_parse_feed(uri: &str) -> Result<Feed, Box<dyn Error>> {
|
||||
let http_client = reqwest::Client::builder().default_headers(headers).build()?;
|
||||
let response = http_client.get(uri).send().await?.text().await?;
|
||||
|
||||
info!("Got response, parsing feed");
|
||||
info!("Got response, parsing feed from {}", uri);
|
||||
let feed = parser::parse(response.as_bytes())?;
|
||||
|
||||
let feed_title = match feed.title.clone() {
|
||||
@ -82,7 +82,11 @@ pub fn format_entry(feed: Feed, entry: model::Entry) -> Result<Entry, Box<dyn Er
|
||||
Some(t) => t.content,
|
||||
None => String::from("Untitled")
|
||||
},
|
||||
link: entry.links[0].href.clone(),
|
||||
link: if entry.links.len() > 0 {
|
||||
entry.links[0].href.clone()
|
||||
} else {
|
||||
String::from("#")
|
||||
},
|
||||
ts: match entry.updated {
|
||||
Some(d) => d,
|
||||
None => entry.published.unwrap_or(Utc::now())
|
||||
|
24
src/main.rs
24
src/main.rs
@ -39,7 +39,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use std::{ sync::Arc, cmp::max };
|
||||
use tracing::{ info, debug };
|
||||
use tracing::{ info, debug, error };
|
||||
use chrono::DateTime;
|
||||
|
||||
#[tokio::main]
|
||||
@ -51,7 +51,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
// This message passing channel is used for sending messages to the matrix module,
|
||||
// it holds a tuple with an HTML message and a list of rooms to post to
|
||||
let (bcast_tx, bcast_rx) = broadcast::channel(16);
|
||||
let (bcast_tx, bcast_rx) = broadcast::channel(1024);
|
||||
|
||||
let state_db = FeedReaderStateDb::new("state.yaml").await
|
||||
.expect("Failed to initialize feed reader state db");
|
||||
@ -63,8 +63,24 @@ async fn main() -> anyhow::Result<()> {
|
||||
tokio::spawn(async move {
|
||||
|
||||
loop {
|
||||
let feed = fetch_and_parse_feed(&feed_config.url).await
|
||||
.expect("Failed to parse feed");
|
||||
let feed: Option<_> = match fetch_and_parse_feed(&feed_config.url).await {
|
||||
Ok(f) => Some(f),
|
||||
Err(e) => {
|
||||
error!("Failed to parse feed {}: {:?}",
|
||||
feed_config.url,
|
||||
e
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let feed = if feed.is_none() {
|
||||
info!("Sleeping for {} seconds before refreshing this feed", feed_config.delay);
|
||||
sleep(Duration::from_secs(feed_config.delay)).await;
|
||||
continue;
|
||||
} else {
|
||||
feed.unwrap()
|
||||
};
|
||||
|
||||
let rooms = feed_config.rooms.clone();
|
||||
|
||||
|
@ -32,6 +32,8 @@ use tokio::{
|
||||
sync::broadcast
|
||||
};
|
||||
|
||||
use tracing::info;
|
||||
|
||||
async fn on_stripped_state_member(
|
||||
room_member: StrippedRoomMemberEvent,
|
||||
client: Client,
|
||||
@ -97,18 +99,24 @@ pub async fn login_and_sync<T: Clone>(
|
||||
loop {
|
||||
tokio::select! {
|
||||
Ok(_) = client.sync(settings.clone()) => return Ok(()),
|
||||
Ok((msg, rooms)) = rx.recv() => {
|
||||
recv = rx.recv() => match recv {
|
||||
Ok((msg, rooms)) => {
|
||||
|
||||
let msg = &msg.clone();
|
||||
for r in rooms {
|
||||
info!("Joining room {}", r);
|
||||
let room = client.join_room_by_id(<&RoomId>::try_from(r.as_str())
|
||||
.expect("invalid room ID")
|
||||
).await.expect(format!("Failed to join room {}", r).as_str());
|
||||
|
||||
info!("Posting to room {}", r);
|
||||
room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await
|
||||
.expect("Failed to send matrix event");
|
||||
}
|
||||
|
||||
},
|
||||
Err(e) => {
|
||||
panic!("Broadcast channel is lagging: {e:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user