-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #455 from l3vels/feat/search-api
feat: google search suggestions
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter, Depends | ||
from fastapi.responses import JSONResponse | ||
from serpapi.google_search import GoogleSearch | ||
|
||
router = APIRouter() | ||
|
||
|
||
# Assuming you have a function to fetch the secret API key | ||
def get_secret_api_key(): | ||
# Your implementation to get the API key | ||
return "be37de5344aaa3f5444f1083f4f303a11cce32bd84456a7387a52efeab006e88" | ||
|
||
|
||
@router.post("/suggestions") | ||
def autocomplete_search(q: str, api_key: str = Depends(get_secret_api_key)): | ||
""" | ||
Perform autocomplete search using Serpapi. | ||
Args: | ||
q (str): The query string for autocomplete search. | ||
api_key (str): Serpapi API key. | ||
Returns: | ||
List[str]: List of autocomplete suggestions. | ||
""" | ||
params = {"engine": "google_autocomplete", "q": q, "api_key": api_key} | ||
|
||
search = GoogleSearch(params) | ||
results = search.get_dict() | ||
suggestions = results["suggestions"] | ||
return JSONResponse(content=suggestions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters