File size: 850 Bytes
826a1b8
 
 
 
 
206e141
826a1b8
 
 
 
 
 
 
 
 
8121f99
826a1b8
 
 
 
 
 
 
8121f99
 
 
 
 
826a1b8
 
 
 
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
# nuse_modules/google_search.py

import os
import requests

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
GOOGLE_CX_ID = os.getenv("GOOGLE_CX_ID")

def search_google_news(keywords: list[str], num_results: int = 5):
    query = " ".join(keywords)
    url = (
        f"https://www.googleapis.com/customsearch/v1"
        f"?key={GOOGLE_API_KEY}&cx={GOOGLE_CX_ID}"
        f"&q={query}&num={num_results}"
    )
    
    try:
        res = requests.get(url, timeout=10)
        res.raise_for_status()
        data = res.json()
        results = []

        for item in data.get("items", []):
            results.append({
                "title": item.get("title"),
                "link": item.get("link"),
                "snippet": item.get("snippet"),
            })

        return results
    except Exception as e:
        return {"error": str(e)}