-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (51 loc) · 1.32 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
// Require the module
var bodyParser = require("body-parser");
const cheerio = require("cheerio");
const axios = require("axios");
// Set up the Body Parser to your App
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const cryptoPages = [
{
name: "Independent",
address: "https://www.independent.co.uk/topic/cryptocurrency",
base: "https://www.independent.co.uk",
},
{
name: "BBC",
address: "https://www.bbc.com/news",
base: "https://www.bbc.com",
},
{
name: "CNN",
address: "https://edition.cnn.com/business",
base: "https://edition.cnn.com",
},
];
const cryptoNews = [];
cryptoPages.forEach((website) => {
axios
.get(website.address)
.then((response) => {
const html = response.data;
const $ = cheerio.load(html);
$('a:contains("crypto")', html).each(function () {
const title = $(this).text();
const url = $(this).attr("href");
cryptoNews.push({
title,
url:
url.includes("www") || url.includes("http")
? url
: website.base + url,
});
});
})
.catch((e) => console.log(e));
});
app.get("/", (req, res) => {
res.json("This is an API for crypto news!!");
});
app.get("/cryptonews", (req, res) => {
res.json(cryptoNews);
});