-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
277 lines (230 loc) · 6.64 KB
/
scraper.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package goquery
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"github.com/devalexandre/langsmithgo"
"github.com/devalexandre/mylangchaingo"
"github.com/google/uuid"
"golang.org/x/net/html"
"log"
"net/url"
"os"
"regexp"
"runtime"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/tmc/langchaingo/tools"
)
const (
DefaultMaxDepth = 1
DefaultParallels = 2
DefaultDelay = 3
DefaultAsync = true
)
var ErrScrapingFailed = errors.New("scraper could not read URL, or scraping is not allowed for provided URL")
type Scraper struct {
MaxDepth int
Parallels int
Delay int64
Blacklist []string
Async bool
langsmithClient *langsmithgo.Client
}
var _ tools.Tool = Scraper{}
func New(options ...Options) (*Scraper, error) {
scraper := &Scraper{
MaxDepth: DefaultMaxDepth,
Parallels: DefaultParallels,
Delay: int64(DefaultDelay),
Async: DefaultAsync,
Blacklist: []string{
"login", "signup", "signin", "register", "logout", "download", "redirect",
},
}
for _, opt := range options {
opt(scraper)
}
if os.Getenv("LANGCHAIN_TRACING") != "" && os.Getenv("LANGCHAIN_TRACING") != "false" {
client, err := langsmithgo.NewClient()
if err != nil {
return nil, err
}
scraper.langsmithClient = client
root := uuid.New().String()
mylangchaingo.SetRunId(root)
}
return scraper, nil
}
func (s Scraper) Name() string {
return "Web Scraper"
}
func (s Scraper) Description() string {
return "Web Scraper will scan a URL and return the content of the web page. Input should be a working URL."
}
func (s Scraper) Call(ctx context.Context, input string) (string, error) {
urlLink, err := ExtractURL(input)
if err != nil {
return "", fmt.Errorf("failed to extract URL: %w", err)
}
if s.langsmithClient != nil {
err := s.langsmithClient.Run(&langsmithgo.RunPayload{
Name: fmt.Sprintf("%v-%v-%v", langsmithgo.Tool, s.Name(), "GoQuery"),
SessionName: os.Getenv("LANGCHAIN_PROJECT_NAME"),
RunType: langsmithgo.Tool,
RunID: mylangchaingo.GetRunId(),
ParentID: mylangchaingo.GetParentId(),
Inputs: map[string]interface{}{
"payload": input,
},
Extras: map[string]interface{}{
"Metadata": map[string]interface{}{
"langsmithgo_version": "v1.0.0",
"go_version": runtime.Version(),
"platform": runtime.GOOS,
"arch": runtime.GOARCH,
},
},
})
if err != nil {
return "", err
}
}
u, err := url.ParseRequestURI(urlLink)
if err != nil {
return "", fmt.Errorf("%w: %s", ErrScrapingFailed, err)
}
res, err := goquery.NewDocument(urlLink)
if err != nil {
return "", fmt.Errorf("%w: %s", ErrScrapingFailed, err)
}
var siteData strings.Builder
siteData.WriteString("\n\nPage URL: " + input)
title := res.Find("title").Text()
if title != "" {
siteData.WriteString("\nPage Title: " + title)
}
description, _ := res.Find("meta[name='description']").Attr("content")
if description != "" {
siteData.WriteString("\nPage Description: " + description)
}
siteData.WriteString("\nHeaders:")
res.Find("h1, h2, h3, h4, h5, h6").Each(func(i int, sel *goquery.Selection) {
siteData.WriteString("\n" + sel.Text())
})
siteData.WriteString("\nContent:")
res.Find("p").Each(func(i int, sel *goquery.Selection) {
siteData.WriteString("\n" + sel.Text())
})
res.Find("div#content").Each(func(i int, sel *goquery.Selection) {
siteData.WriteString("content:" + sel.Text())
})
res.Find("main#content").Each(func(i int, sel *goquery.Selection) {
siteData.WriteString("content:" + sel.Text())
})
links := make(map[string]bool)
res.Find("a[href]").Each(func(i int, sel *goquery.Selection) {
link, exists := sel.Attr("href")
if exists {
absoluteLink, err := u.Parse(link)
if err != nil {
log.Println(err)
return
}
if absoluteLink.Hostname() != u.Hostname() {
return
}
for _, item := range s.Blacklist {
if strings.Contains(absoluteLink.Path, item) {
return
}
}
if absoluteLink.Path == "/index.html" || absoluteLink.Path == "" {
absoluteLink.Path = "/"
}
if !links[absoluteLink.String()] {
links[absoluteLink.String()] = true
siteData.WriteString("\nLink: " + absoluteLink.String())
}
}
})
siteData.WriteString("\n\nScraped Links:")
for link := range links {
siteData.WriteString("\n" + link)
}
textExtracted, err := ExtractTextFromHTML(siteData.String())
if err != nil {
return "", err
}
response := RemoveBlankLines(textExtracted)
if s.langsmithClient != nil {
err := s.langsmithClient.Run(&langsmithgo.RunPayload{
RunID: mylangchaingo.GetRunId(),
Outputs: map[string]interface{}{
"output": response,
},
})
if err != nil {
return "", fmt.Errorf("error running langsmith: %w", err)
}
}
return response, nil
}
func ExtractURL(str string) (string, error) {
// Este padrão de expressão regular é projetado para corresponder à maioria das URLs.
// Modifique conforme necessário para atender a casos de uso mais específicos.
// O padrão atual busca por protocolos comuns como http, https, ftp, etc.
pattern := `https?://[^\s]+`
re, err := regexp.Compile(pattern)
if err != nil {
return "", err // Retorna um erro se a expressão regular for inválida
}
// Encontra a primeira correspondência na string fornecida.
match := re.FindString(str)
if match == "" {
return "", fmt.Errorf("no URL found in string")
}
//remove > from the end of the URL
if strings.HasSuffix(match, ">") {
match = match[:len(match)-1]
}
return match, nil
}
func ExtractTextFromHTML(htmlContent string) (string, error) {
// Parse the HTML content
doc, err := html.Parse(strings.NewReader(htmlContent))
if err != nil {
return "", err // Retorna um erro se o HTML não puder ser analisado
}
var b bytes.Buffer // Um buffer para armazenar o texto extraído
walkNodes(doc, func(n *html.Node) {
if n.Type == html.TextNode {
b.WriteString(n.Data)
}
})
return b.String(), nil // Retorna o texto extraído
}
// walkNodes percorre os nós do documento HTML e aplica a função fn a cada nó
func walkNodes(n *html.Node, fn func(*html.Node)) {
fn(n) // Aplica a função ao nó atual
for c := n.FirstChild; c != nil; c = c.NextSibling {
walkNodes(c, fn) // Chama a função recursivamente para cada filho
}
}
func RemoveBlankLines(input string) string {
var builder strings.Builder
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
line := scanner.Text()
if strings.TrimSpace(line) != "" {
builder.WriteString(line + "\n")
}
}
if err := scanner.Err(); err != nil {
// Tratamento de erro, se necessário
return ""
}
return strings.TrimRight(builder.String(), "\n")
}