-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
93 lines (70 loc) · 2.63 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
93
require('dotenv').config()
const express = require('express')
const queue = require('express-queue');
const mysql = require('mysql');
const bodyparser = require('body-parser');
const GoogleMaps = require('./Class/GoogleMaps')
const Validation = require('./Class/Validation')
const app = express();
const queueMw = queue({ activeLimit: 2, queuedLimit: -1 });
app.use( bodyparser.json());
app.use(queueMw);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
async function start(query, webhook, time, hook){
const browser = await GoogleMaps.getBrowser();
const page = await browser.newPage();
const CONFIG = {
LISTING: process.env.LISTING,
SCROLL: process.env.SCROLL
}
page.setDefaultNavigationTimeout(0)
Promise.all([
await page.goto('https://maps.google.com'),
await page.waitForSelector('#searchboxinput'),
await page.click('#searchboxinput'),
await page.type('#searchboxinput', query),
await page.keyboard.press('Enter'),
await page.waitForSelector(CONFIG.LISTING),
await GoogleMaps.scroll(page, CONFIG.SCROLL, 1000, time),
await GoogleMaps.parse(browser, webhook, await GoogleMaps.getData(page), hook)
]).then(() => {
console.log('Response sent')
}).catch((err) => {
console.log(err)
});
}
app.get('/', (req, res) => {
const path = `/api/find`;
res.setHeader('Content-Type', 'text/html');
res.setHeader('Cache-Control', 's-max-age=1, stale-while-revalidate');
res.end(`Nice! Go to item: <a href="${path}"> ${path} </a>`);
});
app.post('/api/find', async(req, res) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 's-max-age=1, stale-while-revalidate');
if( Validation.validate(req.body) ){
const query = req.body.query;
const webhook = req.body.webhook;
const times = req.body.time;
const hook = req.body.hook;
await start(query, webhook, times, hook);
return res.status(200).json({
"error": false,
"message": "Sucesso, você receberá os dados em seu webhook em até 5 minutos.",
"query": query,
"webhook": webhook
});
}else{
return res.status(400).json({
"error": true,
"message": "Erro, verifique se você enviou todos os dados corretamente."
});
}
});
app.listen(process.env.PORT, () =>
console.log(`App listening on port ${process.env.PORT}!`),
);