#![allow(dead_code)] use crate::cloudflare_bypass::detector::CloudflareDetector; 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; use tracing::{info, warn as tracing_warn}; pub struct GoogleProvider; #[async_trait] impl SearchProvider for GoogleProvider { fn name(&self) -> &'static str { "google" } async fn search(&self, query: &str, ctx: Arc) -> Result> { let client = crate::meta_search::providers::build_client_with_transport(&ctx, 15).await?; info!("Using Tor/proxy transport for Google"); let url = format!( "https://www.google.com/search?q={}&num=50", urlencoding::encode(query) ); let resp = client.get(&url).send().await?; let status = resp.status().as_u16(); let body = resp.text().await?; // Skip Cloudflare/Google challenge pages if !CloudflareDetector::is_usable_content(status, &body) { tracing_warn!("Google returned challenge page, skipping"); return Ok(vec![]); } let document = Html::parse_document(&body); let result_selector = Selector::parse("div.g").unwrap(); let title_selector = Selector::parse("h3").unwrap(); let link_selector = Selector::parse("a").unwrap(); let snippet_selector = Selector::parse("div.VwiC3b").unwrap(); let mut results = Vec::new(); for element in document.select(&result_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("") .to_string(); let description = element .select(&snippet_selector) .next() .map(|e| e.text().collect::()) .unwrap_or_default(); if !url.is_empty() && !title.is_empty() { results.push(SearchResult { title, url, description, source: "google".to_string(), score: 1.0, media_type: "text".to_string(), thumbnail_url: None, published_at: None, }); } } } if results.is_empty() && body.contains("captcha") { tracing_warn!("Google search was blocked by CAPTCHA"); return Err(anyhow::anyhow!("Blocked by CAPTCHA")); } Ok(results) } fn as_any(&self) -> &dyn std::any::Any { self } }