-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_search.py
More file actions
80 lines (63 loc) · 2.35 KB
/
web_search.py
File metadata and controls
80 lines (63 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
web_search.py
Web search and browsing functions (duckduckgo, readability...)
"""
from ddgs import DDGS
import requests
from bs4 import BeautifulSoup
from readability import Document
import random
import conf_module
def browse(query: str, num_results: int = 5) -> str:
"""
Browse the web or search using DuckDuckGo.
Args:
query (str): The search query or URL to browse.
num_results (int): Number of search results to return if using DuckDuckGo.
Returns:
str: The main text of the webpage if a URL is provided, otherwise search results.
"""
query = query.strip()
if query.lower().startswith(("http://", "https://")):
try:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
response = requests.get(query, headers=headers, timeout=10)
response.raise_for_status()
# Use Readability to extract the main article
doc = Document(response.text)
html = doc.summary()
soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text(separator='\n', strip=True)
return text[:1000]
except Exception as e:
return f"Error: {e}"
else:
results = DDGS().text(query, max_results=num_results)
return {"query": query, "results": results}
def gif(query: str) -> str:
"""
Searches for a GIF on Tenor and returns a random URL from the first 5 results.
Args:
query (str): The search query for the GIF.
Returns:
str: URL of a random GIF from the search results or an error message.
"""
API_KEY = conf_module.load_conf('GIF_TOKEN')
url = "https://tenor.googleapis.com/v2/search"
params = {
"q": query,
"key": API_KEY,
"limit": 5,
"media_filter": "minimal"
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
if "results" in data and len(data["results"]) > 0:
top_five_results = data["results"][:5] # Get only the first 5 results
random_gif = random.choice(top_five_results) # Select a random GIF from these
return random_gif["media_formats"]["gif"]["url"]
return "No GIF found."
except requests.exceptions.RequestException as e:
return f"Error: {e}"