#![allow(dead_code)] use crate::meta_search::models::SearchResult; use crate::meta_search::providers::{SearchContext, SearchProvider}; use anyhow::Result; use async_trait::async_trait; use scraper::{Html, Selector}; use std::sync::Arc; pub struct MediumProvider; #[async_trait] impl SearchProvider for MediumProvider { fn name(&self) -> &'static str { "medium" } async fn search(&self, query: &str, ctx: Arc) -> Result> { let client = crate::meta_search::providers::build_client_with_transport(&ctx, 15).await?; let url = format!("https://medium.com/search?q={}", urlencoding::encode(query)); let resp = client.get(url).send().await?.text().await?; let document = Html::parse_document(&resp); let article_selector = Selector::parse("article").unwrap(); let title_selector = Selector::parse("h2").unwrap(); let link_selector = Selector::parse("a[href*='/p/']").unwrap(); let mut results = Vec::new(); for element in document.select(&article_selector) { if let Some(title_el) = element.select(&title_selector).next() { let title = title_el.text().collect::(); let url = element .select(&link_selector) .next() .and_then(|e| e.value().attr("href")) .unwrap_or(""); let final_url = if url.starts_with('/') { format!("https://medium.com{}", url) } else { url.to_string() }; if !final_url.is_empty() && !title.is_empty() { results.push(SearchResult { title: title.trim().to_string(), url: final_url, description: "Article on Medium".to_string(), source: "medium".to_string(), score: 0.6, media_type: "text".to_string(), thumbnail_url: None, published_at: None, }); } } } Ok(results) } fn as_any(&self) -> &dyn std::any::Any { self } }