This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
google.go
99 lines (85 loc) · 2.93 KB
/
google.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
package google
import (
"fmt"
"github.com/kamushadenes/chloe/config"
"github.com/kamushadenes/chloe/errors"
"github.com/kamushadenes/chloe/langchain/actions/scrape"
"github.com/kamushadenes/chloe/logging"
"github.com/kamushadenes/chloe/structs/action_structs"
"github.com/kamushadenes/chloe/structs/response_object_structs"
"github.com/kamushadenes/chloe/utils"
googlesearch "github.com/rocketlaunchr/google-search"
customsearch "google.golang.org/api/customsearch/v1"
"google.golang.org/api/option"
)
func (a *GoogleAction) GetNotification() string {
return fmt.Sprintf("🔍 Searching Google: **%s**", a.MustGetParam("query"))
}
func (a *GoogleAction) Execute(request *action_structs.ActionRequest) ([]*response_object_structs.ResponseObject, error) {
logger := logging.FromContext(request.Context).With().Str("action", a.GetName()).Logger()
var results []googleResult
var objs []*response_object_structs.ResponseObject
fallback := true
if config.React.GoogleCustomSearchID != "" && config.React.GoogleCustomSearchAPIKey != "" {
logger.Info().Msg("using custom search api")
svc, err := customsearch.NewService(request.Context, option.WithAPIKey(config.React.GoogleCustomSearchAPIKey))
if err != nil {
logger.Warn().Err(err).Msg("failed to create custom search service, falling back to search scraping")
} else {
s := svc.Cse.List()
s.Q(a.MustGetParam("query"))
s.Num(int64(config.React.GoogleMaxResults))
s.Cx(config.React.GoogleCustomSearchID)
search, err := s.Do()
if err != nil {
logger.Warn().Err(err).Msg("failed to perform api search, falling back to search scraping")
} else {
for k := range search.Items {
results = append(results, googleResult{
URL: search.Items[k].Link,
Title: search.Items[k].Title,
Description: search.Items[k].Snippet,
})
}
if len(results) == 0 {
logger.Warn().Msg("no results from api, falling back to search scraping")
} else {
fallback = false
}
}
}
}
if fallback {
res, err := googlesearch.Search(request.GetContext(), a.MustGetParam("query"), googlesearch.SearchOptions{Limit: config.React.GoogleMaxResults})
if err != nil {
return nil, errors.Wrap(errors.ErrActionFailed, err)
}
for k := range res {
results = append(results, googleResult{
URL: res[k].URL,
Title: res[k].Title,
Description: res[k].Description,
})
}
}
if !utils.Testing() {
for k := range results {
r := results[k]
na := scrape.NewScrapeAction()
na.SetParam("url", r.URL)
if err := na.RunPreActions(request); err != nil {
continue
}
request.Message.NotifyAction(na.GetNotification())
aobjs, err := na.Execute(request)
if err != nil && !errors.Is(err, errors.ErrProceed) {
continue
}
objs = append(objs, aobjs...)
}
}
return objs, errors.ErrProceed
}
func (a *GoogleAction) RunPostActions(request *action_structs.ActionRequest) error {
return errors.ErrProceed
}