-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
42 lines (36 loc) · 1.54 KB
/
gatsby-node.js
File metadata and controls
42 lines (36 loc) · 1.54 KB
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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const puppeteer = require('puppeteer');
const uri = 'https://news.google.com/covid19/map?hl=pt-BR&gl=BR&ceid=BR:pt-419';
exports.onCreateDevServer = ({ app }) => {
app.get('/data', async (req, res) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(uri);
data = await page.evaluate(() => {
// eslint-disable-next-line no-undef
const elements = document.querySelectorAll('div > div > div > table > tbody > tr');
const brazilColumns = elements[2].querySelectorAll('td');
const globalColumns = elements[0].querySelectorAll('td');
return {
global_cases: {
confirmed: parseInt(globalColumns[0].textContent.replace(/[.]/g, ''), 10),
per_million: parseInt(globalColumns[2].textContent.replace(/[.]/g, ''), 10),
recovered: parseInt(globalColumns[3].textContent.replace(/[.]/g, ''), 10),
deaths: parseInt(globalColumns[4].textContent.replace(/[.]/g, ''), 10),
},
brazil_cases: {
confirmed: parseInt(brazilColumns[0].textContent.replace(/[.]/g, ''), 10),
per_million: parseInt(brazilColumns[2].textContent.replace(/[.]/g, ''), 10),
recovered: parseInt(brazilColumns[3].textContent.replace(/[.]/g, ''), 10),
deaths: parseInt(brazilColumns[4].textContent.replace(/[.]/g, ''), 10),
},
}
});
await browser.close();
res.send(data);
});
};