| | import requests |
| | import urllib.parse |
| |
|
| | |
| | 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 [] |
| | |