add http-json, rss, and media data source adapters
http-json: generic HTTP+JSON polling adapter, converts serde_json to domain Value. 4 tests. rss: XML RSS feed parser, extracts items into Value array. 1 test. media: Navidrome/Subsonic getNowPlaying adapter. 2 tests with fake server.
This commit is contained in:
144
crates/adapters/rss/src/lib.rs
Normal file
144
crates/adapters/rss/src/lib.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
use std::collections::BTreeMap;
|
||||
use domain::{DataSource, DataSourcePort, Value};
|
||||
use quick_xml::events::Event;
|
||||
use quick_xml::Reader;
|
||||
|
||||
pub struct RssAdapter {
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RssError {
|
||||
Request(reqwest::Error),
|
||||
NoUrl,
|
||||
Parse(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for RssError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
RssError::Request(e) => write!(f, "request: {e}"),
|
||||
RssError::NoUrl => write!(f, "no url configured"),
|
||||
RssError::Parse(e) => write!(f, "parse: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RssAdapter {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_rss(xml: &str) -> Result<Value, RssError> {
|
||||
let mut reader = Reader::from_str(xml);
|
||||
let mut items: Vec<Value> = Vec::new();
|
||||
let mut current_item: Option<BTreeMap<String, Value>> = None;
|
||||
let mut current_tag = String::new();
|
||||
let mut in_channel = false;
|
||||
let mut channel_title = String::new();
|
||||
let mut channel_link = String::new();
|
||||
|
||||
loop {
|
||||
match reader.read_event() {
|
||||
Ok(Event::Start(e)) => {
|
||||
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
|
||||
match tag.as_str() {
|
||||
"channel" => in_channel = true,
|
||||
"item" => { current_item = Some(BTreeMap::new()); }
|
||||
_ => current_tag = tag,
|
||||
}
|
||||
}
|
||||
Ok(Event::End(e)) => {
|
||||
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
|
||||
if tag == "item" {
|
||||
if let Some(item) = current_item.take() {
|
||||
items.push(Value::Object(item));
|
||||
}
|
||||
}
|
||||
current_tag.clear();
|
||||
}
|
||||
Ok(Event::Text(e)) => {
|
||||
let text = e.unescape().unwrap_or_default().to_string();
|
||||
if !current_tag.is_empty() && !text.trim().is_empty() {
|
||||
if let Some(item) = current_item.as_mut() {
|
||||
item.insert(current_tag.clone(), Value::String(text));
|
||||
} else if in_channel {
|
||||
match current_tag.as_str() {
|
||||
"title" => channel_title = text,
|
||||
"link" => channel_link = text,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Event::CData(e)) => {
|
||||
let text = String::from_utf8_lossy(&e).to_string();
|
||||
if !current_tag.is_empty() {
|
||||
if let Some(item) = current_item.as_mut() {
|
||||
item.insert(current_tag.clone(), Value::String(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Event::Eof) => break,
|
||||
Err(e) => return Err(RssError::Parse(format!("{e}"))),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = BTreeMap::new();
|
||||
result.insert("title".into(), Value::String(channel_title));
|
||||
result.insert("link".into(), Value::String(channel_link));
|
||||
result.insert("count".into(), Value::Number(items.len() as f64));
|
||||
result.insert("items".into(), Value::Array(items));
|
||||
|
||||
Ok(Value::Object(result))
|
||||
}
|
||||
|
||||
impl DataSourcePort for RssAdapter {
|
||||
type Error = RssError;
|
||||
|
||||
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> {
|
||||
let url = source.config.url.as_ref().ok_or(RssError::NoUrl)?;
|
||||
|
||||
let resp = self.client.get(url).send().await.map_err(RssError::Request)?;
|
||||
let xml = resp.text().await.map_err(RssError::Request)?;
|
||||
|
||||
parse_rss(&xml)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const SAMPLE_RSS: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>Test Feed</title>
|
||||
<link>https://example.com</link>
|
||||
<item>
|
||||
<title>First Article</title>
|
||||
<description>Description of first article</description>
|
||||
<link>https://example.com/1</link>
|
||||
</item>
|
||||
<item>
|
||||
<title>Second Article</title>
|
||||
<description>Description of second</description>
|
||||
<link>https://example.com/2</link>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>"#;
|
||||
|
||||
#[test]
|
||||
fn parses_rss_into_value() {
|
||||
let result = parse_rss(SAMPLE_RSS).unwrap();
|
||||
|
||||
assert_eq!(result.get_path("$.title"), Some(&Value::String("Test Feed".into())));
|
||||
assert_eq!(result.get_path("$.items[0].title"), Some(&Value::String("First Article".into())));
|
||||
assert_eq!(result.get_path("$.items[1].title"), Some(&Value::String("Second Article".into())));
|
||||
assert_eq!(result.get_path("$.items[0].description"), Some(&Value::String("Description of first article".into())));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user