-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (78 loc) · 2.68 KB
/
Copy pathindex.js
File metadata and controls
90 lines (78 loc) · 2.68 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express')
const axios = require('axios')
const cheerio = require('cheerio')
require('dotenv').config({ path: '.env' })
const PORT = process.env.PORT || 9000
const app = express()
const newspapers = [
{
name: 'Times of India',
address: 'https://timesofindia.indiatimes.com/topic/climate%20change',
baseAddress: 'https://timesofindia.indiatimes.com'
},
{
name: 'India Today',
address: 'https://www.indiatoday.in/topic/climate-change',
baseAddress: 'https://www.indiatoday.in'
},
{
name: 'The Hindu',
address: 'https://www.thehindu.com/sci-tech/energy-and-environment',
baseAddress: 'https://www.thehindu.com'
},
{
name: 'Hindustan Times',
address: 'https://www.hindustantimes.com/ht-insight/climate-change',
baseAddress: 'https://www.hindustantimes.com'
},
{
name: 'Newslaundry',
address: 'https://www.newslaundry.com/reports',
baseAddress: 'https://www.newslaundry.com'
}
]
const keyword = 'climate'
const articles = []
newspapers.forEach(newspaper => {
axios.get(newspaper.address)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
$(`a:contains(${keyword})`, html).each(function () {
const title = $(this).text();
let url = $(this).attr('href')
if (!url.startsWith('http') || !url.startsWith('https')) {
url = newspaper.baseAddress + url
}
articles.push({
title,
url,
source: newspaper.name
})
})
}).catch(err => console.log(err))
})
function removeDuplicateObjFromArrObj(arrObj) {
let arrStr = arrObj.map(obj => JSON.stringify(obj))
let arrSetStr = Array.from(new Set(arrStr)) // OR [...new Set(arrStr)]
let arrSetObj = arrSetStr.map(data => JSON.parse(data))
return arrSetObj;
// return Array.from(new Set(arrObj.map(obj => JSON.stringify(obj)))).map(data => JSON.parse(data));
// return [...new Set(listObj.map(obj => JSON.stringify(obj)))].map(data => JSON.parse(data));
}
const message = {
"message": "Welcome to Climate Change News API",
"metadata": {
"info": "News Article links",
"_href": "/news"
}
}
app.get('/', (req, res) => {
res.json(message)
})
app.get('/news', (req, res) => {
// To remove duplicate object from an array-of-object
let articlesData = removeDuplicateObjFromArrObj(articles)
res.json(articlesData)
})
app.listen(PORT, () => console.log(`Server running on port : ${PORT}`))