forked from vitalets/google-translate-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
282 lines (248 loc) · 8.06 KB
/
index.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const got = require('got');
const deepClone = require('lodash.clonedeep');
const languages = require('./languages');
const ENDPOINT_MAP = {};
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
function extract(key, res) {
const re = new RegExp(`"${key}":".*?"`);
const result = re.exec(res.body);
if (result !== null) {
return result[0].replace(`"${key}":"`, '').slice(0, -1);
}
return '';
}
ENDPOINT_MAP.website = async function(text, opts, gotopts) {
gotopts = deepClone(gotopts);
let url = 'https://translate.google.' + opts.tld;
let res = await got(url, gotopts);
const rpcids = 'MkEWBc';
const data = {
'rpcids': rpcids,
'source-path': '/',
'f.sid': extract('FdrFJe', res),
'bl': extract('cfb2h', res),
'hl': 'en-US',
'soc-app': 1,
'soc-platform': 1,
'soc-device': 1,
'_reqid': Math.floor(1000 + (Math.random() * 9000)),
'rt': 'c'
};
url += '/_/TranslateWebserverUi/data/batchexecute';
gotopts.searchParams = data;
gotopts.body = 'f.req=' + encodeURIComponent(JSON.stringify([[[rpcids, JSON.stringify([[text, opts.from, opts.to, true], [null]]), null, 'generic']]])) + '&';
gotopts.headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
res = await got.post(url, gotopts);
let json = res.body.slice(6);
let length = '';
const result = {
text: '',
pronunciation: void 0,
from: {
language: {
didYouMean: false,
iso: ''
},
text: {
autoCorrected: false,
value: '',
didYouMean: false
}
},
raw: void 0,
endpoint: 'website'
};
try {
length = /^\d+/.exec(json)[0];
json = JSON.parse(json.slice(length.length, parseInt(length, 10) + length.length));
json = JSON.parse(json[0][2]);
opts.raw && (result.raw = json);
} catch (e) {
return result;
}
if (json[1][0][0][5] == null) {
// translation not found, could be a hyperlink or gender-specific translation?
result.text = json[1][0][0][0];
} else {
json[1][0][0][5].forEach(obj => {
if (obj[0]) {
result.text += obj[0];
}
});
}
result.pronunciation = json[0][0];
// From language
if (json[0] && json[0][1] && json[0][1][1]) {
result.from.language.didYouMean = true;
result.from.language.iso = json[0][1][1][0];
} else if (json[1][3] === 'auto') {
result.from.language.iso = json[2];
} else {
result.from.language.iso = json[1][3];
}
// Did you mean & autocorrect
if (json[0] && json[0][1] && json[0][1][0]) {
const str = json[0][1][0][0][1]
.replace(/<b>(<i>)?/g, '[')
.replace(/(<\/i>)?<\/b>/g, ']');
result.from.text.value = str;
if (json[0][1][0][2] === 1) {
result.from.text.autoCorrected = true;
} else {
result.from.text.didYouMean = true;
}
}
return result;
};
ENDPOINT_MAP.dictExt = async function(text, opts, gotopts) {
gotopts = deepClone(gotopts);
const query = new URLSearchParams([
['client', 'dict-chrome-ex'],
['sl', opts.from],
['tl', opts.to],
['q', text],
['dj', 1],
['dt', 't'],
['dt', 'sp'],
['dt', 'ld'],
['dt', 'bd']
]);
const url = 'https://clients' + (~~(Math.random() * 5) + 1) + '.google.com/translate_a/single';
gotopts.searchParams = query;
const res = await got(url, gotopts).json();
return {
text: res.sentences.map(r => r.trans).join(''),
pronunciation: res.sentences[res.sentences.length - 1].src_translit,
from: {
language: {
didYouMean: false,
iso: res.src
},
text: {
autoCorrected: false,
value: res.sentences.map(r => r.orig).join(''),
didYouMean: false
}
},
raw: opts.raw && res,
endpoint: 'dictExt'
};
};
ENDPOINT_MAP.api = async function(text, opts, gotopts) {
gotopts = deepClone(gotopts);
const query = {
'client': 'gtx',
'dt': 't',
'sl': opts.from,
'tl': opts.to,
'q': text
};
const url = 'https://translate.googleapis.com/translate_a/single';
gotopts.searchParams = query;
const res = await got(url, gotopts).json();
return {
text: res[0].map(r => r[0]).join(''),
// not supported
pronunciation: void 0,
from: {
language: {
didYouMean: false,
iso: res[2]
},
text: {
autoCorrected: false,
value: res[0].map(r => r[1]).join(''),
didYouMean: false
}
},
raw: opts.raw && res,
endpoint: 'api'
};
};
async function translate(text, opts, gotopts) {
opts = opts || {};
gotopts = gotopts || {};
gotopts.headers = gotopts.headers || {};
gotopts.headers['user-agent'] = gotopts.headers['User-Agent']
|| gotopts.headers['user-agent']
|| DEFAULT_USER_AGENT;
delete gotopts.headers['User-Agent'];
[opts.from, opts.to].forEach(lang => {
if (lang && !languages.isSupported(lang)) {
throw new Error('The language \'' + lang + '\' is not supported');
}
});
opts.from = opts.from || 'auto';
opts.to = opts.to || 'en';
opts.tld = opts.tld || 'com';
opts.endpointFallback = opts.endpointFallback == null ? true : !!opts.endpointFallback;
opts.from = languages.getCode(opts.from);
opts.to = languages.getCode(opts.to);
const allEndpoints = Object.keys(ENDPOINT_MAP);
let endpoints = opts.endpoints;
if (!endpoints || !endpoints.length || !Array.isArray(endpoints)) {
endpoints = allEndpoints;
}
let arr = [];
let len = endpoints.length;
endpoints.forEach(ep => {
if (!ENDPOINT_MAP[ep]) {
const err = `unknown endpoint: ${ep}`;
if (len > 1) {
console.warn(err);
} else {
throw new Error(err);
}
} else {
arr.push(ep);
}
});
len = (endpoints = arr).length;
let err;
if (opts.randomEndpoint && len > 1) {
let idx = ~~(Math.random() * len);
let endpoint = endpoints[idx];
try {
return await ENDPOINT_MAP[endpoint](text, opts, gotopts);
} catch (e) {
if (!opts.endpointFallback) {
throw e;
}
err = e;
let newIdx;
do {
newIdx = ~~(Math.random() * len);
} while(newIdx === idx);
for (let i = 0; i < len; i++) {
if (i === idx) {
continue;
}
console.warn(`failed to translate with the random endpoint '${endpoint}'. Trying the next random endpoint '${endpoint = endpoints[idx = newIdx]}'.`);
try {
return await ENDPOINT_MAP[endpoint](text, opts, gotopts);
} catch (e) {
if (!opts.endpointFallback) {
throw e;
}
err = e;
}
}
}
} else {
for (let i = 0, endpoint; i < len; i++) {
endpoint = endpoints[i];
try {
return await ENDPOINT_MAP[endpoint](text, opts, gotopts);
} catch (e) {
if (!opts.endpointFallback) {
throw e;
}
err = e;
console.warn(`failed to translate with the endpoint '${endpoint}'.${len > 1 ? ' Trying the next endpoint.' : ''}`);
}
}
}
throw err;
}
module.exports = translate;
module.exports.languages = languages;