-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathinternet.py
202 lines (171 loc) · 5.24 KB
/
internet.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import re
import os
import logging
from typing import Any, Optional, Tuple
from requests import RequestException, Timeout
from holmes.core.tools import Tool, ToolParameter, Toolset, ToolsetTag
from markdownify import markdownify
from bs4 import BeautifulSoup
import requests
# TODO: change and make it holmes
INTERNET_TOOLSET_USER_AGENT = os.environ.get(
"INTERNET_TOOLSET_USER_AGENT",
"Mozilla/5.0 (X11; Linux x86_64; rv:128.0; holmesgpt;) Gecko/20100101 Firefox/128.0",
)
INTERNET_TOOLSET_TIMEOUT_SECONDS = int(
os.environ.get("INTERNET_TOOLSET_TIMEOUT_SECONDS", "60")
)
SELECTORS_TO_REMOVE = [
"script",
"style",
"meta",
"link",
"noscript",
"header",
"footer",
"nav",
"iframe",
"svg",
"img",
"button",
"menu",
"sidebar",
"aside",
".header" ".footer" ".navigation",
".nav",
".menu",
".sidebar",
".ad",
".advertisement",
".social",
".popup",
".modal",
".banner",
".cookie-notice",
".social-share",
".related-articles",
".recommended",
"#header" "#footer" "#navigation",
"#nav",
"#menu",
"#sidebar",
"#ad",
"#advertisement",
"#social",
"#popup",
"#modal",
"#banner",
"#cookie-notice",
"#social-share",
"#related-articles",
"#recommended",
]
def scrape(url) -> Tuple[Optional[str], Optional[str]]:
response = None
content = None
mime_type = None
try:
response = requests.get(
url,
headers={"User-Agent": INTERNET_TOOLSET_USER_AGENT},
timeout=INTERNET_TOOLSET_TIMEOUT_SECONDS,
)
response.raise_for_status()
except Timeout:
logging.error(
f"Failed to load {url}. Timeout after {INTERNET_TOOLSET_TIMEOUT_SECONDS} seconds",
exc_info=True,
)
except RequestException as e:
logging.error(f"Failed to load {url}: {str(e)}", exc_info=True)
return None, None
if response:
content = response.text
try:
content_type = response.headers["content-type"]
if content_type:
mime_type = content_type.split(";")[0]
except Exception:
logging.info(
f"Failed to parse content type from headers {response.headers}"
)
return (content, mime_type)
def cleanup(soup: BeautifulSoup):
"""Remove all elements that are irrelevant to the textual representation of a web page.
This includes images, extra data, even links as there is no intention to navigate from that page.
"""
for selector in SELECTORS_TO_REMOVE:
for element in soup.select(selector):
element.decompose()
for tag in soup.find_all(True):
for attr in list(tag.attrs):
if attr != "href":
tag.attrs.pop(attr, None)
return soup
def html_to_markdown(page_source: str):
soup = BeautifulSoup(page_source, "html.parser")
soup = cleanup(soup)
page_source = str(soup)
try:
md = markdownify(page_source)
except OSError as e:
logging.error(
f"There was an error in converting the HTML to markdown. Falling back to returning the raw HTML. Error: {str(e)}"
)
return page_source
md = re.sub(r"</div>", " ", md)
md = re.sub(r"<div>", " ", md)
md = re.sub(r"\n\s*\n", "\n\n", md)
return md
def looks_like_html(content):
"""
Check if the content looks like HTML.
"""
if isinstance(content, str):
# Check for common HTML tags
html_patterns = [r"<!DOCTYPE\s+html", r"<html", r"<head", r"<body"]
return any(
re.search(pattern, content, re.IGNORECASE) for pattern in html_patterns
)
return False
class FetchWebpage(Tool):
def __init__(self):
super().__init__(
name="fetch_webpage",
description="Fetch a webpage. Use this to fetch runbooks if they are present before starting your investigation (if no other tool like confluence is more appropriate)",
parameters={
"url": ToolParameter(
description="The URL to fetch",
type="string",
required=True,
)
},
)
def invoke(self, params: Any) -> str:
url: str = params["url"]
content, mime_type = scrape(url)
if not content:
logging.error(f"Failed to retrieve content from {url}")
return ""
# Check if the content is HTML based on MIME type or content
if (mime_type and mime_type.startswith("text/html")) or (
mime_type is None and looks_like_html(content)
):
content = html_to_markdown(content)
return content
def get_parameterized_one_liner(self, params) -> str:
url: str = params["url"]
return f"fetched webpage {url}"
class InternetToolset(Toolset):
def __init__(self):
super().__init__(
name="internet",
description="Fetch webpages",
icon_url="https://platform.robusta.dev/demos/internet-access.svg",
prerequisites=[],
tools=[FetchWebpage()],
tags=[
ToolsetTag.CORE,
],
is_default=True,
)