File size: 1,974 Bytes
f89a3d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51d2734
 
f89a3d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51d2734
 
f89a3d6
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import Any

import httpx
from dotenv import load_dotenv

load_dotenv()

GOOGLE_FC_ENDPOINT = "https://factchecktools.googleapis.com/v1alpha1/claims:search"


async def search_google_factcheck(
    query: str,
    *,
    language_code: str = "ar",
    page_size: int = 5,
    api_key: str | None = None,
) -> list[dict[str, Any]]:
    resolved_api_key = api_key or os.getenv(
        "GOOGLE_FACTCHECK_API_KEY", "").strip()
    if not resolved_api_key:
        print("[GoogleFC] No API key found in GOOGLE_FACTCHECK_API_KEY.")
        return []

    params = {
        "query": query,
        "key": resolved_api_key,
        "languageCode": language_code,
        "pageSize": page_size,
    }

    try:
        async with httpx.AsyncClient(timeout=10) as client:
            response = await client.get(GOOGLE_FC_ENDPOINT, params=params)
            response.raise_for_status()
            data = response.json()
    except httpx.HTTPStatusError as exc:
        print(f"[GoogleFC] HTTP error: {exc.response.status_code}")
        return []
    except Exception as exc:
        print(f"[GoogleFC] Request error: {exc}")
        return []

    evidence: list[dict[str, Any]] = []
    for claim_item in data.get("claims", []):
        claim_text = str(claim_item.get("text", "")).strip()
        for review in claim_item.get("claimReview", []):
            publisher = str(review.get(
                "publisher", {}).get("name", "")).strip()
            rating = str(review.get("textualRating", "")).strip()
            url = str(review.get("url", "")).strip()
            title = str(review.get("title", "")).strip() or claim_text

            evidence.append(
                {
                    "snippet": f"[{publisher}] {title} — التقييم: {rating}",
                    "source_url": url,
                    "source": "google_factcheck",
                    "rating": rating or None,
                }
            )

    return evidence