-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.go
145 lines (128 loc) · 3.28 KB
/
scrape.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
package scrape
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
const (
mwDefinitionEndpointFormat = "http://www.merriam-webster.com/dictionary/%s"
mwPopularityEndpointFormat = "http://stats.merriam-webster.com/pop-score-redesign.php?word=%s&t=%d&id=popularity-score"
)
var (
definitionNotFound = errors.New("definition not found")
bufStdout = bufio.NewWriter(os.Stdout)
outMutex sync.Mutex
)
func getWordFromBody(body string) (string, error) {
lines := strings.Split(string(body), "\n")
for _, l := range lines {
if strings.Contains(l, "<title>") {
word := strings.Split(l, "<title>")[1]
word = strings.Split(word, "|")[0]
word = strings.TrimSpace(strings.ToLower(word))
return word, nil
}
}
return "", definitionNotFound
}
func getPopularityFromBody(body string) (score int, label string, err error) {
lines := strings.Split(string(body), "\n")
for _, l := range lines {
if strings.Contains(l, "label:") {
label = strings.TrimSuffix(strings.SplitAfter(l, ":")[1], ",")
}
if strings.Contains(l, "score:") {
score, err = strconv.Atoi(strings.TrimSuffix(strings.SplitAfter(l, " ")[1], ","))
if err != nil {
return
}
}
}
return
}
func lookupWord(word string) (statusCode int, mappedWord string, score int, label string, err error) {
escWord := url.QueryEscape(word)
defEp := fmt.Sprintf(mwDefinitionEndpointFormat, escWord)
// get the definition
defResp, er := http.Get(defEp)
if er != nil {
err = er
return
}
defer defResp.Body.Close()
statusCode = defResp.StatusCode
if statusCode != 200 {
return
}
defBody, _ := ioutil.ReadAll(defResp.Body)
mappedWord, er = getWordFromBody(string(defBody))
// word exists, so get its popularity
popEp := fmt.Sprintf(mwPopularityEndpointFormat, mappedWord, time.Now().Nanosecond()/1000)
popResp, er := http.Get(popEp)
if er != nil {
err = er
return
}
defer popResp.Body.Close()
popBody, _ := ioutil.ReadAll(popResp.Body)
score, label, err = getPopularityFromBody(string(popBody))
return
}
func exitError(message string) {
fmt.Fprintf(os.Stderr, message)
os.Exit(1)
}
func lookupLoop(outputPrefix string, words <-chan string) {
for word := range words {
var response string
st, wrd, scr, lbl, er := lookupWord(word)
if er != nil || st != 200 {
response = fmt.Sprintf("%s\t%d\t%s\t%s\terror: %v\n", outputPrefix, st, word, wrd, er)
} else {
response = fmt.Sprintf("%s\t%d\t%s\t%s\t%d\t%s\n", outputPrefix, st, word, wrd, scr, lbl)
}
outMutex.Lock()
bufStdout.WriteString(response)
outMutex.Unlock()
}
}
func Main() {
runtime.GOMAXPROCS(30)
inputWords := make(chan string, 1000)
var wg sync.WaitGroup
// multiple lookup goroutines
for nl := 1; nl <= 25; nl++ {
wg.Add(1)
prefixString := fmt.Sprintf("loop%d", nl)
go func() {
defer wg.Done()
lookupLoop(prefixString, inputWords)
}()
}
// main goroutine pushes all the words to the input channel
byts, _ := ioutil.ReadAll(os.Stdin)
buffer := bytes.NewBuffer(byts)
word, err := buffer.ReadString('\n')
for err != io.EOF {
word = strings.Trim(word, " \n\t")
if err != nil {
exitError(err.Error())
}
inputWords <- word
word, err = buffer.ReadString('\n')
}
close(inputWords)
wg.Wait()
bufStdout.Flush()
}