forked from rewnd/lingualeo-subtitle-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlleo-api.js
79 lines (63 loc) · 2.3 KB
/
lleo-api.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
'use strict';
//TODO Use HTTPS
const client = require('bhttp'),
querystring = require('querystring');
class LinguaLeo {
constructor(email, password) {
this._email = email;
this._password = password;
this._session = client.session();
}
_errorHandler(error, isCritical = false) {
console.log(error);
if(isCritical) process.exit(0);
}
auth(callback) {
let authParams = querystring.stringify({
'email': this._email,
'password': this._password,
'type': 'login'});
this._session.get(`${LinguaLeo.apiParams.host}${LinguaLeo.apiParams.auth}?${authParams}`, {},
(err) => {
if(err) this._errorHandler(err, true);
this.checkAuth(callback);
});
}
checkAuth(callback) {
this._session.get(`${LinguaLeo.apiParams.host}${LinguaLeo.apiParams.checkAuth}`, {},
(err, resp) => {
if(err) this._errorHandler(err, true);
if(resp.body.is_authorized) {
console.log('Успешная авторизация в lingualeo');
console.log('Нажмите Ctrl+C для выхода');
callback();
} else {
console.error('Ошибка авторизации, пожалуйста, проверьте e-mail и пароль в файле auth-config.js');
process.exit(0);
}
});
}
getTranslations(word, callback) {
this._session.get(`${LinguaLeo.apiParams.host}${LinguaLeo.apiParams.getTranslate}?word=${word}`, {},
(err, resp) => {
if(err) this._errorHandler(err);
let respJson = resp.body;
callback(respJson);
}
);
}
addWord(word, tword) {
this._session.get(`${LinguaLeo.apiParams.host}${LinguaLeo.apiParams.addWord}?word=${word}&tword${tword}`, {},
(err, resp) => {
if(err) this._errorHandler(err);
});
}
}
LinguaLeo.apiParams = {
host: 'http://api.lingualeo.com',
auth: '/api/login',
checkAuth: '/isauthorized',
getTranslate: '/gettranslates',
addWord: '/addword'
};
module.exports = LinguaLeo;