File size: 987 Bytes
860c2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from dotenv import load_dotenv

load_dotenv(".env")

API_KEY = os.getenv("GOOGLE_API_KEY")
CSE_ID = os.getenv("GOOGLE_CSE_ID")
# QUERY = "OpenAI GPT-4"
QUERY = "IPL 2025 final points table predictions team performance analysis"

if not API_KEY or not CSE_ID:
    print("Missing API key or Search Engine ID")
    exit(1)

params = {
    "q": QUERY,
    "key": API_KEY,
    "cx": CSE_ID
}

response = requests.get("https://www.googleapis.com/customsearch/v1", params=params)

if response.status_code != 200:
    print("Error:", response.status_code, response.text)
else:
    data = response.json()
    results = data.get("items", [])
    if not results:
        print("API is working, but no search results found.")
    else:
        for i, result in enumerate(results[:3], 1):
            print(f"\nResult {i}:")
            print("Title:", result.get("title"))
            print("Link:", result.get("link"))
            print("Snippet:", result.get("snippet"))