use crate::errors::Result;
use crate::extractors::common::{html_utils, url_utils};
use crate::types::oembed::{OEmbedDiscovery, OEmbedEndpoint, OEmbedFormat};
#[cfg(test)]
mod tests;
pub fn extract(html: &str, base_url: Option<&str>) -> Result<OEmbedDiscovery> {
let document = html_utils::parse_html(html);
let mut discovery = OEmbedDiscovery::default();
if let Ok(selector) = html_utils::create_selector("link[rel~=\"alternate\"][type][href]") {
for element in document.select(&selector) {
if let (Some(link_type), Some(href)) =
(html_utils::get_attr(&element, "type"), html_utils::get_attr(&element, "href"))
{
if href.trim().is_empty() {
continue;
}
let resolved_href = url_utils::resolve_url(base_url, &href).unwrap_or(href.clone());
let title = html_utils::get_attr(&element, "title");
let link_type_lower = link_type.to_lowercase();
if link_type_lower.contains("oembed") {
let endpoint = OEmbedEndpoint {
href: resolved_href,
format: if link_type_lower.contains("json") {
OEmbedFormat::Json
} else if link_type_lower.contains("xml") {
OEmbedFormat::Xml
} else {
OEmbedFormat::Json
},
title,
};
match endpoint.format {
OEmbedFormat::Json => discovery.json_endpoints.push(endpoint),
OEmbedFormat::Xml => discovery.xml_endpoints.push(endpoint),
}
}
}
}
}
Ok(discovery)
}