This repository was archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerriam-webster.go
117 lines (110 loc) · 3.19 KB
/
merriam-webster.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
package service4mydictionary
import (
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/zzc-tongji/vocabulary4mydictionary"
)
const merriamWebsterName = "Merriam Webster"
// MerriamWebsterStruct : Merriam Webster struct
type MerriamWebsterStruct struct {
cache CacheStruct
}
// GetServiceName : get service name
func (service *MerriamWebsterStruct) GetServiceName() (value string) {
value = merriamWebsterName
return
}
// GetCache : get cache
func (service *MerriamWebsterStruct) GetCache() (cache *CacheStruct) {
cache = &service.cache
return
}
// Query : query vocabulary
func (service *MerriamWebsterStruct) Query(vocabularyAsk vocabulary4mydictionary.VocabularyAskStruct) (vocabularyAnswer vocabulary4mydictionary.VocabularyAnswerStruct) {
var (
err error
queryString string
item CacheItemStruct
document *goquery.Document
selection1 *goquery.Selection
indexList []int
selectionList []*goquery.Selection
selection2 *goquery.Selection
selection3 *goquery.Selection
)
// set
vocabularyAnswer.SourceName = merriamWebsterName
vocabularyAnswer.Location.TableType = vocabulary4mydictionary.Online
vocabularyAnswer.Location.TableIndex = -1
vocabularyAnswer.Location.ItemIndex = -1
// query cache
queryString = url.QueryEscape(vocabularyAsk.Word)
item, err = service.cache.Query(queryString)
if err == nil {
goto SET
}
// query online
// no phrase is contained by Merriam Webster
if strings.Contains(queryString, "%20") {
item.Status = "null: MW01"
goto ADD
}
// get page
document, err = goquery.NewDocument("https://www.merriam-webster.com/dictionary/" + queryString)
if err != nil {
item.Status = err.Error()
goto ADD
}
selection1 = document.Find(".inner-box-wrapper").Find(".card-box-title")
if selection1.Nodes == nil {
item.Status = "null: MW02"
goto ADD
}
// locate
for i := 0; i < selection1.Size(); i++ {
if strings.Contains(selection1.Eq(i).Text(), "English Language Learners") {
indexList = append(indexList, i)
}
}
for i := 0; i < len(indexList); i++ {
selectionList = append(selectionList, selection1.Eq(indexList[i]).Parent().Parent().Parent())
}
if selectionList == nil {
item.Status = "null: MW03"
goto ADD
}
// get word
selection2 = selectionList[0].Find(".word-and-pronunciation").Find("h2")
if selection2.Nodes == nil {
item.Status = "null: MW04"
goto ADD
}
item.Word = strings.TrimSpace(selection2.Text())
// get define
for i := 0; i < len(selectionList); i++ {
item.Definition = append(item.Definition, selectionList[i].Find(".main-attr").Text())
selection3 = selectionList[i].Find(".definition-inner-item")
for j := 0; j < selection3.Size(); j++ {
item.Definition = append(item.Definition, strings.TrimSpace(selection3.Eq(j).Text()))
}
}
// mark difference
if strings.Compare(queryString, url.QueryEscape(item.Word)) != 0 {
item.Status = "different"
} else {
item.Status = vocabulary4mydictionary.Basic
}
ADD:
// add to cache
item.QueryString = queryString
item.CreationTime = time.Now().Unix()
service.cache.Add(item)
SET:
// set
vocabularyAnswer.Word = item.Word
vocabularyAnswer.Definition = item.Definition
vocabularyAnswer.Status = item.Status
return
}