Create WebScape_TOOL.py
Browse files- WebScape_TOOL.py +99 -0
WebScape_TOOL.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import tool
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import requests
|
| 4 |
+
from langchain.tools import tool
|
| 5 |
+
from duckduckgo_search import DDGS
|
| 6 |
+
|
| 7 |
+
class WebSearchTools:
|
| 8 |
+
|
| 9 |
+
#################################### - DDG SEARCH ENGINE TOOL - ####################################
|
| 10 |
+
@tool("internet_search")
|
| 11 |
+
def internet_search(query: str) -> str:
|
| 12 |
+
"""
|
| 13 |
+
Performs an internet search using a DuckDuckGo-like service and returns the results.
|
| 14 |
+
|
| 15 |
+
Parameters:
|
| 16 |
+
query (str): The search query.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: The search results or a message indicating no results were found.
|
| 20 |
+
"""
|
| 21 |
+
# Assuming `ddgs` is initialized and ready to use here, with a context manager support
|
| 22 |
+
with DDGS() as ddgs:
|
| 23 |
+
results = [r for r in ddgs.text(query, max_results=3)]
|
| 24 |
+
return results if results else "No results found."
|
| 25 |
+
#################################### - DDG SEARCH ENGINE TOOL - ####################################
|
| 26 |
+
|
| 27 |
+
####################$################ - BS4 URL SCRAPER TOOL - ############$########################
|
| 28 |
+
@staticmethod
|
| 29 |
+
@tool("process_search_results", return_direct=False)
|
| 30 |
+
def process_search_results(url: str) -> str:
|
| 31 |
+
"""
|
| 32 |
+
Processes the content from webpages given a URL using BeautifulSoup.
|
| 33 |
+
|
| 34 |
+
Parameters:
|
| 35 |
+
url (str): The URL to fetch and process.
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
str: The text content of the webpage.
|
| 39 |
+
"""
|
| 40 |
+
response = requests.get(url=url)
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
| 43 |
+
return soup.get_text()
|
| 44 |
+
else:
|
| 45 |
+
return "Failed to fetch content."
|
| 46 |
+
####################$################ - BS4 URL SCRAPER TOOL - ############$########################
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@tool("forecast search")
|
| 52 |
+
def forecast_search(query: str) -> str:
|
| 53 |
+
"""
|
| 54 |
+
Performs an internet search using a DuckDuckGo-like service and returns the results.
|
| 55 |
+
|
| 56 |
+
Parameters:
|
| 57 |
+
query (str): The search query.
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
str: The search results or a message indicating no results were found.
|
| 61 |
+
"""
|
| 62 |
+
# Assuming `ddgs` is initialized and ready to use here, with a context manager support
|
| 63 |
+
with DDGS() as ddgs:
|
| 64 |
+
results = [r for r in ddgs.text(f"site:digitalcoinprice.com/forecast {query}", max_results=4)]
|
| 65 |
+
return results if results else "No results found."
|
| 66 |
+
|
| 67 |
+
@tool("technical signal search")
|
| 68 |
+
def technicalsignals_search(query: str) -> str:
|
| 69 |
+
"""
|
| 70 |
+
Performs an internet search using a DuckDuckGo-like service and returns the results.
|
| 71 |
+
|
| 72 |
+
Parameters:
|
| 73 |
+
query (str): The search query.
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
str: The search results or a message indicating no results were found.
|
| 77 |
+
"""
|
| 78 |
+
# Assuming `ddgs` is initialized and ready to use here, with a context manager support
|
| 79 |
+
with DDGS() as ddgs:
|
| 80 |
+
results = [r for r in ddgs.text(f"site:centralcharts.com signals {query}", max_results=4)]
|
| 81 |
+
return results if results else "No results found."
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@tool("price target search")
|
| 85 |
+
def pricetargets_search(query: str) -> str:
|
| 86 |
+
"""
|
| 87 |
+
Performs an internet search using a DuckDuckGo-like service and returns the results
|
| 88 |
+
|
| 89 |
+
Parameters:
|
| 90 |
+
query (str): The search query.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
str: The search results or a message indicating no results were found.
|
| 94 |
+
"""
|
| 95 |
+
# Assuming `ddgs` is initialized and ready to use here, with a context manager support
|
| 96 |
+
with DDGS() as ddgs:
|
| 97 |
+
results = [r for r in ddgs.text(f"site:coincodex.com prediction {query}", max_results=3)]
|
| 98 |
+
return results if results else "No results found."
|
| 99 |
+
|