Skip to content

Commit

Permalink
fix youdao, bing, tencent translators
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Oct 28, 2021
1 parent 6e477af commit 209e5bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
10 changes: 7 additions & 3 deletions baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ func (b *baiduTranslator) translate(ctx context.Context, req Request) (resp *Res
param := url.Values{"query": {req.Text}}
detectUrl := "https://fanyi.baidu.com/langdetect"
body := strings.NewReader(param.Encode())
r, data, err := sendRequest(ctx, http.MethodPost, detectUrl, body, func(req *http.Request) error {
_, data, err := sendRequest(ctx, http.MethodPost, detectUrl, body, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
return nil
})
if err != nil {
resp.Err = err
return
}

jr := gjson.Parse(string(data))
if errorCode := jr.Get("error").Int(); errorCode != 0 {
Expand All @@ -56,13 +60,13 @@ func (b *baiduTranslator) translate(ctx context.Context, req Request) (resp *Res
}
from := jr.Get("lan").String()

r, data, err = sendRequest(ctx, http.MethodGet, "https://fanyi.baidu.com", nil, func(req *http.Request) error {
_, data, err = sendRequest(ctx, http.MethodGet, "https://fanyi.baidu.com", nil, func(req *http.Request) error {
addCookies(req, cookies)
addCookies(req, fanyiCookies)
return nil
})
if err != nil {
err = fmt.Errorf("SendRequest error: %v", err)
resp.Err = fmt.Errorf("SendRequest error: %v", err)
return
}

Expand Down
22 changes: 19 additions & 3 deletions bing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func BingTranslate(ctx context.Context, req Request) *Response {
func (b *bingTranslator) translate(ctx context.Context, req Request) (resp *Response) {
resp = newResp(b)

_, data, err := sendRequest(ctx, http.MethodGet, "https://cn.bing.com/translator", nil, nil)
r, data, err := sendRequest(ctx, http.MethodGet, "https://www.bing.com/translator", nil, nil)
if err != nil {
resp.Err = fmt.Errorf("readResp error: %v", err)
return
Expand All @@ -37,6 +37,12 @@ func (b *bingTranslator) translate(ctx context.Context, req Request) (resp *Resp
return
}

igValue, err := b.getIGValue(string(data))
if err != nil {
resp.Err = err
return
}

req.ToLang = b.convertLanguage(req.ToLang)
param := url.Values{
"fromLang": {"auto-detect"},
Expand All @@ -46,11 +52,12 @@ func (b *bingTranslator) translate(ctx context.Context, req Request) (resp *Resp
"token": {token},
}

urlStr := "https://cn.bing.com/ttranslatev3"
urlStr := fmt.Sprintf("https://www.bing.com/ttranslatev3?isVertical=1&IG=%s&IID=translator.5023.1", igValue)
body := strings.NewReader(param.Encode())
_, data, err = sendRequest(ctx, "POST", urlStr, body, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", UserAgent)
addCookies(req, r.Cookies())
return nil
})
if err != nil {
Expand Down Expand Up @@ -78,10 +85,19 @@ func (b *bingTranslator) convertLanguage(language string) string {

func (*bingTranslator) getTimestampAndToken(dataStr string) (string, string, error) {
// dataStr := `var params_RichTranslateHelper = [1623165977131,"Q5b9PD_0XEdOagXBcPVtlnB6ZML4958D",3600000,true];`
result := regexp.MustCompile(`var params_RichTranslateHelper = \[(?P<result>[\s\S]+),"(?P<result1>[\s\S]+)",3600000,true\]`).FindStringSubmatch(dataStr)
result := regexp.MustCompile(`var params_RichTranslateHelper = \[(?P<result>[\s\S]+),"(?P<result1>[\s\S]+)",3600000,true,false\]`).FindStringSubmatch(dataStr)
if len(result) != 3 {
return "", "", fmt.Errorf("cannot get timestamp and token")
}

return result[1], result[2], nil
}

func (*bingTranslator) getIGValue(dataStr string) (string, error) {
// IG:"678CAA0BB18740F3A5D3EF29A6D434B0"
igResult := regexp.MustCompile(`IG:"(?P<token>\S+)",`).FindStringSubmatch(dataStr)
if len(igResult) != 2 {
return "", fmt.Errorf("cannot get ig value")
}
return igResult[1], nil
}
5 changes: 3 additions & 2 deletions tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TencentTranslate(ctx context.Context, req Request) *Response {
func (t *tencentTranslator) translate(ctx context.Context, req Request) (resp *Response) {
resp = newResp(t)

_, data, err := sendRequest(ctx, "POST", "https://fanyi.qq.com/api/reauth1232f", nil, nil)
_, data, err := sendRequest(ctx, "POST", "https://fanyi.qq.com/api/reauth12f", nil, nil)
if err != nil {
resp.Err = err
return
Expand All @@ -52,7 +52,8 @@ func (t *tencentTranslator) translate(ctx context.Context, req Request) (resp *R
urlStr := "https://fanyi.qq.com/api/translate"
body := strings.NewReader(param.Encode())
_, data, err = sendRequest(ctx, "POST", urlStr, body, func(req *http.Request) error {
req.Header.Set("Origin", "http://fanyi.qq.com")
req.Header.Set("Origin", "https://fanyi.qq.com")
req.Header.Set("uc", "qEcP9NeHbGbSxJMTi6uKB7KximMaJOZIM0yohxonQs8=")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
return nil
Expand Down
2 changes: 1 addition & 1 deletion youdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (y *youdaoTranslator) translate(ctx context.Context, req Request) (resp *Re
// o = u.md5(S + n + r + D);
salt := fmt.Sprintf("%d", int(time.Now().UnixNano()/1000000)+rand.Intn(10))
h := md5.New()
h.Write([]byte("fanyideskweb" + req.Text + salt + `97_3(jkMYg@T[KZQmqjTK`))
h.Write([]byte("fanyideskweb" + req.Text + salt + `Y2FYu%TNSbMCxc3t2u^XT`))
sign := hex.EncodeToString(h.Sum(nil))

req.ToLang = y.convertLanguage(req.ToLang)
Expand Down

0 comments on commit 209e5bd

Please sign in to comment.