-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (119 loc) · 3.6 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const PORT = 8000
const express = require('express')
const axios = require('axios')
const cheerio = require('cheerio')
const { response } = require('express')
const app = express()
const newspapers = [
{
name: 'The Times Of India',
address: 'https://timesofindia.indiatimes.com/',
base:''
},
{
name: 'ndtv',
address: 'https://www.ndtv.com/',
base:''
},
{
name: 'Hindustan Times',
address: 'https://www.hindustantimes.com/',
base:'https://www.hindustantimes.com'
},
{
name: 'Indian Epxress',
address: 'https://indianexpress.com/',
base:''
},
{
name: 'News 18',
address: 'https://www.news18.com/',
base:''
}
]
const articles = []
newspapers.forEach(newspaper => {
axios.get(newspaper.address)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
$('a:contains("India")', html).each(function() {
const title = $(this).text()
var url = ''
if(newspaper.name==='Hindustan Times') {
var aft = $(this).attr('href')
if(aft.startsWith('http',0)) {
url = aft;
} else {
url = newspaper.base + aft
}
} else{
url = $(this).attr('href')
}
articles.push({
title,
url,
source: newspaper.name
})
})
})
})
app.get('/', (req, res) => {
res.json("Welcome to the News API")
})
app.get('/news',(req,res) => {
res.json(articles)
})
app.get('/news/:newspaperId', (req,res) => {
const newspaperId = req.params.newspaperId
const newspaperBase = newspapers.filter(newspaper => newspaper.name === newspaperId)
console.log(newspaperBase)
axios.get(newspaperBase[0].address)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
const specart = []
$('a:contains("India")',html).each(function() {
const title = $(this).text()
const url = $(this).attr('href')
specart.push({
title,
url,
source: newspaperId
})
})
res.json(specart)
}).catch(err => console.log(err))
})
app.get('/news/st/climate', (req,res) => {
const spectoparticles = []
newspapers.forEach(newspaper => {
axios.get(newspaper.address)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
$('a:contains("climate")', html).each(function() {
const title = $(this).text()
var url = ''
if(newspaper.name==='Hindustan Times') {
var aft = $(this).attr('href')
if(aft.startsWith('http',0)) {
url = aft;
} else {
url = newspaper.base + aft
}
} else{
url = $(this).attr('href')
}
spectoparticles.push({
title,
url,
source: newspaper.name
})
})
})
})
console.log(spectoparticles)
res.json(spectoparticles)
})
app.listen(PORT,() => console.log(`Server running on port ${PORT}`))