-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
213 lines (190 loc) · 4.48 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"unicode/utf8"
"regexp"
)
// https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html
// user: 15803
// pass: .....
const (
appID = "4c67797e6790510c"
appKey = "OJDxTKImnx15Ld6bOo0Zy0e1rEZ4nYqT"
baseURL = "https://openapi.youdao.com/api"
salt = "1x2r6y68"
from = "auto"
signType = "v3"
)
// 字段名起始字母必须是大写,否则外部访问不了,即会解析不到值
type ydTransRst struct {
Basic ydBasic
Status int
ErrorCode string
Translation []string
}
type ydBasic struct {
Phonetic string
UkPhonetic string `json:"uk-phonetic"`
UsPhonetic string `json:"us-phonetic"`
Explains []string
Wfs []ydWfOut
}
type ydWfOut struct {
Wf ydWf
}
type ydWf struct {
Name string
Value string
}
func main() {
// Check translate word
if len(os.Args) != 2 {
printErr("Params Error", errors.New("Please input the valid cound of translate word"))
return
}
TransByYd(os.Args[1])
}
//export TransByYd
func TransByYd(q string) string {
if strings.Compare("", q) == 0 {
errMsg := "Please input a word for translate"
printErr("Params Error", errors.New(errMsg))
return errMsg
}
// Generate request params
toStr := getTo(q)
curTime := strconv.FormatInt(time.Now().Unix(), 10)
sign := generateSign(q, curTime)
// Create http client
client := &http.Client{}
req, err := http.NewRequest("GET", baseURL, nil)
if err != nil {
panic(err)
}
// Add query params
query := req.URL.Query()
query.Add("appKey", appID)
query.Add("signType", signType)
query.Add("from", from)
query.Add("to", toStr)
query.Add("salt", salt)
query.Add("curtime", curTime)
query.Add("sign", sign)
query.Add("q", q)
req.URL.RawQuery = query.Encode()
// fmt.Printf("reqUrl: %s\n\n", req.URL);
// Request
resp, err := client.Do(req)
if err != nil {
printErr("Request Error", err)
return err.Error()
}
// Read response
// defer 总是在方法最后执行,即使它放在代码中间位置
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
printErr("Response Read Error", err)
return err.Error()
}
// Parse
bodyStr := string(body)
// fmt.Printf("rspBody: %s\n", bodyStr)
var transRst ydTransRst
unmarshalErr := json.Unmarshal(body, &transRst)
if unmarshalErr != nil {
printErr("Response Unmarshal Error", unmarshalErr)
return unmarshalErr.Error()
}
status := transRst.Status
if status != 0 {
printErr("Response Status Error", errors.New(bodyStr))
return bodyStr
}
errorCode := transRst.ErrorCode
if strings.Compare("", errorCode) != 0 && strings.Compare("0", errorCode) != 0 {
printErr("Response Code Error", errors.New(bodyStr))
return bodyStr
}
// Print query word
fmt.Printf("\n %s \n\n", q)
// Phonetic
// printPhonetic(q, transRst.Basic)
// Ws
// wfs := transRst.Basic.Wfs
// if len(wfs) > 0 {
// for _, wfOut := range wfs {
// name := wfOut.Wf.Name
// value := wfOut.Wf.Value
// nameLen := utf8.RuneCountInString(name)
// w := 0
// if nameLen == 2 {
// w = 10
// } else if nameLen == 3 {
// w = 9
// } else if nameLen == 4 {
// w = 8
// } else if nameLen == 5 {
// w = 7
// } else if nameLen == 6 {
// w = 6
// }
// fmt.Printf(" %-"+strconv.Itoa(w)+"s : %s\n", name, value)
// }
// fmt.Println()
// }
// Explains
for _, explain := range transRst.Translation {
fmt.Println(" - " + explain)
}
fmt.Println()
return bodyStr
}
func printPhonetic(q string, basic ydBasic) {
phonetic := basic.Phonetic
ukPhonetic := basic.UkPhonetic
usPhonetic := basic.UsPhonetic
if strings.Compare("", phonetic) != 0 {
fmt.Printf(" 音 [ %s ]", phonetic)
}
if strings.Compare("", ukPhonetic) != 0 {
fmt.Printf(" 英 [ %s ]", ukPhonetic)
}
if strings.Compare("", usPhonetic) != 0 {
fmt.Printf(" 美 [ %s ]", usPhonetic)
}
fmt.Printf("\n\n")
}
func printErr(errType string, err error) {
fmt.Printf("\n%s: \n\n %v\n\n", errType, err)
}
func getTo(q string) string {
if regexp.MustCompile(`^[a-z A-Z]+$`).MatchString(q) {
return "zh-CHS"
}
return "en"
}
func generateSign(q string, curTime string) string {
input := ""
qLen := utf8.RuneCountInString(q)
if qLen < 20 {
input = q
} else {
input = q[:10] + strconv.Itoa(qLen) + q[20:qLen]
}
oriSign := appID + input + salt + curTime + appKey
h := sha256.New()
h.Write([]byte(oriSign))
x := hex.EncodeToString(h.Sum(nil))
return x
}