-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
150 lines (116 loc) · 3.24 KB
/
app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import express from "express";
import { addExtra } from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
import fs from "fs";
import vanillaPuppeteer from "puppeteer";
import { EOL } from "os";
const puppeteer = addExtra(vanillaPuppeteer);
puppeteer.use(StealthPlugin());
var proxies = fs
.readFileSync("proxies.txt")
.toString()
.split(EOL)
.map((p) => {
let parts = p.split(":");
let host = parts[0];
let port = parts[1];
let login = parts[2];
let password = parts[3];
return {
host,
port,
login,
password,
};
});
puppeteer.use(StealthPlugin());
let app = express();
app.use(express.json());
app.listen(3000, () => {
console.log("Server running on port 3000");
});
app.post("/api/v1/translate", async (req, res, next) => {
let translation = await retryAsync(async () => {
return await translateText(req.body.text, req.body.from, [
req.body.to,
])
});
console.log("done");
res.send(translation);
});
app.post("/api/v1/paraphrase", async (req, res, next) => {
let translation = await retryAsync(async () => {
return await translateText(req.body.text, req.body.from, req.body.langs);
});
console.log("done");
res.send(translation);
});
async function translateText(text, from, langs) {
let proxyIndex = getRandomInt(0, proxies.length - 1);
var proxy = {
address: proxies[proxyIndex].host,
port: proxies[proxyIndex].port,
username: proxies[proxyIndex].login,
password: proxies[proxyIndex].password,
};
const browser = await puppeteer.launch({
headless: true,
args: [
'--disable-dev-shm-usage',
'--no-sandbox',
'--disable-setuid-sandbox',
`--proxy-server=${proxy.address}:${proxy.port}`],
});
try {
const languages = [from, ...langs];
let currentText = text;
const page = await browser.newPage();
await page.authenticate({
username: proxy.username,
password: proxy.password,
});
for (let i = 0; i < languages.length - 1; i++) {
var address = `https://translate.google.com/?sl=${languages[i]}&tl=${
languages[i + 1]
}`;
await page.goto(address);
//await page.focus('.er8xn');
//await page.keyboard.type(currentText);
await page.$eval(
".er8xn",
(el, value) => (el.value = value),
currentText
);
await page.focus(".er8xn");
await page.keyboard.type("\n");
var mainTranslationEl = await page.waitForSelector(".VIiyi", {
timeout: 30000,
});
let value = await page.evaluate((el) => el.innerText, mainTranslationEl);
currentText = value;
}
await browser.close();
return currentText;
} catch (ex) {
await browser.close();
console.log(ex.message);
throw new Error(ex.message);
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
async function retryAsync(func, retryCount = 2) {
for (let i = 0; i <= retryCount; i++) {
try {
return await func();
} catch (error) {
if(i == 3) {
throw error;
}
console.log('retrying...')
}
}
}