-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (55 loc) · 2.14 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
const express = require('express');
const ws = require("ws")
const app = express();
const http = require('http');
const port = process.env.PORT || 3000;
const puppeteer = require('puppeteer');
const cities = ['alkmaar', 'almere', 'amersfoort', 'amstelveen', 'amsterdam-nl', 'apeldoorn',
'arnhem', 'assen', 'breda', 'delft', 's-hertogenbosch', 'the-hague-den-haag', 'den-helder',
'deventer', 'doorn', 'dordrecht', 'edam', 'ede', 'eindhoven', 'enkhuizen', 'enschede', 'giethoorn',
'gouda', 'haarlem', 'heerlen', 'hilversum', 'hoorn', 'kampen', 'kinderdijk', 'leeuwarden',
'leiden', 'lelystad', 'maastricht', 'medemblik', 'nijmegen', 'roermond', 'rotterdam', 'sneek',
'tilburg', 'utrecht', 'veere', 'venlo', 'wageningen', 'zaanse-schans', 'zwolle'];
let waterData = {};
async function scrapeProduct() {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox','--disable-setuid-sandbox']
});
const page = await browser.newPage();
let items;
for (let i = 0; i<cities.length; i++) {
const url = `https://www.tapsafe.org/is-${cities[i]}-tap-water-safe-to-drink/`;
try {
await page.goto(url);
await page.waitForSelector('.water-quality-graph__bar');
items = await page.$$eval('.water-quality-graph__bar', itemElements => itemElements.map(i => i.innerText.trim()));
} catch (e) {
items = 'unknown';
}
waterData[cities[i]] = items;
}
browser.close();
};
scrapeProduct();
app.set("views", `${__dirname}/views`);
app.set("view engine", "ejs");
app.use(express.static(`${__dirname}/public`, {
extensions:['html']
}));
app.get('/', (req, res) => {
res.render("splash");
})
const server = http.createServer(app);
const wss = new ws.Server({ server });
wss.on("connection", (con) => {
con.on("message", async (request) => {
const city = JSON.parse(request)
con.send(JSON.stringify(waterData[city]));
});
con.on("close", () => {
})
});
server.listen(port, ()=>{
console.log(`Server conected to port ${port}`);
});