-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
76 lines (68 loc) · 2.6 KB
/
server.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
require('events').EventEmitter.prototype._maxListeners = 0;
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const CORS = require('cors');
//PUPPETEER 🐱💻
const puppeteer = require('puppeteer');
//MIDDLEWARE
app.use(bodyParser.json());
app.use(CORS());
//START SERVER
const PORT = process.env.PORT || 8000
app.listen(PORT, () => {
console.log(`***Server is listening on ${PORT}***`);
});
//HEADLESS CHROME
(async () => {
//INIT PUPPETEER
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
const page = await browser.newPage();
const celebComedianList = 'https://en.wikipedia.org/wiki/List_of_comedians'
//GET CELEB COMEDIAN NAMES PAGE
await page.goto(celebComedianList);
const names = await page.evaluate(
() => Array.from(document.querySelectorAll('div ul li a[title]'))
.map(element => element.textContent.split(' ').join('_'))
);
//ASYNC MAP ALL CELEB INFO BY NAME
const results = names.slice(90, 100).map(async (name) => {
const page = await browser.newPage();
await page.goto(`https://en.wikipedia.org/api/rest_v1/page/html/${name}?redirect=false`)
const data = await page.evaluate(
() => document.querySelector('span .bday') ?
document.querySelector('span .bday').textContent :
null
)
const death = await page.evaluate(
() => Array.from(document.querySelectorAll('body section table tbody tr th'))
.find(th => th.textContent.includes('Died'))
)
return ({name:name, born: data, died: death})
})
//!ENDPOINTS
/* GET: COMEDIAN CELEBS */
app.get('/comedians', (req, res) => {
//console.log(names)
res.send(names)
});
/* GET: CELEB BIRTH DATA BY NAME (?NAME=) */
app.get('/byname', async (req, res) => {
//console.log(`https://en.wikipedia.org/api/rest_v1/page/html/${req.query.name}?redirect=false`);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`https://en.wikipedia.org/api/rest_v1/page/html/${req.query.name}?redirect=false`)
const data = await page.evaluate(
() => document.querySelector('span .bday').textContent
)
//console.log(data)
res.send(data)
});
/* GET: ALL CELEB DATA */
app.get('/all', async (req, res) => {
//START PROCESS
return Promise.all(results)
.then(complete => res.send({data: complete}))
.catch(err => console.log('ERROR: ', err))
})
})();