Add support for DuckDuckGo search engine in web search API

This commit is contained in:
Cohee
2023-11-28 22:39:35 +02:00
parent 05cab1c918
commit aded5b4363
3 changed files with 30 additions and 5 deletions

View File

@@ -560,9 +560,10 @@ Animated transparent image
### Perform web search
`POST /api/websearch`
Available engines: `google` (default), `duckduckgo`
#### **Input**
```
{ "query": "what is beauty?" }
{ "query": "what is beauty?", "engine": "google" }
```
#### **Output**
```

View File

@@ -4,6 +4,7 @@ from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
import atexit
def get_driver():
@@ -27,8 +28,8 @@ def get_driver():
def search_google(query: str) -> str:
driver = get_driver()
print("Searching for " + query + "...")
global driver
print(f"Searching Google for {query}...")
driver.get("https://google.com/search?hl=en&q=" + query)
text = ''
# Answer box
@@ -44,5 +45,23 @@ def search_google(query: str) -> str:
if el and el.text:
text += el.text + '\n'
print("Found: " + text)
driver.quit()
return text
def search_duckduckgo(query: str) -> str:
global driver
print(f"Searching DuckDuckGo for {query}...")
driver.get("https://duckduckgo.com/?kp=-2&kl=wt-wt&q=" + query)
text = ''
for el in driver.find_elements(By.CSS_SELECTOR, '[data-result="snippet"]'):
if el and el.text:
text += el.text + '\n'
print("Found: " + text)
return text
driver = get_driver()
def quit_driver():
driver.quit()
atexit.register(quit_driver)

View File

@@ -1108,8 +1108,13 @@ def api_websearch():
abort(400, '"query" is required')
query = data["query"]
engine = data["engine"] if "engine" in data else "google"
import modules.websearch.script as websearch
results = websearch.search_google(query)
if engine == "duckduckgo":
results = websearch.search_duckduckgo(query)
else:
results = websearch.search_google(query)
return jsonify({"results": results})