Remove import and add more commentaries

This commit is contained in:
Andros Fenollosa 2024-02-09 08:39:07 +01:00
parent 377c6ebc81
commit b337375e38
3 changed files with 23 additions and 1 deletions

View File

@ -3,6 +3,7 @@ from bs4 import BeautifulSoup
from typing import Union
class DuckDuckGoGateway:
"""Gateway to DuckDuckGo search engine"""
url = "https://duckduckgo.com/html/"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:84.0) Gecko/20100101 Firefox/84.0",

View File

@ -1,4 +1,3 @@
from typing import Union
from fastapi import FastAPI
from gateways.externals import DuckDuckGoGateway
from use_cases import search_ddg
@ -7,5 +6,10 @@ app = FastAPI()
@app.get("/search/")
def api_search(q: str):
"""Search for a query in DuckDuckGo.
Args:
q (str): The query to search for.
Returns:
dict: The search results."""
repo = DuckDuckGoGateway()
return search_ddg(repo, q)

View File

@ -1,4 +1,21 @@
from typing import Union
def search_ddg(repo, query: str) -> Union[dict, list]:
"""Search DuckDuckGo for a query
Args:
repo (DuckDuckGoGateway): The DuckDuckGoGateway repository
query (str): The query to search for
Returns:
Union[dict, list]: A list of search results
Example:
[
{
"title": "DuckDuckGo — Privacy, simplified.",
"link": "https://duckduckgo.com/",
"body": "The Internet privacy company that empowers you to seamlessly take control of your personal information online, without any tradeoffs.",
"icon": "/assets/meta/DDG-icon_256x256.png",
},
...
]
"""
return repo.search(query)