iobroker.senec 1.6.12 → 1.6.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/io-package.json +14 -14
- package/lib/api_trans.js +20 -21
- package/lib/state_attr.js +5606 -5606
- package/lib/state_trans.js +473 -468
- package/lib/tools.js +50 -52
- package/main.js +722 -484
- package/package.json +23 -16
package/lib/tools.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const axios = require(
|
|
1
|
+
const axios = require("axios").default;
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Tests whether the given variable is a real object and not an Array
|
|
@@ -6,11 +6,11 @@ const axios = require('axios').default;
|
|
|
6
6
|
* @returns {it is Record<string, any>}
|
|
7
7
|
*/
|
|
8
8
|
function isObject(it) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
// This is necessary because:
|
|
10
|
+
// typeof null === "object"
|
|
11
|
+
// typeof [] === "object"
|
|
12
|
+
// [] instanceof Object === true
|
|
13
|
+
return Object.prototype.toString.call(it) === "[object Object]";
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -19,8 +19,8 @@ function isObject(it) {
|
|
|
19
19
|
* @returns {it is any[]}
|
|
20
20
|
*/
|
|
21
21
|
function isArray(it) {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
if (typeof Array.isArray === "function") return Array.isArray(it);
|
|
23
|
+
return Object.prototype.toString.call(it) === "[object Array]";
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -31,16 +31,16 @@ function isArray(it) {
|
|
|
31
31
|
* @returns {Promise<string>}
|
|
32
32
|
*/
|
|
33
33
|
async function translateText(text, targetLang, yandexApiKey) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
if (targetLang === "en") {
|
|
35
|
+
return text;
|
|
36
|
+
} else if (!text) {
|
|
37
|
+
return "";
|
|
38
|
+
}
|
|
39
|
+
if (yandexApiKey) {
|
|
40
|
+
return translateYandex(text, targetLang, yandexApiKey);
|
|
41
|
+
} else {
|
|
42
|
+
return translateGoogle(text, targetLang);
|
|
43
|
+
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -51,20 +51,20 @@ async function translateText(text, targetLang, yandexApiKey) {
|
|
|
51
51
|
* @returns {Promise<string>}
|
|
52
52
|
*/
|
|
53
53
|
async function translateYandex(text, targetLang, apiKey) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
54
|
+
if (targetLang === "zh-cn") {
|
|
55
|
+
targetLang = "zh";
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
|
|
59
|
+
/** @type {any} */
|
|
60
|
+
const response = await axios({ url, timeout: 15000 });
|
|
61
|
+
if (response.data && response.data.text && isArray(response.data.text)) {
|
|
62
|
+
return response.data.text[0];
|
|
63
|
+
}
|
|
64
|
+
throw new Error("Invalid response for translate request");
|
|
65
|
+
} catch (e) {
|
|
66
|
+
throw new Error(`Could not translate to "${targetLang}": ${e}`);
|
|
67
|
+
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
@@ -74,27 +74,25 @@ async function translateYandex(text, targetLang, apiKey) {
|
|
|
74
74
|
* @returns {Promise<string>}
|
|
75
75
|
*/
|
|
76
76
|
async function translateGoogle(text, targetLang) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
}
|
|
77
|
+
try {
|
|
78
|
+
const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`;
|
|
79
|
+
const response = await axios({ url, timeout: 15000 });
|
|
80
|
+
if (isArray(response.data)) {
|
|
81
|
+
// we got a valid response
|
|
82
|
+
return response.data[0][0][0];
|
|
83
|
+
}
|
|
84
|
+
throw new Error("Invalid response for translate request");
|
|
85
|
+
} catch (e) {
|
|
86
|
+
if (e.response && e.response.status === 429) {
|
|
87
|
+
throw new Error(`Could not translate to "${targetLang}": Rate-limited by Google Translate`);
|
|
88
|
+
} else {
|
|
89
|
+
throw new Error(`Could not translate to "${targetLang}": ${e}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
94
92
|
}
|
|
95
93
|
|
|
96
94
|
module.exports = {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
95
|
+
isArray,
|
|
96
|
+
isObject,
|
|
97
|
+
translateText,
|
|
100
98
|
};
|