-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
252 lines (220 loc) · 7.16 KB
/
news.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const axios = require("axios");
const JSSoup = require("jssoup").default;
let newsObj = {};
const filterNews = (news) => {
const reg = /Who|What|How|Here|This|These|Watch|quiz|\?$|\.{3,6}/;
if (!reg.test(news)) return true;
else false;
};
const formatNews = (news) => {
news = news.replace(/"|'/g, ""); //remove quotation marks
news = news.replace(/'|"|"|'/g, ""); //remove quotation marks (in html format)
news = news.replace(/\t|\n/g, ""); //remove \t and \n
news = news.replace(/\s{2,6}/g, "'"); //remove multiple spaces
return news;
};
const engadget = async () => {
console.log("Fetching news from engadget");
let news = [];
try {
const response = await axios.get("https://www.engadget.com/");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headingBlock = soup.find("div", { id: "module-latest" });
let headings = headingBlock.findAll("h2");
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["engadget"] = news;
};
const gadgets_ndtv = async () => {
console.log("Fetching news from gadgets_ndtv");
let news = [];
try {
const response = await axios.get("https://gadgets.ndtv.com/news");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("span", "news_listing");
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["gadgets-ndtv"] = news;
};
const gadgets_now = async () => {
console.log("Fetching news from gadgets_now");
let news = [];
try {
const response = await axios.get("https://www.gadgetsnow.com/tech-news");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("figcaption"); //getting global div which is holding all news heading
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["gadgets-now"] = news;
};
const inshorts = async () => {
console.log("Fetching news from inshorts");
let news = [];
try {
const response = await axios.get("https://inshorts.com/en/read/technology");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("span", { itemProp: "headline" });
for (let heading of headings) {
heading = heading.text; //get text of the span element
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["inshorts"] = news;
};
const techcrunch = async () => {
console.log("Fetching news from techcrunch");
let news = [];
try {
const response = await axios.get("https://techcrunch.com/");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("h2", "post-block__title");
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["techcrunch"] = news;
};
const xda_developers = async () => {
console.log("Fetching news from xda_developers");
let news = [];
try {
const response = await axios.get(
"https://www.xda-developers.com/category/xda-news/"
);
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("div", "item_content");
for (let heading of headings) {
heading = heading.find("h4").text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["xda-developers"] = news;
};
const mobile_reuters = async () => {
console.log("Fetching news from mobile_reuters");
let news = [];
try {
const response = await axios.get("https://mobile.reuters.com/technology");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("a", "media-story-card__heading__eqhp9");
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["mobile-reuters"] = news;
};
const india = async () => {
console.log("Fetching news from india");
let news = [];
try {
const response = await axios.get("https://www.india.com/technology/");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("h3");
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["india"] = news;
};
const beebom = async () => {
console.log("Fetching news from beebom");
let news = [];
try {
const response = await axios.get("https://beebom.com/category/news/");
let htmlContent = response.data; //data field has html code
//scraping..
let soup = new JSSoup(htmlContent);
let headings = soup.findAll("h3", "entry-title");
for (let heading of headings) {
heading = heading.text;
heading = formatNews(heading);
if (filterNews(heading)) news.push(heading);
}
} catch (err) {
console.log(err);
if (news.length == 0) news.push("None");
}
newsObj["beebom"] = news;
};
const about = async () => {
newsObj["about"] = {
info: "this is a unofficial news-api from different popular tech news websites. Feel free to give it a star or add new website",
"my-github": "https://github.com/Shubhamrawat5",
"news-api-github": "https://github.com/Shubhamrawat5/news-api",
};
};
module.exports.getNews = async () => {
console.log("GETTING NEWS!!!");
const gNdtv = gadgets_ndtv();
const gNow = gadgets_now();
const inshts = inshorts();
const reuters = mobile_reuters();
const info = about();
// const crunch = techcrunch();
// const xda = xda_developers();
// const ind = india();
const bee = beebom();
const engad = engadget();
await Promise.all([gNdtv, gNow, inshts, reuters, info, bee, engad]);
return newsObj;
};