-
Notifications
You must be signed in to change notification settings - Fork 27
/
translate.js
227 lines (192 loc) · 5.76 KB
/
translate.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const axios = require('axios').default;
const { random } = require('lodash');
const zlib = require('zlib');
const DEEPL_BASE_URL = 'https://www2.deepl.com/jsonrpc';
function getICount(translateText) {
return (translateText || '').split('i').length - 1;
}
function getRandomNumber() {
return random(8300000, 8399998) * 1000;
}
function getTimestamp(iCount) {
const ts = Date.now();
if (iCount === 0) {
return ts;
}
iCount++;
return ts - (ts % iCount) + iCount;
}
function isRichText(text) {
return text.includes('<') && text.includes('>');
}
function formatPostString(postData) {
let postStr = JSON.stringify(postData);
const id = postData.id;
if ((id + 5) % 29 === 0 || (id + 3) % 13 === 0) {
postStr = postStr.replace('"method":"', '"method" : "');
} else {
postStr = postStr.replace('"method":"', '"method": "');
}
return postStr;
}
async function makeRequest(postData, method, dlSession = '', proxy = '') {
const url = `${DEEPL_BASE_URL}?client=chrome-extension,1.6.0&method=${method}`;
const postDataStr = formatPostString(postData);
const headers = {
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.9',
'Content-Type': 'application/json',
'Origin': 'chrome-extension://bppidhpdkcbahckohjehbehjmcnhpkck',
'Referer': 'https://www.deepl.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
};
if (dlSession) {
headers['Cookie'] = `dl_session=${dlSession}`;
}
try {
const response = await axios.post(url, postDataStr, {
headers: headers,
responseType: 'arraybuffer',
decompress: false,
...(proxy && { proxy: proxy }),
validateStatus: function (status) {
return status >= 200 && status < 500;
},
});
let data;
const encoding = response.headers['content-encoding'];
if (encoding === 'br') {
data = zlib.brotliDecompressSync(response.data).toString();
} else {
data = response.data.toString();
}
if (response.status >= 400) {
throw new Error(`Ошибка от сервера DeepL: ${response.status} - ${data}`);
}
return JSON.parse(data);
} catch (err) {
throw err;
}
}
async function splitText(text, tagHandling) {
const id = getRandomNumber();
const postData = {
jsonrpc: '2.0',
method: 'LMT_split_text',
id: id,
params: {
texts: [text],
lang: {
lang_user_selected: 'auto',
},
splitting: 'newlines',
text_type: (tagHandling === 'html' || tagHandling === 'xml' || isRichText(text)) ? 'richtext' : 'plaintext',
},
};
const response = await makeRequest(postData, 'LMT_split_text');
return response;
}
async function translate(
text,
sourceLang = 'auto',
targetLang = 'RU',
tagHandling = '',
dlSession = '',
proxy = ''
) {
if (!text) {
throw new Error('Нет текста для перевода.');
}
const splitResult = await splitText(text, tagHandling);
if (!splitResult || !splitResult.result) {
throw new Error('Не удалось разделить текст.');
}
const detectedSourceLang = splitResult.result.lang.detected || sourceLang;
const jobs = [];
const chunks = splitResult.result.texts[0].chunks;
for (let idx = 0; idx < chunks.length; idx++) {
const chunk = chunks[idx];
const sentence = chunk.sentences[0];
const contextBefore = idx > 0 ? [chunks[idx - 1].sentences[0].text] : [];
const contextAfter = idx < chunks.length - 1 ? [chunks[idx + 1].sentences[0].text] : [];
jobs.push({
kind: 'default',
raw_en_context_before: contextBefore,
raw_en_context_after: contextAfter,
preferred_num_beams: 1,
sentences: [
{
id: idx + 1,
prefix: sentence.prefix,
text: sentence.text,
},
],
});
}
let hasRegionalVariant = false;
let targetLangCode = targetLang;
const targetLangParts = targetLang.split('-');
if (targetLangParts.length > 1) {
targetLangCode = targetLangParts[0];
hasRegionalVariant = true;
}
const iCount = getICount(text);
const id = getRandomNumber();
const postData = {
jsonrpc: '2.0',
method: 'LMT_handle_jobs',
id: id,
params: {
jobs: jobs,
lang: {
source_lang_user_selected: detectedSourceLang.toUpperCase(),
target_lang: targetLangCode.toUpperCase(),
},
priority: 1,
commonJobParams: {
mode: 'translate',
...(hasRegionalVariant && { regionalVariant: targetLang }),
},
timestamp: getTimestamp(iCount),
},
};
const response = await makeRequest(postData, 'LMT_handle_jobs', dlSession, proxy);
if (!response || !response.result) {
throw new Error('Не удалось выполнить перевод.');
}
const translations = response.result.translations;
let translatedText = '';
let alternatives = [];
if (translations && translations.length > 0) {
const numBeams = translations[0].beams.length;
for (let i = 0; i < numBeams; i++) {
let altText = '';
for (const translation of translations) {
if (i < translation.beams.length) {
altText += translation.beams[i].sentences.map(s => s.text).join(' ');
}
}
if (altText) {
alternatives.push(altText);
}
}
for (const translation of translations) {
translatedText += translation.beams[0].sentences.map(s => s.text).join(' ') + ' ';
}
translatedText = translatedText.trim();
}
if (!translatedText) {
throw new Error('Перевод не получен.');
}
const result = {
code: 200,
id: id,
data: translatedText,
alternatives: alternatives,
source_lang: detectedSourceLang,
target_lang: targetLang,
method: dlSession ? 'Pro' : 'Free',
};
return result;
}
exports.translate = translate;