forked from frdel/agent-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pr/15' into development
- Loading branch information
Showing
4 changed files
with
67 additions
and
6 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
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
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,39 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
from urllib.parse import urlparse | ||
from newspaper import Article | ||
from python.helpers.tool import Tool, Response | ||
|
||
class WebpageContentTool(Tool): | ||
def execute(self, url="", **kwargs): | ||
if not url: | ||
return Response(message="Error: No URL provided.", break_loop=False) | ||
|
||
try: | ||
# Validate URL | ||
parsed_url = urlparse(url) | ||
if not all([parsed_url.scheme, parsed_url.netloc]): | ||
return Response(message="Error: Invalid URL format.", break_loop=False) | ||
|
||
# Fetch webpage content | ||
response = requests.get(url, timeout=10) | ||
response.raise_for_status() | ||
|
||
# Use newspaper3k for article extraction | ||
article = Article(url) | ||
article.download() | ||
article.parse() | ||
|
||
# If it's not an article, fall back to BeautifulSoup | ||
if not article.text: | ||
soup = BeautifulSoup(response.content, 'html.parser') | ||
text_content = ' '.join(soup.stripped_strings) | ||
else: | ||
text_content = article.text | ||
|
||
return Response(message=f"Webpage content:\n\n{text_content}", break_loop=False) | ||
|
||
except requests.RequestException as e: | ||
return Response(message=f"Error fetching webpage: {str(e)}", break_loop=False) | ||
except Exception as e: | ||
return Response(message=f"An error occurred: {str(e)}", break_loop=False) |
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