-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeepL.js
57 lines (49 loc) · 1.73 KB
/
DeepL.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
const axios = require("axios");
require("dotenv").config({path: __dirname + "/.env"});
// Get command line arguments
const arg = process.argv.slice(2)[0]; // The real command line arguments start from the third element
// Use regular expression to match Chinese characters
const isChinese = /[\u4e00-\u9fa5]/.test(arg);
const input = arg.trim();
if (isChinese || input.split(" ").length > 1) {
const target_lang = isChinese ? "EN-US" : "ZH";
const data = JSON.stringify({
text: [input],
target_lang: target_lang,
});
const translate_req = {
method: "post",
maxBodyLength: Infinity,
url: "https://api-free.deepl.com/v2/translate",
headers: {
Authorization: `DeepL-Auth-Key ${process.env.DEEPL_AUTH_KEY}`,
"Content-Type": "application/json",
},
data: data,
};
const usage_req = {
method: "get",
maxBodyLength: Infinity,
url: "https://api-free.deepl.com/v2/usage",
headers: {
Authorization: `DeepL-Auth-Key ${process.env.DEEPL_AUTH_KEY}`,
},
};
Promise.all([axios.request(translate_req), axios.request(usage_req)])
.then(([translate_res, usage_res]) => {
const translate_data = translate_res.data.translations[0];
const detected_source_language = translate_data.detected_source_language;
const text = translate_data.text;
const character_count = usage_res.data.character_count;
const character_limit = usage_res.data.character_limit;
console.log(
`[${detected_source_language}] ${input} <hr> [${target_lang}] ${text}`
);
console.log(`
<div style="text-align: end;padding: 5px 5px 0;">Usage: ${character_count}/${character_limit}</div>
`);
})
.catch((error) => {
console.log(error);
});
}