forked from ghetzel/go-webfriend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-search.go
80 lines (66 loc) · 2.14 KB
/
google-search.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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/PerformLine/go-webfriend"
browser "github.com/PerformLine/go-webfriend/browser"
"github.com/PerformLine/go-webfriend/commands/core"
)
const (
googleUrl string = "https://google.com"
)
// PROTIP: Export the environment variable LOGLEVEL=debug before executing your program to see
// every single little thing going on behind the scenes while Webfriend is executing.
// If it's too much, "info", "warning", and "error" are also valid values.
func main() {
var searchTerm string
if len(os.Args) > 1 {
searchTerm = strings.Join(os.Args[1:], ` `)
} else {
log.Fatalf("Must specify a search term")
}
chrome := browser.NewBrowser()
// uncomment this to actually see the browser doing stuff
// chrome.Headless = false
if err := chrome.Launch(); err == nil {
environment := webfriend.NewEnvironment(chrome)
// call the high-level "go" command, which takes care of a BUNCH of
// stuff for you; namely it blocks until the page loads or the
// timeout is reached.
if _, err := environment.Core.Go(googleUrl, &core.GoArgs{
Timeout: 10 * time.Second,
}); err != nil {
log.Fatalf("failed to load %v: %v", googleUrl, err)
}
// type in the search term into the #lst-ib field (what the search input is called)
// on google.com
//
// The "Enter" parameter will send an Enter/Return keypress (keycode 13, 0x0D).
// This will perform the search.
if _, err := environment.Core.Field(`#lst-ib`, &core.FieldArgs{
Value: searchTerm,
Enter: true,
}); err != nil {
log.Fatalf("failed to perform search: %v", err)
}
// find all search result links, spend up to 5 seconds looking for them.
if resultLinks, err := environment.Core.Select(`h3.r a`, &core.SelectArgs{
MinMatches: 0,
}); err == nil {
if len(resultLinks) > 0 {
// loop through all search result links and print their name and URL
for i, a := range resultLinks {
fmt.Printf("[%02d] %s\n", i, a.Text)
fmt.Printf(" %s\n", a.Attributes[`href`])
}
} else {
fmt.Printf("No results found for %q\n", searchTerm)
}
} else {
log.Fatal(err)
}
}
}