-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (103 loc) · 2.78 KB
/
index.js
File metadata and controls
122 lines (103 loc) · 2.78 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
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
const PORT = process.env.PORT || 5000
const express = require('express');
const cheerio = require('cheerio');
const axios = require('axios');
const { response } = require('express');
const app = express()
const newspapers = [
{
name: 'DailySmarty',
address: 'https://www.dailysmarty.com/topics/javascript',
base: 'https://www.dailysmarty.com'
},
{
name: 'DeveloperTech',
address: 'https://developer-tech.com/categories/developer-languages/developer-languages-javascript/',
base: ''
},
{
name: 'BleepingComputer',
address: 'https://www.bleepingcomputer.com/tag/javascript/',
base: ''
},
{
name: 'GoogleNews',
address: 'https://news.google.com/search?q=Javascript&hl=en-US&gl=US&ceid=US%3Aen',
base: 'https://news.google.com'
},
{
name: 'InfoWorld',
address: 'https://www.infoworld.com/category/javascript/',
base: 'https://www.infoworld.com'
},
{
name: 'ChangeLog',
address: 'https://changelog.com/topic/javascript',
base: ''
},
{
name: 'WisdomGeek',
address: 'https://www.wisdomgeek.com/',
base: ''
},
{
name: 'Dev',
address: 'https://dev.to/latest',
base: ''
},
{
name: 'InfoQ',
address: 'https://www.infoq.com/javascript/news/',
base: 'https://infoq.com'
},
{
name: 'TechXplore',
address: 'https://techxplore.com/software-news/',
base: ''
},
]
const articles = []
newspapers.forEach(newspaper => {
axios.get(newspaper.address)
.then(response => {
const html = response.data
const $ = cheerio.load(html)
$('a:contains("JavaScript")', 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 My Javascript News API')
})
app.get('/news', (req, res) => {
res.json(articles);
})
app.get('/news/:newspaperId', (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("JavaScript")', html).each(function () {
const title = $(this).text()
const url = $(this).attr('href')
specificArticles.push({
title,
url: newspaperBase + url,
source: newspaperId
})
})
res.json(specificArticles)
})
})
app.listen(PORT, () => console.log(`server running on ${PORT}`))