-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (61 loc) · 2.13 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
68
69
70
71
//require.paths.push('/Users/chand/lin/node_modules');
const PORT = process.env.PORT || 8000
const express = require("express");
const axios = require("axios");
const cheerio = require("cheerio");
const { response } = require("express");
const app = express();
const np = [{
name: 'toi', add: 'https://timesofindia.indiatimes.com/topic/plastic/news',
base: ''
},
{
name: 'thehindu', add: 'https://www.thehindu.com/tag/62824-870-684/',
base: ''
}
// {
// name: 'telegraph', add: 'https://www.telegraph.co.uk/plastic/',
// base: 'https://www.telegraph.co.uk'
// }
]
const articles = []
np.forEach(newspaper => {
axios.get(newspaper.add)
.then((response) => {
const html = response.data
const $ = cheerio.load(html)
$('a:contains("plastic")', html).each(function () {
const title = $(this).text()
const url = $(this).attr('href')
articles.push({
title,
url: newspaper.base + url,
source: newspaper.name
})
})
})
})
app.get('/', (req, res) => { res.json('wlcm to CCAPI') })
app.get('/news', (req, res) => { res.json(articles) })
app.get('/news/:newspaperId', (req, res) => {
const newspaperId = req.params.newspaperId
const newspaperAdd = np.filter(newspaper => newspaper.name == newspaperId)[0].add
const newspaperBase = np.filter(newspaper => newspaper.name == newspaperId)[0].base
axios.get(newspaperAdd)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
const specArticles = []
$('a:contains("plastic")', html).each(function () {
const title = $(this).text()
const url = $(this).attr('href')
specArticles.push({
title,
url: newspaperBase + url,
source: newspaperId
})
})
res.json(specArticles)
}).catch(err => console.log(err))
})
app.listen(PORT, () => console.log(`server running on ${PORT}`))