forked from rewnd/lingualeo-subtitle-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (70 loc) · 2.26 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
'use strict';
const copypaste = require('copy-paste'),
notifier = require('node-notifier');
const LinguaLeo = require('./lleo-api.js');
const authConfig = require('./auth-config.js');
//checking if --nodict argument is set
let noDict = process.argv.some((elem) => {
return elem === '--nodict';
});
if(noDict) {
console.log('Установлен параметр --nodict, только показ переводов без добавления в словарь');
}
//clipboard
let clipboard = "";
copypaste.copy(clipboard);
let lingualeo = new LinguaLeo(authConfig.username, authConfig.password);
lingualeo.auth(listenClipboard);
function listenClipboard() {
let clipboardNew = copypaste.paste();
if (clipboardNew !== clipboard && clipboardNew.indexOf(" ") === -1 && clipboardNew) {
clipboard = clipboardNew;
lingualeo.getTranslations(clipboard, displayTranslations);
}
setTimeout(listenClipboard, 300);
}
//functions
function displayTranslations (respJson) {
let translationError = checkTranslations(respJson);
if (translationError) {
displayNotification(TranslationError);
return false;
}
let notification = {
title: `${clipboard} [${checkDict(respJson, noDict)}]`,
message: getTranslationsList(respJson)
};
displayNotification(notification);
}
function checkDict(respJson, noDict = true) {
if(respJson.is_user) {
return 'Есть в словаре';
}
if(!noDict) {
lingualeo.addWord(clipboard, respJson.translate[0].value);
}
return 'Новое слово';
}
function getTranslationsList(respJson) {
let translations = respJson.translate;
let translationsList = '';
translations.forEach((elem) => {
translationsList += '- ' + elem.value + '\n';
});
return translationsList;
}
function checkTranslations(respJson) {
if(respJson.word_forms === undefined) {
return {
title: 'Ошибка при поиске перевода',
message: respJson.error_msg
};
}
return false;
}
function displayNotification(notification) {
notifier.notify({
title: notification.title,
message: notification.message
});
}