Skip to content

Commit f36d53e

Browse files
committed
support .env config file, add socks5 proxy support
1 parent caee558 commit f36d53e

File tree

4 files changed

+148
-77
lines changed

4 files changed

+148
-77
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
[1]: https://travis-ci.org/TimothyYe/ydict.svg?branch=master
1313
[2]: https://travis-ci.org/TimothyYe/ydict
14-
[3]: https://img.shields.io/badge/release-v0.4-brightgreen.svg
14+
[3]: https://img.shields.io/badge/release-v0.5-brightgreen.svg
1515
[4]: https://github.com/TimothyYe/ydict/releases
1616
[5]: https://img.shields.io/dub/l/vibe-d.svg
1717
[6]: LICENSE

main.go

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,18 @@
11
package main
22

33
import (
4-
"fmt"
5-
"log"
64
"os"
75
"strings"
8-
9-
"github.com/PuerkitoBio/goquery"
10-
"github.com/fatih/color"
116
)
127

13-
const ()
14-
15-
func query(word string) {
16-
var url string
17-
isChinese := IsChinese(word)
18-
19-
if isChinese {
20-
url = "http://dict.youdao.com/w/eng/%s"
21-
} else {
22-
url = "http://dict.youdao.com/w/%s"
23-
}
24-
25-
doc, err := goquery.NewDocument(fmt.Sprintf(url, word))
26-
if err != nil {
27-
log.Fatal(err)
28-
os.Exit(1)
29-
}
30-
31-
if isChinese {
32-
// Find the result
33-
fmt.Println()
34-
doc.Find(".trans-container > ul > p > span.contentTitle").Each(func(i int, s *goquery.Selection) {
35-
color.Green(" %s", s.Find(".search-js").Text())
36-
})
37-
} else {
38-
// Find the result
39-
result := doc.Find("div#phrsListTab > div.trans-container > ul").Text()
40-
color.Green(result)
41-
}
42-
43-
// Show examples
44-
sentences := getSentences(doc, isChinese)
45-
if len(sentences) > 0 {
46-
fmt.Println()
47-
for _, sentence := range sentences {
48-
color.Green(" %s", sentence[0])
49-
color.Magenta(" %s", sentence[1])
50-
}
51-
fmt.Println()
52-
}
53-
}
54-
55-
func getSentences(doc *goquery.Document, isChinese bool) [][]string {
56-
result := [][]string{}
57-
doc.Find("#bilingual ul li").Each(func(_ int, s *goquery.Selection) {
58-
r := []string{}
59-
s.Children().Each(func(ii int, ss *goquery.Selection) {
60-
// Ignore source
61-
if ii == 2 {
62-
return
63-
}
64-
var sentence string
65-
ss.Children().Each(func(iii int, sss *goquery.Selection) {
66-
if text := strings.TrimSpace(sss.Text()); text != "" {
67-
addSpace := (ii == 1 && isChinese) || (ii == 0 && !isChinese)
68-
if addSpace && iii != 0 && text != "." {
69-
text = " " + text
70-
}
71-
sentence += text
72-
}
73-
})
74-
r = append(r, sentence)
75-
})
76-
if len(r) == 2 {
77-
result = append(result, r)
78-
}
79-
})
80-
return result
81-
}
8+
var (
9+
proxy string
10+
)
8211

8312
func main() {
13+
//Check & load .env file
14+
loadEnv()
15+
8416
if len(os.Args) == 1 {
8517
displayUsage()
8618
os.Exit(0)

query.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
"strings"
9+
10+
"github.com/PuerkitoBio/goquery"
11+
"github.com/fatih/color"
12+
proxier "golang.org/x/net/proxy"
13+
)
14+
15+
func query(word string) {
16+
var url string
17+
var doc *goquery.Document
18+
isChinese := isChinese(word)
19+
20+
if isChinese {
21+
url = "http://dict.youdao.com/w/eng/%s"
22+
} else {
23+
url = "http://dict.youdao.com/w/%s"
24+
}
25+
26+
//Check proxy
27+
if proxy != "" {
28+
client := &http.Client{}
29+
dialer, err := proxier.SOCKS5("tcp", proxy, nil, proxier.Direct)
30+
31+
if err != nil {
32+
color.Red("Can't connect to the proxy: %s", err)
33+
os.Exit(1)
34+
}
35+
36+
httpTransport := &http.Transport{}
37+
client.Transport = httpTransport
38+
httpTransport.Dial = dialer.Dial
39+
40+
resp, err := client.Get(fmt.Sprintf(url, word))
41+
42+
if err != nil {
43+
color.Red("Query failed with err: %s", err.Error())
44+
os.Exit(1)
45+
}
46+
47+
doc, _ = goquery.NewDocumentFromResponse(resp)
48+
49+
} else {
50+
var err error
51+
doc, err = goquery.NewDocument(fmt.Sprintf(url, word))
52+
if err != nil {
53+
log.Fatal(err)
54+
os.Exit(1)
55+
}
56+
}
57+
58+
if isChinese {
59+
// Find the result
60+
fmt.Println()
61+
doc.Find(".trans-container > ul > p > span.contentTitle").Each(func(i int, s *goquery.Selection) {
62+
color.Green(" %s", s.Find(".search-js").Text())
63+
})
64+
} else {
65+
// Find the result
66+
result := doc.Find("div#phrsListTab > div.trans-container > ul").Text()
67+
color.Green(result)
68+
}
69+
70+
// Show examples
71+
sentences := getSentences(doc, isChinese)
72+
if len(sentences) > 0 {
73+
fmt.Println()
74+
for _, sentence := range sentences {
75+
color.Green(" %s", sentence[0])
76+
color.Magenta(" %s", sentence[1])
77+
}
78+
fmt.Println()
79+
}
80+
}
81+
82+
func getSentences(doc *goquery.Document, isChinese bool) [][]string {
83+
result := [][]string{}
84+
doc.Find("#bilingual ul li").Each(func(_ int, s *goquery.Selection) {
85+
r := []string{}
86+
s.Children().Each(func(ii int, ss *goquery.Selection) {
87+
// Ignore source
88+
if ii == 2 {
89+
return
90+
}
91+
var sentence string
92+
ss.Children().Each(func(iii int, sss *goquery.Selection) {
93+
if text := strings.TrimSpace(sss.Text()); text != "" {
94+
addSpace := (ii == 1 && isChinese) || (ii == 0 && !isChinese)
95+
if addSpace && iii != 0 && text != "." {
96+
text = " " + text
97+
}
98+
sentence += text
99+
}
100+
})
101+
r = append(r, sentence)
102+
})
103+
if len(r) == 2 {
104+
result = append(result, r)
105+
}
106+
})
107+
return result
108+
}

utils.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package main
22

33
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
48
"unicode"
59

610
"github.com/fatih/color"
11+
"github.com/joho/godotenv"
712
)
813

914
const (
@@ -15,7 +20,7 @@ const (
1520
██║ ██████╔╝██║╚██████╗ ██║
1621
╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝
1722
18-
YDict V0.4
23+
YDict V0.5
1924
https://github.com/TimothyYe/ydict
2025
2126
`
@@ -26,11 +31,37 @@ func displayUsage() {
2631
color.Cyan("Usage: ydict <word to query>")
2732
}
2833

29-
func IsChinese(str string) bool {
34+
func isChinese(str string) bool {
3035
for _, r := range str {
3136
if unicode.Is(unicode.Scripts["Han"], r) {
3237
return true
3338
}
3439
}
3540
return false
3641
}
42+
43+
func getExecutePath() string {
44+
ex, err := os.Executable()
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
return filepath.Dir(ex)
50+
}
51+
52+
func loadEnv() {
53+
exPath := getExecutePath()
54+
55+
// if .env file doesn't exist, just return
56+
if _, err := os.Stat(fmt.Sprintf("%s/.env", exPath)); os.IsNotExist(err) {
57+
return
58+
}
59+
60+
err := godotenv.Load()
61+
if err != nil {
62+
log.Fatal("Error loading .env file")
63+
return
64+
}
65+
66+
proxy = os.Getenv("SOCKS5")
67+
}

0 commit comments

Comments
 (0)