-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (58 loc) · 1.78 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
var express = require('express');
var cors = require('cors');
var scrapeIt = require('scrape-it');
var urls = [];
var contents = [];
var app = express();
app.use(express.json());
app.use(cors());
app.get('/', function (req, res) {
res.send('News API by @rodnavarroc');
});
app.get('/theverge/tech', (req, res) => {
articles = [];
contents = [];
scrapeIt("https://www.theverge.com/tech", {
articles: {
listItem: ".c-compact-river__entry",
data: {
url: {
selector: ".c-entry-box--compact__title a",
attr: "href"
}
}
}
}).then(({ data, response }) => {
urls = data.articles;
}).then(() => {
urls.forEach(function (e, idx, array) {
scrapeIt(e.url, {
title: ".c-page-title",
subtitle: ".c-entry-summary",
date: ".c-byline__item > time",
content: {
selector: ".c-entry-content p",
eq: 0
}
}).then(({ data, response }) => {
if (data.title != "" && data.subtitle != "" && data.content != "") contents.push(
{
"url": e.url,
"title": data.title,
"subtitle": data.subtitle,
"date": data.date,
"content": data.content
}
);
}).then(() => {
if (idx === array.length - 1) {
res.send(contents);
}
})
})
})
});
const puerto = process.env.PORT || 5000;
app.listen(puerto, function () {
console.log("Servidor iniciado en puerto:" + puerto);
});