Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alpkeskin
GitHub Repository: alpkeskin/mosint
Path: blob/master/v3/pkg/scrape/googlesearch/google_search.go
689 views
1
/*
2
Copyright © 2023 github.com/alpkeskin
3
*/
4
package googlesearch
5
6
import (
7
"fmt"
8
"regexp"
9
"strings"
10
11
"github.com/alpkeskin/mosint/v3/internal/spinner"
12
"github.com/fatih/color"
13
"github.com/gocolly/colly/v2"
14
)
15
16
type GoogleSearch struct {
17
Response []string
18
}
19
20
func New() *GoogleSearch {
21
return &GoogleSearch{}
22
}
23
24
func (g *GoogleSearch) Search(email string) {
25
spinner := spinner.New("Google Searching")
26
spinner.Start()
27
28
query := fmt.Sprintf("intext:'%s'", email)
29
url := fmt.Sprintf("https://www.google.com/search?q=%s", query)
30
31
pattern := `https?://[^\s"']+`
32
re := regexp.MustCompile(pattern)
33
removedPrefix := "/url?q="
34
removedParams := "&sa="
35
36
c := colly.NewCollector()
37
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
38
link := e.Attr("href")
39
if containsGoogle(link) {
40
return
41
}
42
43
var response []string
44
if re.MatchString(link) {
45
link := strings.TrimPrefix(link, removedPrefix)
46
link = strings.Split(link, removedParams)[0]
47
response = append(response, link)
48
}
49
50
g.Response = append(g.Response, response...)
51
})
52
53
err := c.Visit(url)
54
55
if err != nil {
56
spinner.StopFail()
57
spinner.SetMessage(err.Error())
58
return
59
}
60
61
spinner.Stop()
62
}
63
64
func (g *GoogleSearch) Print() {
65
fmt.Println("[*] Google Search Results")
66
if len(g.Response) == 0 {
67
fmt.Println(color.RedString("[!]"), "No results found")
68
return
69
}
70
71
for _, link := range g.Response {
72
fmt.Println(color.GreenString("[+]"), link)
73
}
74
}
75
76
func containsGoogle(text string) bool {
77
return regexp.MustCompile(`google\.com`).MatchString(text)
78
}
79
80