Spaces:
Runtime error
Runtime error
Create google_search.py
Browse files- src/google_search.py +15 -0
src/google_search.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
def google_search(query, num=5):
|
| 5 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 6 |
+
cse_id = os.getenv("GOOGLE_CSE_ID")
|
| 7 |
+
url = "https://www.googleapis.com/customsearch/v1"
|
| 8 |
+
params = {
|
| 9 |
+
"key": api_key,
|
| 10 |
+
"cx": cse_id,
|
| 11 |
+
"q": query,
|
| 12 |
+
"num": num
|
| 13 |
+
}
|
| 14 |
+
res = requests.get(url, params=params).json()
|
| 15 |
+
return [item["snippet"] + "\nURL: " + item["link"] for item in res.get("items", [])]
|