-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (145 loc) · 5.79 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
import express from 'express';
import axios from 'axios';
import cheerio from 'cheerio';
const PORT = 3000;
const app = express();
const baseUrl = 'https://www.bbc.com';
const newspaths = [
{ title: "climate", path: "/news/science-environment-56837908" },
{ title: "War in Ukraine", path: "/news/world-60525350" },
{ title: "coronavirus", path: "/news/coronavirus" },
{ title: "world", path: "/news/world" },
{ title: "uk", path: "/news/uk" },
{ title: "business", path: "/news/business" },
{ title: "tech", path: "/news/technology" },
{ title: "science", path: "/science_and_environment" },
{ title: "health", path: "/news/health" },
{ path: "/news/world/africa", title: "africa" },
{ path: "/news/world/australia", title: "australia" },
{ path: "/news/world/europe", title: "europe" },
{ path: "/news/world/asia", title: "asia" },
{ path: "/news/world/latin_america", title: "latin america" },
{ path: "/news/world/middle_east", title: "middle east" },
{ path: "/news/world/us_and_canada", title: "us and canada" },
{ path: "/news/entertainment_and_arts", title: "entertainment and arts" },
{ path: "/news/newsbeat", title: "newsbeat" },
];
app.get('/', (req, res) => {
res.json({ message: 'Hello World🌏' });
});
app.get('/all', async (req, res) => {
try {
const response = await axios.get(`${baseUrl}`);
const html = response.data;
const $ = cheerio.load(html);
const news = [];
$('li.media-list__item', html).each(function () {
const result = {
title: $('h3', this).text().trim(),
summary: $('p', this).text().trim(),
url: baseUrl + $('a', this).attr('href'),
imageUrl: $('img', this).attr('src'),
id: $('a', this).attr('href')?.split('/').pop(),
category: $('a.media__tag', this).text().trim(),
timeStamp: $('time',this).attr('data-datetime')
}
news.push( result );
});
res.status(200).json(news);
} catch (error) {
res.status(500).json(error.message);
}
});
app.get('/news', async (req, res) => {
try {
const response = await axios.get(`${baseUrl}/news`);
const html = response.data;
const $ = cheerio.load(html);
const news = [];
$('div.gs-c-promo', html).each(function () {
const result = {
title: $('h3', this).text().trim(),
summary: $('p', this).text().trim(),
url: baseUrl + $('a', this).attr('href'),
imageUrl: $('img', this).attr('src'),
id: $('a', this).attr('href')?.split('/').pop(),
category: $('a>span:first', this).text().trim(),
timeStamp: $('time', this).attr('datetime')
}
news.push(result);
});
res.status(200).json(news);
} catch (error) {
res.status(500).json(error.message);
}
});
app.get('/news/:category', async (req, res) => {
const { category } = req.params;
const path = newspaths.filter((path) => path.title === category.toLowerCase())[0]?.path;
if (!path) {
res.status(404).json({ message: "Not found" });
return;
}
try {
const response = await axios.get(`${baseUrl}${path}`);
const html = response.data;
const $ = cheerio.load(html);
const news = [];
$('div.gs-c-promo', html).each(function () {
const result = {
title: $('h3', this).text().trim(),
summary: $('p', this).text().trim(),
url: baseUrl + $('a', this).attr('href'),
imageUrl: $('img', this).attr('src'),
id: $('a', this).attr('href')?.split('/').pop(),
category: $('a>span:first', this).text().trim(),
timeStamp: $('time', this).attr('datetime')
}
news.push(result);
});
res.status(200).json(news);
} catch (error) {
res.status(500).json(error.message);
}
});
app.get('/sport', async (req, res) => {
try {
const response = await axios.get(`${baseUrl}/sport`);
const html = response.data;
const $ = cheerio.load(html);
const news = [];
$('div [type=article]', html).each(function () {
const title = $('p', this).text().trim();
const url = baseUrl + $('a', this).attr('href');
const imageUrl = $('img', this).attr('src');
const id = $('a', this).attr('href')?.split('/').pop();
const timeStamp = $('span.e1y6uwnp0', this).text();
const sport = $('span.ecn1o5v1:first', this).text().trim();
news.push({ title, url, imageUrl, id, timeStamp, sport });
});
res.status(200).json(news);
} catch (error) {
res.status(500).json(error.message);
}
});
app.get('/sport/:sportName', async (req, res) => {
const { sportName } = req.params;
try {
const response = await axios.get(`${baseUrl}/sport/${sportName}`);
const html = response.data;
const $ = cheerio.load(html);
const news = [];
$('div.gs-c-promo', html).each(function () {
const url = baseUrl + $('a', this).attr('href');
const title = $('h3', this).text().trim();
const imageUrl = $('img', this).attr('src');
const id = $('a', this).attr('href')?.split('/').pop();
const timeStamp = $('time', this).attr('data-datetime');
news.push({ title, url, imageUrl, id, timeStamp, category: sportName });
});
res.status(200).json(news);
} catch (error) {
res.status(500).json(error.message);
}
});
app.listen(PORT, () => console.log(`Server running on 🚀 http://localhost:${PORT}`));