File size: 1,166 Bytes
8305bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a9a981
 
 
8305bf3
8a9a981
 
 
 
 
 
 
 
 
8305bf3
8a9a981
 
8305bf3
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
import requests
import urllib.parse

#----------------- CONFIGURATION -----------------------------
GOOGLE_API_KEY = "GOOGLE API key"
GOOGLE_CX = ""

def duckduckgo_search(query):
    """Search duck duck go using instant Answer API."""
    try:
        url = "https://api.duckduckgo.com/"
        params = {"q": query, "format": "json", "no_redirect": 1, "no_html":1}
        response = requests.get(url, params=params, timeout=10)
        response.raise_for_status()
        data = response.json()
        results = []
        if data.get("AbstractText"):
            results.append({
                "title": data.get("Heading"),
                "link": data.get("AbstractURL"),
                "snippet": data.get("AbstractText")
            })
        for topic in data.get("RelatedTopics", []):
            if "Text" in topic and "FirstURL" in topic:
                results.append({
                    "title": topic.get("Text"),
                    "link": topic.get("FirstURL"),
                    "snippet": topic.get("Text")
                })
        return results

    except error:
        print(f"[manyata error] {error}")
        return []