-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (83 loc) · 2.43 KB
/
main.go
File metadata and controls
100 lines (83 loc) · 2.43 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"github.com/fatih/color"
"github.com/gocolly/colly"
"github.com/mburakerman/cambridge-cli/languages"
)
// flags
var language = flag.String("language", string(languages.English), "select language")
var showAllMeanings = flag.Bool("showAllMeanings", false, "display all of the meanings of the word")
func main() {
flag.Parse()
if !languages.CheckSupportedLanguage(*language) {
return
}
fmt.Print("Enter word: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
word := scanner.Text()
c := colly.NewCollector(
colly.AllowedDomains("dictionary.cambridge.org"),
)
c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
var wordLevel string
var meanings []string
var exampleSentence string
// get english level info
c.OnHTML(".epp-xref.dxref", func(e *colly.HTMLElement) {
if e.Index == 0 && !*showAllMeanings {
wordLevel = e.Text
wordLevel = strings.TrimSpace(wordLevel)
}
})
// get meanings
var meaningSelector = ".def.ddef_d.db"
if *language != string(languages.English) {
meaningSelector = ".trans.dtrans"
}
c.OnHTML(meaningSelector, func(e *colly.HTMLElement) {
if *showAllMeanings {
results := strings.TrimSpace(e.Text)
results = strings.Replace(results, ":", "", -1)
meanings = append(meanings, results)
} else if e.Index == 0 {
result := strings.TrimSpace(e.Text)
result = strings.Replace(result, ":", "", -1)
meanings = append(meanings, result)
}
})
// get example sentence
c.OnHTML(".eg.deg", func(e *colly.HTMLElement) {
if e.Index == 0 {
result := e.Text
exampleSentence = strings.TrimSpace(result)
}
})
// print when scrapping done
yellowBackground := color.New(color.FgBlack, color.BgYellow)
c.OnScraped(func(r *colly.Response) {
if len(wordLevel) > 0 {
fmt.Print("\n📈 ")
yellowBackground.Print(" " + wordLevel + " ")
fmt.Print("\n ")
}
for i := 0; i < len(meanings); i++ {
color.Green("\n✅ " + meanings[i])
}
if len(exampleSentence) > 0 {
fmt.Println("\n📝 " + color.CyanString(exampleSentence) + "\n")
} else {
fmt.Println("")
}
})
// visit url to scrap
if *language == string(languages.English) {
c.Visit(fmt.Sprintf("https://dictionary.cambridge.org/dictionary/english/%v", word))
}
c.Visit(fmt.Sprintf("https://dictionary.cambridge.org/dictionary/english-%v/%v", *language, word))
}