iobroker.hass 1.3.0 → 2.0.1
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/LICENSE +1 -1
- package/README.md +40 -19
- package/admin/i18n/de.json +8 -0
- package/admin/i18n/en.json +8 -0
- package/admin/i18n/es.json +8 -0
- package/admin/i18n/fr.json +8 -0
- package/admin/i18n/it.json +8 -0
- package/admin/i18n/nl.json +8 -0
- package/admin/i18n/pl.json +8 -0
- package/admin/i18n/pt.json +8 -0
- package/admin/i18n/ru.json +8 -0
- package/admin/i18n/uk.json +8 -0
- package/admin/i18n/zh-cn.json +8 -0
- package/admin/jsonConfig.json +44 -0
- package/io-package.json +61 -46
- package/package.json +29 -14
- package/admin/i18n/de/translations.json +0 -8
- package/admin/i18n/en/translations.json +0 -8
- package/admin/i18n/es/translations.json +0 -8
- package/admin/i18n/fr/translations.json +0 -8
- package/admin/i18n/it/translations.json +0 -8
- package/admin/i18n/nl/translations.json +0 -8
- package/admin/i18n/pl/translations.json +0 -8
- package/admin/i18n/pt/translations.json +0 -8
- package/admin/i18n/ru/translations.json +0 -8
- package/admin/i18n/zh-cn/translations.json +0 -8
- package/admin/index_m.html +0 -108
- package/admin/words.js +0 -11
- package/lib/hass.js +0 -270
- package/lib/tools.js +0 -92
- package/main.js +0 -468
package/admin/index_m.html
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
<html>
|
|
2
|
-
<head>
|
|
3
|
-
<!-- Materialze style -->
|
|
4
|
-
<link rel="stylesheet" type="text/css" href="../../css/adapter.css"/>
|
|
5
|
-
<link rel="stylesheet" type="text/css" href="../../lib/css/materialize.css">
|
|
6
|
-
|
|
7
|
-
<script type="text/javascript" src="../../lib/js/jquery-3.2.1.min.js"></script>
|
|
8
|
-
<script type="text/javascript" src="../../socket.io/socket.io.js"></script>
|
|
9
|
-
|
|
10
|
-
<script type="text/javascript" src="../../js/translate.js"></script>
|
|
11
|
-
<script type="text/javascript" src="../../lib/js/materialize.js"></script>
|
|
12
|
-
<script type="text/javascript" src="../../js/adapter-settings.js"></script>
|
|
13
|
-
<script type="text/javascript" src="words.js"></script>
|
|
14
|
-
|
|
15
|
-
<script type="text/javascript">
|
|
16
|
-
|
|
17
|
-
function load(settings, onChange) {
|
|
18
|
-
if (!settings) return;
|
|
19
|
-
|
|
20
|
-
if (settings.password === undefined) settings.password = '';
|
|
21
|
-
settings.passwordRepeat = settings.password;
|
|
22
|
-
|
|
23
|
-
$('.value').each(function () {
|
|
24
|
-
var key = $(this).attr('id');
|
|
25
|
-
var $value = $('#' + key + '.value');
|
|
26
|
-
if ($value.attr('type') === 'checkbox') {
|
|
27
|
-
$value.prop('checked', settings[key]).change(function() {
|
|
28
|
-
onChange();
|
|
29
|
-
});
|
|
30
|
-
} else {
|
|
31
|
-
$value.val(settings[key]).change(function() {
|
|
32
|
-
onChange();
|
|
33
|
-
}).keyup(function() {
|
|
34
|
-
onChange();
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
// Signal to admin, that no changes yet
|
|
39
|
-
onChange(false);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function save(callback) {
|
|
43
|
-
var obj = {};
|
|
44
|
-
|
|
45
|
-
if ($('#password').val() !== $('#passwordRepeat').val()) {
|
|
46
|
-
showMessage(_('Passwords missmatch!'), _('Warning'), 'alert');
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
$('.value').each(function () {
|
|
51
|
-
var $this = $(this);
|
|
52
|
-
if ($this.attr('type') === 'checkbox') {
|
|
53
|
-
obj[$this.attr('id')] = $this.prop('checked');
|
|
54
|
-
} else {
|
|
55
|
-
obj[$this.attr('id')] = $this.val();
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
delete obj.passwordRepeat;
|
|
59
|
-
|
|
60
|
-
callback(obj);
|
|
61
|
-
}
|
|
62
|
-
</script>
|
|
63
|
-
</head>
|
|
64
|
-
<body>
|
|
65
|
-
<div class="m adapter-container">
|
|
66
|
-
<div class="row">
|
|
67
|
-
<div class="col s12 m4 l2">
|
|
68
|
-
<img src="hass.png" class="logo">
|
|
69
|
-
</div>
|
|
70
|
-
</div>
|
|
71
|
-
|
|
72
|
-
<div class="row">
|
|
73
|
-
<div class="col s12 m8 l8">
|
|
74
|
-
<div class="col s4 input-field">
|
|
75
|
-
<input type="text" class="value" id="host" />
|
|
76
|
-
<label for="host" class="translate">Home assistant IP:</label>
|
|
77
|
-
</div>
|
|
78
|
-
<div class="col s4 input-field">
|
|
79
|
-
<input type="text" class="value" id="port" size="5" maxlength="5"/>
|
|
80
|
-
<label for="port" class="translate">Home assistant WS Port:</label>
|
|
81
|
-
</div>
|
|
82
|
-
</div>
|
|
83
|
-
</div>
|
|
84
|
-
|
|
85
|
-
<div class="row">
|
|
86
|
-
<div class="col s12 m8 l8">
|
|
87
|
-
<div class="col s6 m4 input-field">
|
|
88
|
-
<input type="password" class="value" id="password" />
|
|
89
|
-
<label for="password" class="translate">Password:</label>
|
|
90
|
-
</div>
|
|
91
|
-
<div class="col s6 m4 input-field">
|
|
92
|
-
<input type="password" class="value" id="passwordRepeat" />
|
|
93
|
-
<label for="passwordRepeat" class="translate">Password repeat:</label>
|
|
94
|
-
</div>
|
|
95
|
-
</div>
|
|
96
|
-
</div>
|
|
97
|
-
|
|
98
|
-
<div class="row">
|
|
99
|
-
<div class="col s12 m8 l8">
|
|
100
|
-
<div class="col s6 m4 input-field">
|
|
101
|
-
<input id="secure" type="checkbox" class="value" />
|
|
102
|
-
<span for="secure" class="translate">Secure:</span>
|
|
103
|
-
</div>
|
|
104
|
-
</div>
|
|
105
|
-
</div>
|
|
106
|
-
</div>
|
|
107
|
-
</body>
|
|
108
|
-
</html>
|
package/admin/words.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/*global systemDictionary:true */
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
systemDictionary = {
|
|
5
|
-
"Home assistant IP:": { "en": "Home assistant IP:", "de": "IP-Adresse des Heimassistenten:", "ru": "Домашний помощник IP:", "pt": "IP do assistente doméstico:", "nl": "Thuisassistent IP:", "fr": "IP de l'assistant à domicile :", "it": "IP assistente domestico:", "es": "IP del asistente de hogar:", "pl": "Adres IP asystenta domowego:", "zh-cn": "家庭助理IP:"},
|
|
6
|
-
"Home assistant WS Port:": { "en": "Home assistant WS Port:", "de": "Home Assistant WS-Port:", "ru": "Домашний помощник WS Порт:", "pt": "Assistente doméstico Porta WS:", "nl": "Thuisassistent WS Poort:", "fr": "Port WS de l'assistant domestique :", "it": "Porta WS dell'assistente domestico:", "es": "Asistente doméstico Puerto WS:", "pl": "Asystent domowy Port WS:", "zh-cn": "家庭助理 WS 端口:"},
|
|
7
|
-
"Password repeat:": { "en": "Password repeat:", "de": "Passwort wiederholen:", "ru": "Повтор пароля:", "pt": "Repetição de senha:", "nl": "Wachtwoord herhalen:", "fr": "Répéter le mot de passe :", "it": "Ripeti password:", "es": "Repite la contraseña:", "pl": "Powtórz hasło:", "zh-cn": "密码重复:"},
|
|
8
|
-
"Password:": { "en": "Password:", "de": "Passwort:", "ru": "Пароль:", "pt": "Senha:", "nl": "Wachtwoord:", "fr": "Mot de passe:", "it": "Parola d'ordine:", "es": "Clave:", "pl": "Hasło:", "zh-cn": "密码:"},
|
|
9
|
-
"Passwords missmatch!": { "en": "Passwords missmatch!", "de": "Passwörter stimmen nicht überein!", "ru": "Пароли не совпадают!", "pt": "As senhas não correspondem!", "nl": "Wachtwoorden komen niet overeen!", "fr": "Les mots de passe ne correspondent pas !", "it": "Le password non corrispondono!", "es": "¡Las contraseñas no coinciden!", "pl": "Niezgodność haseł!", "zh-cn": "密码不匹配!"},
|
|
10
|
-
"Secure:": { "en": "HTTPS?", "de": "HTTPS?", "ru": "HTTPS?", "pt": "HTTPS?", "nl": "HTTPS?", "fr": "HTTPS ?", "it": "HTTPS?", "es": "¿HTTPS?", "pl": "HTTPS?", "zh-cn": "HTTPS?"},
|
|
11
|
-
};
|
package/lib/hass.js
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
const util = require('util');
|
|
2
|
-
const EventEmitter = require('events').EventEmitter;
|
|
3
|
-
const WebSocket = require('ws');
|
|
4
|
-
|
|
5
|
-
function HASS(options, log) {
|
|
6
|
-
if (!(this instanceof HASS)) {
|
|
7
|
-
return new HASS(options);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
options = options || {};
|
|
11
|
-
options.host = options.host || '127.0.0.1';
|
|
12
|
-
options.port = parseInt(options.port, 10) || 8123;
|
|
13
|
-
|
|
14
|
-
const ERRORS = {
|
|
15
|
-
1: 'ERR_CANNOT_CONNECT',
|
|
16
|
-
2: 'ERR_INVALID_AUTH',
|
|
17
|
-
3: 'ERR_CONNECTION_LOST'
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
this.socket = null;
|
|
21
|
-
const that = this;
|
|
22
|
-
let currentId = 1;
|
|
23
|
-
const requests = {};
|
|
24
|
-
let connected;
|
|
25
|
-
let connectTimeout = null;
|
|
26
|
-
let closed = false;
|
|
27
|
-
|
|
28
|
-
function subscribeEvents(socket, callback) {
|
|
29
|
-
if (socket && typeof socket.send === 'function') {
|
|
30
|
-
const id = currentId++;
|
|
31
|
-
requests[id] = {type: 'subscribe_events', ts: Date.now(), cb: callback};
|
|
32
|
-
|
|
33
|
-
socket.send(JSON.stringify({
|
|
34
|
-
id: id,
|
|
35
|
-
type: 'subscribe_events'
|
|
36
|
-
/*event_type: 'state_changed'*/
|
|
37
|
-
}));
|
|
38
|
-
} else {
|
|
39
|
-
callback && callback('not connected');
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getConfig(socket, callback) {
|
|
44
|
-
if (socket && typeof socket.send === 'function') {
|
|
45
|
-
const id = currentId++;
|
|
46
|
-
requests[id] = {type: 'get_config', cb: callback, ts: Date.now()};
|
|
47
|
-
socket.send(JSON.stringify({
|
|
48
|
-
id: id,
|
|
49
|
-
type: 'get_config'
|
|
50
|
-
}));
|
|
51
|
-
} else {
|
|
52
|
-
callback && callback('not connected');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function getStates(socket, callback) {
|
|
57
|
-
if (socket && typeof socket.send === 'function') {
|
|
58
|
-
const id = currentId++;
|
|
59
|
-
requests[id] = {type: 'get_states', cb: callback, ts: Date.now()};
|
|
60
|
-
socket.send(JSON.stringify({
|
|
61
|
-
id: id,
|
|
62
|
-
type: 'get_states'
|
|
63
|
-
}));
|
|
64
|
-
} else {
|
|
65
|
-
callback && callback('not connected');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function getPanels(socket, callback) {
|
|
70
|
-
if (socket && typeof socket.send === 'function') {
|
|
71
|
-
const id = currentId++;
|
|
72
|
-
requests[id] = {type: 'get_panels', cb: callback, ts: Date.now()};
|
|
73
|
-
socket.send(JSON.stringify({
|
|
74
|
-
id: id,
|
|
75
|
-
type: 'get_panels'
|
|
76
|
-
}));
|
|
77
|
-
} else {
|
|
78
|
-
callback && callback('not connected');
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getServices(socket, callback) {
|
|
83
|
-
if (socket && typeof socket.send === 'function') {
|
|
84
|
-
const id = currentId++;
|
|
85
|
-
requests[id] = {type: 'get_services', cb: callback, ts: Date.now()};
|
|
86
|
-
socket.send(JSON.stringify({
|
|
87
|
-
id: id,
|
|
88
|
-
type: 'get_services'
|
|
89
|
-
}));
|
|
90
|
-
} else {
|
|
91
|
-
callback && callback('not connected');
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function callService(socket, service, domain, serviceData, target, callback) {
|
|
96
|
-
if (socket && typeof socket.send === 'function') {
|
|
97
|
-
const id = currentId++;
|
|
98
|
-
requests[id] = {type: 'call_service', cb: callback, ts: Date.now()};
|
|
99
|
-
socket.send(JSON.stringify({
|
|
100
|
-
id: id,
|
|
101
|
-
type: 'call_service',
|
|
102
|
-
domain: domain || '',
|
|
103
|
-
service: service,
|
|
104
|
-
service_data: serviceData,
|
|
105
|
-
target
|
|
106
|
-
}));
|
|
107
|
-
} else {
|
|
108
|
-
callback && callback('not connected');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function sendAuth(socket, pass) {
|
|
113
|
-
if (socket && typeof socket.send === 'function') {
|
|
114
|
-
socket.send(JSON.stringify({
|
|
115
|
-
type: 'auth',
|
|
116
|
-
access_token: pass
|
|
117
|
-
}));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function initSocket(socket) {
|
|
122
|
-
socket.on('message', msg => {
|
|
123
|
-
log.silly(msg);
|
|
124
|
-
|
|
125
|
-
const response = JSON.parse(msg);
|
|
126
|
-
if (response.type === 'event') {
|
|
127
|
-
if (response.event.data && response.event.event_type === 'system_log_event') {
|
|
128
|
-
if (response.event.data.level === 'WARNING') {
|
|
129
|
-
log.warn('EVENT: ' + response.event.data.message);
|
|
130
|
-
} else
|
|
131
|
-
if (response.event.data.level === 'ERROR') {
|
|
132
|
-
log.error('EVENT: ' + response.event.data.message);
|
|
133
|
-
} else {
|
|
134
|
-
log.debug('EVENT: ' + response.event.data.message);
|
|
135
|
-
}
|
|
136
|
-
} else if (response.event && response.event.event_type === 'state_changed') {
|
|
137
|
-
that.emit('state_changed', response.event.data.new_state);
|
|
138
|
-
}
|
|
139
|
-
} else
|
|
140
|
-
if (response.type === 'auth_required') {
|
|
141
|
-
if (!options.password) {
|
|
142
|
-
that.emit('error', 'Password required. Connection closed');
|
|
143
|
-
socket.terminate();
|
|
144
|
-
} else {
|
|
145
|
-
setTimeout(() => sendAuth(socket, options.password), 50);
|
|
146
|
-
}
|
|
147
|
-
} else
|
|
148
|
-
if (response.type === 'auth_ok') {
|
|
149
|
-
setImmediate(() =>
|
|
150
|
-
subscribeEvents(socket, err => {
|
|
151
|
-
if (!err) {
|
|
152
|
-
connected = true;
|
|
153
|
-
that.emit('connected');
|
|
154
|
-
}
|
|
155
|
-
}));
|
|
156
|
-
} else if (response.id === undefined) {
|
|
157
|
-
log.error(`Invalid answer: ${msg}`);
|
|
158
|
-
} else {
|
|
159
|
-
if (response.type === 'result' && requests[response.id]) {
|
|
160
|
-
log.debug(`got answer for ${requests[response.id].type} success = ${response.success}, result = ${JSON.stringify(response.result)}`);
|
|
161
|
-
if (typeof requests[response.id].cb === 'function') {
|
|
162
|
-
requests[response.id].cb(!response.success, response.result);
|
|
163
|
-
delete requests[response.id];
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
socket.on('error', err => {
|
|
170
|
-
socket = null;
|
|
171
|
-
if (err && err.message.indexOf('RSV2 and RSV3 must be clear') !== -1) {
|
|
172
|
-
// ignore deflate error
|
|
173
|
-
} else {
|
|
174
|
-
log.error(err);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
socket.on('open', () => {
|
|
178
|
-
if (!connected) {
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
socket.on('close', () => {
|
|
183
|
-
that.socket = null;
|
|
184
|
-
if (connected) {
|
|
185
|
-
connected = false;
|
|
186
|
-
that.emit('disconnected');
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
if (!connectTimeout && !closed) {
|
|
190
|
-
setTimeout(() => {
|
|
191
|
-
connectTimeout = null;
|
|
192
|
-
that.connect();
|
|
193
|
-
}, 3000);
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
this.isConnected = () => connected;
|
|
199
|
-
|
|
200
|
-
this.getConfig = function (callback) {
|
|
201
|
-
if (!connected) {
|
|
202
|
-
typeof callback === 'function' && callback('not connected');
|
|
203
|
-
} else {
|
|
204
|
-
getConfig(this.socket, callback);
|
|
205
|
-
}
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
this.getStates = function (callback) {
|
|
209
|
-
if (!connected) {
|
|
210
|
-
typeof callback === 'function' && callback('not connected');
|
|
211
|
-
} else {
|
|
212
|
-
getStates(this.socket, callback);
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
this.getServices = function (callback) {
|
|
217
|
-
if (!connected) {
|
|
218
|
-
typeof callback === 'function' && callback('not connected');
|
|
219
|
-
} else {
|
|
220
|
-
getServices(this.socket, callback);
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
this.getPanels = function (callback) {
|
|
225
|
-
if (!connected) {
|
|
226
|
-
typeof callback === 'function' && callback('not connected');
|
|
227
|
-
} else {
|
|
228
|
-
getPanels(this.socket, callback);
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
this.callService = function (service, domain, serviceData, target, callback) {
|
|
233
|
-
if (!connected) {
|
|
234
|
-
typeof callback === 'function' && callback('not connected');
|
|
235
|
-
} else {
|
|
236
|
-
callService(this.socket, service, domain, serviceData, target, callback);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
this.connect = function () {
|
|
241
|
-
if (connectTimeout) {
|
|
242
|
-
clearTimeout(connectTimeout);
|
|
243
|
-
connectTimeout = null;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.socket = new WebSocket(`ws${options.secure ? 's' : ''}://${options.host}:${options.port}/api/websocket`, {
|
|
247
|
-
perMessageDeflate: false
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
initSocket(this.socket);
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
this.close = function () {
|
|
254
|
-
if (connectTimeout) {
|
|
255
|
-
clearTimeout(connectTimeout);
|
|
256
|
-
connectTimeout = null;
|
|
257
|
-
}
|
|
258
|
-
closed = true;
|
|
259
|
-
if (this.socket) {
|
|
260
|
-
this.socket.close();
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return this;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// extend the EventEmitter class using our class
|
|
268
|
-
util.inherits(HASS, EventEmitter);
|
|
269
|
-
|
|
270
|
-
module.exports = HASS;
|
package/lib/tools.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
const axios = require("axios");
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Tests whether the given variable is a real object and not an Array
|
|
5
|
-
* @param {any} it The variable to test
|
|
6
|
-
* @returns {it is Record<string, any>}
|
|
7
|
-
*/
|
|
8
|
-
function isObject(it) {
|
|
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
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Tests whether the given variable is really an Array
|
|
18
|
-
* @param {any} it The variable to test
|
|
19
|
-
* @returns {it is any[]}
|
|
20
|
-
*/
|
|
21
|
-
function isArray(it) {
|
|
22
|
-
if (Array.isArray != null)
|
|
23
|
-
return Array.isArray(it);
|
|
24
|
-
return Object.prototype.toString.call(it) === "[object Array]";
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Choose the right tranalation API
|
|
29
|
-
* @param {string} text The text to translate
|
|
30
|
-
* @param {string} targetLang The target languate
|
|
31
|
-
* @param {string} yandex api key
|
|
32
|
-
* @returns {Promise<string>}
|
|
33
|
-
*/
|
|
34
|
-
async function translateText(text, targetLang, yandex) {
|
|
35
|
-
if (targetLang === "en") {
|
|
36
|
-
return text;
|
|
37
|
-
}
|
|
38
|
-
if (yandex) {
|
|
39
|
-
return await translateYandex(text, targetLang, yandex);
|
|
40
|
-
} else {
|
|
41
|
-
return await translateGoogle(text, targetLang);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Translates text with Yandex API
|
|
47
|
-
* @param {string} text The text to translate
|
|
48
|
-
* @param {string} targetLang The target languate
|
|
49
|
-
* @param {string} yandex api key
|
|
50
|
-
* @returns {Promise<string>}
|
|
51
|
-
*/
|
|
52
|
-
async function translateYandex(text, targetLang, yandex) {
|
|
53
|
-
if (targetLang === "zh-cn") {
|
|
54
|
-
targetLang = "zh";
|
|
55
|
-
}
|
|
56
|
-
try {
|
|
57
|
-
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${yandex}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
|
|
58
|
-
const response = await axios({url, timeout: 15000});
|
|
59
|
-
if (response.data && response.data['text']) {
|
|
60
|
-
return response.data['text'][0];
|
|
61
|
-
}
|
|
62
|
-
throw new Error("Invalid response for translate request");
|
|
63
|
-
} catch (e) {
|
|
64
|
-
throw new Error(`Could not translate to "${targetLang}": ${e}`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Translates text with Google API
|
|
70
|
-
* @param {string} text The text to translate
|
|
71
|
-
* @param {string} targetLang The target languate
|
|
72
|
-
* @returns {Promise<string>}
|
|
73
|
-
*/
|
|
74
|
-
async function translateGoogle(text, targetLang) {
|
|
75
|
-
try {
|
|
76
|
-
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`;
|
|
77
|
-
const response = await axios({url, timeout: 15000});
|
|
78
|
-
if (isArray(response.data)) {
|
|
79
|
-
// we got a valid response
|
|
80
|
-
return response.data[0][0][0];
|
|
81
|
-
}
|
|
82
|
-
throw new Error("Invalid response for translate request");
|
|
83
|
-
} catch (e) {
|
|
84
|
-
throw new Error(`Could not translate to "${targetLang}": ${e}`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
module.exports = {
|
|
89
|
-
isArray,
|
|
90
|
-
isObject,
|
|
91
|
-
translateText
|
|
92
|
-
};
|