-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaiyun.go
132 lines (113 loc) · 3.17 KB
/
caiyun.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
package fy
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/google/uuid"
"github.com/tidwall/gjson"
)
type caiyunTranslator struct {
token string
seq1, seq2 string
}
var caiyun translator = &caiyunTranslator{
token: "token:qgemv4jr1y38jyq6vhvi",
seq1: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
seq2: "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm",
}
func (b *caiyunTranslator) Desc() (string, string) {
return "caiyun", "https://fanyi.caiyunapp.com"
}
func CaiyunTranslate(ctx context.Context, req Request) *Response {
return caiyun.translate(ctx, req)
}
func (t *caiyunTranslator) translate(ctx context.Context, req Request) (resp *Response) {
resp = newResp(t)
req.ToLang, resp.Err = t.convertLanguage(req.ToLang)
if resp.Err != nil {
return
}
browserId := strings.ReplaceAll(uuid.New().String(), "-", "")
jwtBody := strings.NewReader(fmt.Sprintf(`{"browser_id":"%s"}`, browserId))
var jwtResponse struct {
Rc int `json:"rc"`
Jwt string `json:"jwt"`
}
_, data, err := sendRequest(ctx, http.MethodPost, "https://api.interpreter.caiyunai.com/v1/user/jwt/generate", jwtBody, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("X-Authorization", t.token)
return nil
})
if err := json.Unmarshal(data, &jwtResponse); err != nil {
resp.Err = fmt.Errorf("get jwt error: %v", err)
return
}
if jwtResponse.Rc != 0 {
resp.Err = fmt.Errorf("unexpected jwt response: %s", string(data))
return
}
param := map[string]interface{}{
"browser_id": browserId,
"cached": true,
"detect": true,
"dict": true,
"media": "text",
"os_type": "web",
"replaced": true,
"request_id": "web_fanyi",
"source": req.Text,
"trans_type": "auto2" + req.ToLang,
}
reqData, _ := json.Marshal(param)
body := bytes.NewReader(reqData)
_, data, err = sendRequest(ctx, http.MethodPost, "https://api.interpreter.caiyunai.com/v1/translator", body, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("X-Authorization", t.token)
req.Header.Set("T-Authorization", jwtResponse.Jwt)
return nil
})
if err != nil {
resp.Err = fmt.Errorf("sendRequest error: %v", err)
return
}
jr := gjson.Parse(string(data))
dit := jr.Get("target").String()
if dit == "" {
resp.Err = fmt.Errorf("cannot get translate result, resp: %s", string(data))
return
}
encoded := ""
for _, s := range dit {
if i := strings.IndexRune(t.seq1, s); i > -1 {
encoded += string(t.seq2[i])
} else {
encoded += string(s)
}
}
result, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
resp.Err = fmt.Errorf("base64 decode error: %v", err)
return
}
resp.Result = string(result)
return
}
func (*caiyunTranslator) convertLanguage(language string) (string, error) {
var l string
switch language {
case Chinese:
l = "zh"
case Japanese, English, Russian, French, Spanish:
l = language
}
if l == "" {
return "", fmt.Errorf("unsupported target language: %s", language)
}
return l, nil
}