-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (92 loc) · 2.88 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
const PORT = process.env.PORT || 9001
const express = require('express')
const axios = require('axios')
const cheerio = require('cheerio')
const app = express()
const newspapers = [
{
name: 'folha',
address: 'https://www1.folha.uol.com.br/folha-topicos/agricultura/#40',
base: ''
},
{
name: 'emais-estadao',
address: 'https://emais.estadao.com.br/blogs/alimentos-organicos/',
base: ''
},
{
name: 'saude-estadao',
address: 'https://saude.estadao.com.br/blogs/organicos/',
base: '',
},
{
name: 'exame',
address: 'https://exame.com/noticias-sobre/produtos-organicos/',
base: 'https://www.exame.com',
},
{
name: 'agrolink',
address: 'https://www.agrolink.com.br/noticias/',
base: '',
},
{
name: 'gauchazh',
address: 'https://gauchazh.clicrbs.com.br/ultimas-noticias/tag/alimentos-organicos/',
base: '',
},
{
name: 'gov',
address: 'https://www.gov.br/agricultura/pt-br/assuntos/sustentabilidade/organicos/noticias-organicos',
base: '',
},
{
name: 'g1',
address: 'https://g1.globo.com/economia/agronegocios/',
base: ''
}
]
const articles = []
newspapers.forEach(newspaper => {
axios.get(newspaper.address)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
$('a:contains("orgânico")', 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('Welcome to Organic News Live API')
})
app.get('/news', (req, res) => {
res.json(articles)
})
app.get('/news/:newsId', (req, res) => {
const newspaperId = req.params.newspaperId
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()
const url = $(this).attr('href')
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))