Skip to content

Commit

Permalink
Fix bing, sogou, qq translators
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Jun 8, 2021
1 parent 77b24f6 commit 3dbd667
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 51 deletions.
30 changes: 28 additions & 2 deletions bing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"

"github.com/tidwall/gjson"
Expand All @@ -24,17 +25,32 @@ 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)
if err != nil {
resp.Err = fmt.Errorf("readResp error: %v", err)
return
}
timestamp, token, err := b.getTimestampAndToken(string(data))
if err != nil {
resp.Err = err
return
}

req.ToLang = b.convertLanguage(req.ToLang)
param := url.Values{
"fromLang": {"auto-detect"},
"to": {req.ToLang},
"text": {req.Text},
"key": {timestamp},
"token": {token},
}

urlStr := "https://cn.bing.com/ttranslatev3"
body := strings.NewReader(param.Encode())
_, data, err := sendRequest(ctx, "POST", urlStr, body, func(req *http.Request) error {
_, 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)
return nil
})
if err != nil {
Expand All @@ -54,8 +70,18 @@ func (b *bingTranslator) convertLanguage(language string) string {
l := language
switch language {
case Chinese:
l = "zh-CHS"
l = "zh-Hans"
}

return l
}

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)
if len(result) != 3 {
return "", "", fmt.Errorf("cannot get timestamp and token")
}

return result[1], result[2], nil
}
2 changes: 2 additions & 0 deletions fy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
French = "fr"
Korean = "ko"
Spanish = "es"

UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
)

var translators = []translator{
Expand Down
52 changes: 30 additions & 22 deletions sogou.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fy

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/robertkrimen/otto"
"github.com/tidwall/gjson"
Expand All @@ -25,6 +25,17 @@ func SogouTranslate(ctx context.Context, req Request) *Response {

func (s *sogouTranslator) translate(ctx context.Context, req Request) (resp *Response) {
resp = newResp(s)

r, _, err := sendRequest(ctx, http.MethodGet, "https://fanyi.sogou.com/", nil, func(r *http.Request) error {
r.Header.Set("User-Agent", UserAgent)
return nil
})
if err != nil {
resp.Err = fmt.Errorf("notReadResp error: %v", err)
return
}
cookies := r.Cookies()

req.ToLang = s.convertLanguage(req.ToLang)

sign, err := s.calSign("auto", req.ToLang, req.Text)
Expand All @@ -33,27 +44,24 @@ func (s *sogouTranslator) translate(ctx context.Context, req Request) (resp *Res
return
}

param := url.Values{
"from": {"auto"},
"to": {req.ToLang},
"text": {req.Text},
"client": {"pc"},
"fr": {"browser_pc"},
"pid": {"sogou-dict-vr"},
"dict": {"true"},
"word_group": {"true"},
"second_query": {"true"},
"uuid": {"66623e19-cb42-4dda-840d-891319162299"},
"needQc": {"1"},
"s": {sign},
param := map[string]interface{}{
"client": "pc",
"exchange": false,
"fr": "browser_pc",
"from": "auto",
"needQc": 1,
"s": sign,
"text": req.Text,
"to": req.ToLang,
}
urlStr := "https://fanyi.sogou.com/reventondc/translateV2"
body := strings.NewReader(param.Encode())
urlStr := "https://fanyi.sogou.com/api/transpc/text/result"

reqData, _ := json.Marshal(param)
body := bytes.NewReader(reqData)
_, data, err := sendRequest(ctx, http.MethodPost, urlStr, body, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
req.Header.Set("Accept", "application/json")
req.Header.Set("Referer", "https://fanyi.sogou.com/")
addCookies(req, cookies)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", UserAgent)
return nil
})
if err != nil {
Expand Down Expand Up @@ -280,7 +288,7 @@ const signJS = `

func (*sogouTranslator) calSign(from, to, text string) (string, error) {
vm := otto.New()
if err := vm.Set("query", from+to+text+"8511813095152"); err != nil {
if err := vm.Set("query", from+to+text+"109984457"); err != nil {
return "", fmt.Errorf("vm.Set query error: %v", err)
}
value, err := vm.Run(signJS)
Expand Down
34 changes: 7 additions & 27 deletions tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package fy

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"time"

Expand All @@ -27,17 +27,17 @@ 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, "GET", "https://fanyi.qq.com", nil, nil)
_, data, err := sendRequest(ctx, "POST", "https://fanyi.qq.com/api/reauth1232f", nil, nil)
if err != nil {
err = fmt.Errorf("sendRequest error: %v", err)
resp.Err = err
return
}

qtv, qtk, err := t.getQtk(string(data))
if err != nil {
resp.Err = fmt.Errorf("getQtk error: %v", err)
m := map[string]string{}
if err := json.Unmarshal(data, &m); err != nil {
resp.Err = err
return
}
qtv, qtk := m["qtv"], m["qtk"]

req.ToLang = t.convertLanguage(req.ToLang)
param := url.Values{
Expand All @@ -55,7 +55,6 @@ func (t *tencentTranslator) translate(ctx context.Context, req Request) (resp *R
req.Header.Set("Origin", "http://fanyi.qq.com")
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")
req.Header.Set("Cookie", fmt.Sprintf("qtv=%s; qtk=%s", qtv, qtk))
return nil
})
if err != nil {
Expand Down Expand Up @@ -93,22 +92,3 @@ func (*tencentTranslator) convertLanguage(language string) string {

return l
}

func (*tencentTranslator) getQtk(dataStr string) (qtv string, qtk string, err error) {
//document.cookie = "qtv=ad15088b8bcde724";
qtvResult := regexp.MustCompile(`"qtv=(?P<qtv>\S+)";`).FindStringSubmatch(dataStr)
if len(qtvResult) != 2 {
err = fmt.Errorf("cannot get qtv")
return
}
qtv = qtvResult[1]

//document.cookie = "qtk=aK4qrfL4bLogktVEfIMc785lhWKxOuLuOF243HgKs/lOcPqPhoiwsR+7ysGoTF/rqx1EABKUpNJq2OqbE1PY9T9ICiU2Qm2l0yIMqg3mworcjCX8tiaZzYjkQQqSTk7gCIz/WY0NhTJUrrOemb6nRQ==";
qtkResult := regexp.MustCompile(`"qtk=(?P<qtk>\S+)";`).FindStringSubmatch(dataStr)
if len(qtkResult) != 2 {
err = fmt.Errorf("cannot get qtk")
return
}
qtk = qtkResult[1]
return
}

0 comments on commit 3dbd667

Please sign in to comment.