"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// https://dev.to/waqasabbasi/building-a-search-engine-api-with-node-express-and-puppeteer-using-google-search-4m21
const express_1 = __importDefault(require("express"));
const HybridTranslate_1 = require("./HybridTranslate");
const app = (0, express_1.default)();
const port = 3100;
class TranslateServer {
constructor() {
this.queue = [];
this.inprogress = false;
}
async listen() {
//Import puppeteer function
const googleTranslate = new HybridTranslate_1.HybridTranslate();
// await googleTranslate.init();
await googleTranslate.prepare();
// await googleTranslate.init();
// let timerInterval: any = null;
// clearInterval(timerInterval);
// timerInterval = setInterval(() => {
// }, 240000)
//Catches requests made to localhost:3000/search
app.get('/start', (request, response) => {
// clearInterval(timerInterval);
// timerInterval = setInterval(() => {
// }, 240000)
// googleTranslate.init();
// googleTranslate.prepare();
response.status(200);
response.json({ "status": "started" });
});
app.get('/stop', (request, response) => {
// clearInterval(timerInterval);
response.status(200);
response.json({ "status": "stoped" });
});
app.use((req, res, next) => {
if (this.inprogress) {
this.queue.push({ req, res, next });
}
else {
next();
}
});
app.get('/translateDeFa/:text', async (request, response, next) => {
if (!this.inprogress) {
this.inprogress = true;
const text = decodeURI(request.params.text);
const result = await googleTranslate.translate(text, "de", "fa");
response.status(200);
response.json({ result: result });
this.inprogress = false;
}
next();
});
app.use((req, res, next) => {
if (this.queue.length > 0) {
const queued = this.queue.shift();
queued.next();
}
next();
});
app.get('/', (req, res) => res.send('Hi Hybrid'));
//Initialises the express server on the port 30000
app.listen(port, "0.0.0.0", () => {
console.log(`app listening to localhost:${port}!`);
console.log(`example http://localhost:${port}/start`);
console.log(`example http://localhost:${port}translateFa/`);
});
}
}
const server = new TranslateServer();
server.listen();