Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Merge branch 'master' into browse_scrape_links_test_and_validate

Itamar Friedman54478b35f2c6
diff --cc scripts/browse.py
index 78fe4ae252,b936c5b197..6d473ed6ba
--- a/scripts/browse.py
+++ b/scripts/browse.py
@@@ -23,38 -32,31 +23,39 @@@ def check_local_file_access(url)
local_prefixes = ['file:///', 'file://localhost', 'http://localhost', 'https://localhost']
return any(url.startswith(prefix) for prefix in local_prefixes)
-def scrape_text(url):
- """Scrape text from a webpage"""
- # Basic check if the URL is valid
- if not url.startswith('http'):
- return "Error: Invalid URL"
+def get_response(url, headers=cfg.user_agent_header, timeout=10):
+ try:
+ # Restrict access to local files
+ if check_local_file_access(url):
+ raise ValueError('Access to local files is restricted')
+
+ # Most basic check if the URL is valid:
+ if not url.startswith('http://') and not url.startswith('https://'):
+ raise ValueError('Invalid URL format')
- # Restrict access to local files
- if check_local_file_access(url):
- return "Error: Access to local files is restricted"
+
- # Validate the input URL
- if not is_valid_url(url):
- # Sanitize the input URL
sanitized_url = sanitize_url(url)
- # Make the request with a timeout and handle exceptions
- response = make_request(sanitized_url)
+ response = requests.get(sanitized_url, headers=headers, timeout=timeout)
- if isinstance(response, str):
- return response
- else:
- # Sanitize the input URL
- sanitized_url = sanitize_url(url)
+ # Check if the response contains an HTTP error
+ if response.status_code >= 400:
+ return None, "Error: HTTP " + str(response.status_code) + " error"
+
+ return response, None
+ except ValueError as ve:
+ # Handle invalid URL format
+ return None, "Error: " + str(ve)
- response = requests.get(sanitized_url, headers=cfg.user_agent_header)
+ except requests.exceptions.RequestException as re:
+ # Handle exceptions related to the HTTP request (e.g., connection errors, timeouts, etc.)
+ return None, "Error: " + str(re)
+
+def scrape_text(url):
+ """Scrape text from a webpage"""
+ response, error_message = get_response(url)
+ if error_message:
+ return error_message
soup = BeautifulSoup(response.text, "html.parser")