From 209e5bdecf347b06bbade9b7c87bf648c2e1b45b Mon Sep 17 00:00:00 2001 From: Wendell Sun Date: Thu, 28 Oct 2021 18:39:42 +0800 Subject: [PATCH] fix youdao, bing, tencent translators --- baidu.go | 10 +++++++--- bing.go | 22 +++++++++++++++++++--- tencent.go | 5 +++-- youdao.go | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/baidu.go b/baidu.go index cd02798..2608d10 100644 --- a/baidu.go +++ b/baidu.go @@ -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 { @@ -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 } diff --git a/bing.go b/bing.go index 64d3e97..811864d 100644 --- a/bing.go +++ b/bing.go @@ -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 @@ -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"}, @@ -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 { @@ -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[\s\S]+),"(?P[\s\S]+)",3600000,true\]`).FindStringSubmatch(dataStr) + result := regexp.MustCompile(`var params_RichTranslateHelper = \[(?P[\s\S]+),"(?P[\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\S+)",`).FindStringSubmatch(dataStr) + if len(igResult) != 2 { + return "", fmt.Errorf("cannot get ig value") + } + return igResult[1], nil +} diff --git a/tencent.go b/tencent.go index 8b5768a..5b7bff2 100644 --- a/tencent.go +++ b/tencent.go @@ -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 @@ -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 diff --git a/youdao.go b/youdao.go index 3175545..622157a 100644 --- a/youdao.go +++ b/youdao.go @@ -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)