File size: 3,036 Bytes
0ca462b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#![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<SearchContext>) -> Result<Vec<SearchResult>> {
        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::<String>();
                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::<String>())
                    .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
    }
}