Spaces:
Runtime error
Runtime error
import requests | |
import os | |
API_KEY = os.getenv("GOOGLE_API_KEY") | |
CX_ID = os.getenv("GOOGLE_CSE_ID") | |
def google_search(query, num_results=10): | |
if not API_KEY or not CX_ID: | |
return [] | |
search_url = "https://www.googleapis.com/customsearch/v1" | |
params = { | |
"key": API_KEY, | |
"cx": CX_ID, | |
"q": query, | |
"num": num_results | |
} | |
response = requests.get(search_url, params=params) | |
if response.status_code == 200: | |
data = response.json() | |
results = [] | |
for item in data.get("items", []): | |
results.append({ | |
"title": item.get("title"), | |
"link": item.get("link"), | |
"snippet": item.get("snippet", "") | |
}) | |
return results | |
else: | |
print("Google Search Error:", response.text) | |
return [] | |