-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.go
150 lines (127 loc) · 3.12 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
package vibot
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
// to pull the href attribute from a token
func getHref(t html.Token) (ok bool, href string) {
for _, a := range t.Attr {
if a.Key == "href" {
href = a.Val
ok = true
}
}
return
}
func getContain(t html.Token) (ok bool, href string) {
for _, div := range t.Attr {
if div.Key == "id" && div.Val == "Definition" {
fmt.Println(div.Val)
fmt.Println(div.Key)
fmt.Println(div.Namespace)
}
}
return
}
func crawl(url string, ch chan string, chFinished chan bool) {
resp, err := http.Get(url)
defer func() {
chFinished <- true
}()
if err != nil {
fmt.Println("ERROR: Failed to crawl \"" + url + "\"")
return
}
b := resp.Body
defer b.Close()
z := html.NewTokenizer(b)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
// End of document
return
case tt == html.StartTagToken:
t := z.Token()
isAnChor := t.Data == "div"
if !isAnChor {
continue
}
ok, url := getContain(t)
if !ok {
continue
}
// Make sure the url begines in http
hasProto := strings.Index(url, "http") == 0
if hasProto {
ch <- url
}
}
}
}
//func (vb *ViBot) GetAllHref(url string) {
//foundUrls := make(map[string]bool)
//// Channels
//chUrls := make(chan string)
//chFinished := make(chan bool)
//// Kick off the crawl process
////for _, url := range seedUrls {
//go crawl(url, chUrls, chFinished)
////}
//// Subcribe to both channel
//for c := 0; c < 1; {
//select {
//case url := <-chUrls:
//foundUrls[url] = true
//case <-chFinished:
//c++
//}
//}
//for url, _ := range foundUrls {
//fmt.Println(" - " + url + " \n")
//}
//close(chUrls)
//}
func (vb *ViBot) GetDefinition(url, searchWord string) {
doc, err := goquery.NewDocument(url + searchWord)
if err != nil {
log.Fatal(err)
}
divDefi := doc.Find("#Definition")
pronoun := divDefi.Find("section[data-src|=\"hc_dict\"] > span.pron").Text()
fmt.Printf("%s: /%s/ - ", searchWord, pronoun)
divDefi.Find("section[data-src|=\"hc_dict\"] > div:not(.etyseg, .runseg) > i").Each(func(i int, s *goquery.Selection) {
kindOfWord := s.Text()
if kindOfWord != "tr" && kindOfWord != "or" {
fmt.Printf("%s ,", strings.ToUpper(s.Text()))
}
})
fmt.Printf("\n")
divDefi.Find("section[data-src|=\"hc_dict\"] > div > div.ds-list").Each(func(i int, s *goquery.Selection) {
fmt.Printf(" %s\n", s.Text())
})
fmt.Printf("Relate: ")
divDefi.Find("section[data-src|=\"hc_dict\"] > div.runseg").Each(func(i int, s *goquery.Selection) {
fmt.Printf("%s (%s) ", s.Find("b").Text(), s.Find("i").Text())
})
fmt.Printf("\n")
//fmt.Println("Idioms: ")
//fmt.Printf("\n")
//fmt.Printf("Verb")
//divDefi.Find("section[data-src|=\"hcVerbTblEn\"]").Each(func(i int, s *goquery.Selection) {
//fmt.Printf("%s - %s\n", s.Find("b > span.hvr").Text(), s.Find("span.hvr").Text())
//})
sound := doc.Find("#content > div > span:nth-child(3)")
if sound != nil {
data, _ := sound.Attr("data-snd")
for i := 0; i < 2; i++ {
vb.PlaySound("http://img2.tfd.com/pron/mp3/" + data + ".mp3")
time.Sleep(3 * time.Second)
}
}
}