-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (92 loc) · 2.71 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
const express=require('express');
const axios=require('axios');
const cheerio=require('cheerio');
const { prependTo } = require('cheerio/lib/api/manipulation');
const app=express();
const PORT=process.env.PORT || 4000;
const newspapers=[
{
name: 'guradian',
address: 'https://www.theguardian.com/environment/climate-crisis',
base:""
},
{
name:'telegraph',
address:'https://www.telegraphindia.com/topic/global-warming',
base:"https://www.telegraphindia.com"
},
{
name:'timesofindia',
address:'https://timesofindia.indiatimes.com/topic/climate-change/news',
base:"https://timesofindia.indiatimes.com"
},
]
const articles=[];
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()
const url=$(this).attr('href')
articles.push({
title,
url:newspaper.base+url,
source:newspaper.name
})
})
})
.catch((err)=>{console.log(err)})
})
const message=[
"welcome to climate chnage news api",
" for news visit for news api visit https://climatechnageneswapi.herokuapp.com/news",
"for news from specfic media house like timeofindia visit https://climatechnageneswapi.herokuapp.com/news/timesofindia"
];
app.get('/',(req,res)=>{
res.json(message)
})
app.get('/news',(req,res)=>{
res.json(articles)
})
app.get('/newsapi',(req,res)=>{
var options = {
method: 'GET',
url: 'https://bing-news-search1.p.rapidapi.com/news',
params: {safeSearch: 'Off', textFormat: 'Raw'},
headers: {
'x-bingapis-sdk': 'true',
'x-rapidapi-host': 'bing-news-search1.p.rapidapi.com',
'x-rapidapi-key': '034d333c60msh3be117efca9c3a3p145cacjsnc975a2c875a2'
}
};
axios.request(options).then(function (response) {
res.json(response.data);
}).catch(function (error) {
console.error(error);
});
})
app.get('/news/:newspaperid',async (req,res)=>{
const specficarticles=[];
const newspaperid=req.params.newspaperid;
const newspaper=newspapers.filter(newspaper=>newspaper.name==newspaperid)[0]
const newspaperaddress=newspaper.address;
axios.get(newspaperaddress)
.then((response)=>{
const html=response.data;
const $=cheerio.load(html)
$('a:contains("climate")',html).each(function(){
const title=$(this).text()
const url=$(this).attr('href')
specficarticles.push({
title,
url:newspaper.base+url,
source:newspaper.name
})
})
res.json(specficarticles);
})
.catch((err)=>{console.log(err)})
})
app.listen(PORT,()=>{console.log("server start")});