-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (132 loc) · 4.3 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
const PORT = process.env.PORT || 8000
const express = require('express')
const axios = require('axios')
const cheerio = require('cheerio')
const app = express()
const newspapers = [
{
name: 'cityam',
address: 'https://www.cityam.com/london-must-become-a-world-leader-on-climate-change-action/',
base: 'https://www.cityam.com'
},
{
name: 'thetimes',
address: 'https://www.thetimes.co.uk/environment/climate-change',
base: 'https://www.thetimes.co.uk'
},
{
name: 'guardian',
address: 'https://www.theguardian.com/environment/climate-crisis',
base: 'https://www.theguardian.com',
},
{
name: 'telegraph',
address: 'https://www.telegraph.co.uk/climate-change',
base: 'https://www.telegraph.co.uk',
},
{
name: 'nyt',
address: 'https://www.nytimes.com/international/section/climate',
base: 'https://www.nytimes.com',
},
{
name: 'latimes',
address: 'https://www.latimes.com/environment',
base: 'https://www.latimes.com',
},
{
name: 'smh',
address: 'https://www.smh.com.au/environment/climate-change',
base: 'https://www.smh.com.au',
},
// {
// name: 'un',
// address: 'https://www.un.org/climatechange',
// base: '',
// },
{
name: 'bbc',
address: 'https://www.bbc.co.uk/news/science_and_environment',
base: 'https://www.bbc.co.uk',
},
{
name: 'es',
address: 'https://www.standard.co.uk/topic/climate-change',
base: 'https://www.standard.co.uk'
},
{
name: 'sun',
address: 'https://www.thesun.co.uk/topic/climate-change-environment/',
base: 'https://www.thesun.co.uk'
},
// {
// name: 'dm',
// address: 'https://www.dailymail.co.uk/news/climate_change_global_warming/index.html',
// base: ''
// },
{
name: 'nyp',
address: 'https://nypost.com/tag/climate-change/',
base: 'https://nypost.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()
let url = $(this).attr('href')
if (url.startsWith("https")){
url = url
}
else{
url = newspaper.base+url
}
articles.push({
title,
url: newspaper.base + url,
source: newspaper.name
})
})
})
})
app.get('/', (req, res) => {
res.json('Welcome to my Climate Change News API')
})
app.get('/news', (req, res) => {
res.json(articles)
})
app.get('/news/:newspaperId', (req, res) => {
const newspaperId = req.params.newspaperId
if (newspapers.filter(newspaper => newspaper.name == newspaperId).length==0){
res.json("Newspaper ID wrong")
}
const newspaperAddress = newspapers.filter(newspaper => newspaper.name == newspaperId)[0].address
const newspaperBase = newspapers.filter(newspaper => newspaper.name == newspaperId)[0].base
axios.get(newspaperAddress)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
const specificArticles = []
$('a:contains("climate")', html).each(function () {
const title = $(this).text()
let url = $(this).attr('href')
if (url.startsWith("https")){
url = url
}
else{
url = newspaperBase+url
}
specificArticles.push({
title,
url: newspaperBase + url,
source: newspaperId
})
})
res.json(specificArticles)
}).catch(err => console.log(err))
})
app.listen(PORT, () => console.log(`server running on PORT ${PORT}`))