-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (93 loc) · 2.33 KB
/
Copy pathserver.js
File metadata and controls
109 lines (93 loc) · 2.33 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const express = require("express");
const translate = require("@vitalets/google-translate-api");
const cambridgeDictionary = require("camb-dict");
const languages = [
"en",
"de",
"fr",
"bg",
"nl",
"it",
"eo",
"fi",
"et",
"ko",
"pl",
"ru",
"sl",
"tr",
];
const app = express();
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.render("index");
});
app.post("/rewrite", (req, res) => {
useages("rewrite");
var strength = 4;
if (req.body.strength === "Simple") strength = 2;
else if (req.body.strength === "Strong") strength = 6;
var langs;
if (strength === 2) langs = ["fr", "it"];
else langs = chooseRandom(languages, strength);
var text = req.body.text;
translate(text, { to: langs[0] }).then((resp) => {
text = resp.text;
langs.push(resp.from.language.iso);
trans();
});
function trans() {
langs.shift();
translate(text, { to: langs[0] }).then((resp) => {
if (langs.length > 1) {
text = resp.text;
trans();
} else {
res.send({ rewritten: resp.text });
return;
}
});
}
});
app.get("/word", (req, res) => {
res.render("word");
});
app.post("/word", async (req, res) => {
useages("word");
try {
var dic = new cambridgeDictionary.Dictionary();
await dic.meaning(req.body.word.trim().toLowerCase()).then((data) => {
delete data.audio;
data.meaning = data.meaning.replace(": ", "");
translate(data.word, { to: "de" }).then((resp) => {
data.german = resp.text;
res.send(data);
});
});
} catch (e) {
res.send({ error: "We could't find this word." });
}
});
app.use("*", (req, res) => {
res.status(404).render("PageNotFound");
});
const chooseRandom = (arr, num = 5) => {
const res = [];
for (let i = 0; i < num; ) {
const random = Math.floor(Math.random() * arr.length);
if (res.indexOf(arr[random]) !== -1) {
continue;
}
res.push(arr[random]);
i++;
}
return res;
};
function useages(str) {
console.log(new Date().toLocaleTimeString() + "-> usage - " + str);
}
app.listen(5000, () => {
console.log("Rewrite app running on Port: 5000");
});