-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
82 lines (76 loc) · 1.69 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
// +build ignore
package main
import (
"errors"
"log"
"net/url"
"strings"
"github.com/anaskhan96/soup"
"golang.org/x/net/html"
)
type result struct {
url, desc string
}
func google(s string) ([]result, error) {
var resp string
var err error
defer func() {
e := recover()
if e != nil {
//log.Println("contents: "+ resp)
panic(e)
}
}()
if s == "panictest" {
panic(errors.New("fof"))
}
resp, err = soup.Get("https://www.google.com/search?q=" + url.QueryEscape(s))
if err != nil {
return []result{}, errors.New("failed to reach google")
}
var results = []result{}
root := soup.HTMLParse(resp)
for _, x := range root.FindAll("div", "class", "g") {
if len(results) > 3 {
break
}
//var buf bytes.Buffer
//html.Render(&buf, x.Pointer)
//log.Println(buf.String())
if x.Attrs()["class"] != "g" {
continue
}
linkMom := x.Find("h3", "class", "r")
if linkMom.Error != nil {
log.Println(linkMom.Error)
continue
}
linkTarget := linkMom.Find("a")
descMom := x.Find("span", "class", "st")
if descMom.Error != nil {
log.Println(descMom.Error)
continue
}
descTarget := descMom.Pointer
var f func(*html.Node)
descList := []string{}
f = func(n *html.Node) {
if n != nil && n.Type == html.TextNode {
if n.Data != "\n" {
descList = append(descList, n.Data)
}
}
if n != nil {
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
}
f(descTarget)
if strings.HasPrefix(linkTarget.Attrs()["href"], "/url?") {
toTrim, _ := url.Parse("https://www.google.com" + linkTarget.Attrs()["href"])
results = append(results, result{toTrim.Query().Get("q"), strings.Join(descList, " ")})
}
}
return results, nil
}