File size: 521 Bytes
6d24925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
import requests
from typing import List, Dict

def fetch_google_news(query: str, api_key: str, cse_id: str, num_results: int = 10) -> List[Dict]:
    url = "https://www.googleapis.com/customsearch/v1"
    params = {
        "q": query,
        "key": api_key,
        "cx": cse_id,
        "num": num_results,
    }
    response = requests.get(url, params=params)
    if response.status_code != 200:
        raise Exception(f"Google News API error: {response.text}")
    return response.json().get("items", [])