// https://dev.to/waqasabbasi/building-a-search-engine-api-with-node-express-and-puppeteer-using-google-search-4m21
import express from 'express';
import { Server } from 'http';
import { HybridTranslate } from './HybridTranslate';
const app = express();
const port = 3100;
class TranslateServer {
private queue: Array<any> = [];
private inprogress: boolean = false;
async listen() {
//Import puppeteer function
const googleTranslate = new 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();