Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect format by content type header #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/feed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ impl<'a> Feed {
Ok(feed)
}

pub fn read_rss_from<R>(reader: R) -> Result<Self, FeedError>
where
R: BufRead,
{
let channel = rss::Channel::read_from(reader)?;

debug!(format = "RSS", items = channel.items().len(), "parsed feed");

Ok(Self::Rss(channel))
}

pub fn read_atom_from<R>(reader: R) -> Result<Self, FeedError>
where
R: BufRead,
{
let feed = atom_syndication::Feed::read_from(reader)?;

debug!(format = "Atom", items = feed.entries().len(), "parsed feed");

Ok(Self::Atom(feed))
}

pub fn title(&self) -> &str {
match self {
Feed::Rss(c) => &c.title,
Expand Down
18 changes: 15 additions & 3 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use std::time::Duration;

use chrono::{DateTime, FixedOffset};
use reqwest::{Client, IntoUrl, Url};
use reqwest::{header, Client, IntoUrl, Url};
use tokio::sync::broadcast::Receiver;
use tracing::{debug, error};

Expand Down Expand Up @@ -167,9 +167,21 @@ impl<T: Sink> Watcher<T> {
debug!("fetching feed");

let res = self.client.get(self.url.as_ref()).send().await?;
let body = res.error_for_status()?.bytes().await?;

let feed = Feed::read_from(&body[..])?;
let feed = match res.headers().get(header::CONTENT_TYPE) {
Some(v) if v.as_bytes().starts_with(b"application/rss+xml") => {
let body = res.error_for_status()?.bytes().await?;
Feed::read_rss_from(&body[..])?
}
Some(v) if v.as_bytes().starts_with(b"application/atom+xml") => {
let body = res.error_for_status()?.bytes().await?;
Feed::read_atom_from(&body[..])?
}
_ => {
let body = res.error_for_status()?.bytes().await?;
Feed::read_from(&body[..])?
}
};

Ok(feed)
}
Expand Down