-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
372 lines (308 loc) · 10.8 KB
/
Copy pathscript.js
File metadata and controls
372 lines (308 loc) · 10.8 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const generalBtn = document.getElementById("general");
const businessBtn = document.getElementById("business");
const sportsBtn = document.getElementById("sports");
const technologyBtn = document.getElementById("technology");
const entertainmentBtn = document.getElementById("entertainment");
const healthBtn = document.getElementById("health");
const scienceBtn = document.getElementById("science");
const searchBtn = document.getElementById("searchBtn");
const newsRoomBtn = document.getElementById("newsRoomBtn");
const newsQuery = document.getElementById("newsQuery");
const newsType = document.getElementById("newsType");
const newsDetails = document.getElementById("newsDetails");
// Array
let newsDataArr = [];
// api's
const headlines_news = "https://saurav.tech/NewsAPI/everything/cnn.json";
const general_news =
"https://saurav.tech/NewsAPI/top-headlines/category/general/in.json";
const business_news =
"https://saurav.tech/NewsAPI/top-headlines/category/business/in.json";
const sports_news =
"https://saurav.tech/NewsAPI/top-headlines/category/sports/in.json";
const entertainment_news =
"https://saurav.tech/NewsAPI/top-headlines/category/entertainment/in.json";
const technology_news =
"https://saurav.tech/NewsAPI/top-headlines/category/technology/in.json";
const health_news =
"https://saurav.tech/NewsAPI/top-headlines/category/health/in.json";
const science_news =
"https://saurav.tech/NewsAPI/top-headlines/category/science/in.json";
const search_news = "https://saurav.tech/NewsAPI/everything/cnn.json";
const news_room = "https://saurav.tech/NewsAPI/everything/cnn.json";
window.onload = function () {
newsType.innerHTML = "<h4>Headlines</h4>";
fetchHeadlines();
};
// adding event listeners
newsRoomBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Headlines</h4>";
fetchNewsRoom();
});
generalBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>General news</h4>";
// below is another function that is getting called.
fetchGeneralNews();
});
businessBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Business news</h4>";
fetchBusinessNews();
});
sportsBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Sports news</h4>";
fetchSportsNews();
});
entertainmentBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Entertainment news</h4>";
fetchEntertainmentNews();
});
technologyBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Technology news</h4>";
fetchTechnologyNews();
});
healthBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Health news</h4>";
fetchHealthNews();
});
scienceBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Science news</h4>";
fetchScienceNews();
});
// searchBtn.addEventListener("click",function(){
// newsType.innerHTML="<h4>Search : " +newsQuery.value+ "</h4>";
// fetchQueryNews();
// });
// var myJson;
// myJson.articles.slice(0,12);
const fetchHeadlines = async () => {
const response = await fetch(headlines_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
// fetching the API's.
const fetchGeneralNews = async () => {
const response = await fetch(general_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchBusinessNews = async () => {
const response = await fetch(business_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchNewsRoom = async () => {
const response = await fetch(news_room);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchEntertainmentNews = async () => {
const response = await fetch(entertainment_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchSportsNews = async () => {
const response = await fetch(sports_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchTechnologyNews = async () => {
const response = await fetch(technology_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchHealthNews = async () => {
const response = await fetch(health_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchScienceNews = async () => {
const response = await fetch(science_news);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
// handle errors
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
const fetchQueryNews = async () => {
if (newsQuery.value == null) return;
const response = await fetch(
search_news + encodeURIComponent(newsQuery.value)
);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
console.log(myJson);
newsDataArr = myJson.articles.slice(0, 12);
} else {
//error handle
console.log(response.status, response.statusText);
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
displayNews();
};
function displayNews() {
newsDetails.innerHTML = "";
if (newsDataArr.length == 0) {
newsDetails.innerHTML = "<h5>No data found.</h5>";
return;
}
newsDataArr.forEach((news) => {
// dynamic html.
let date = news.publishedAt.split("T");
let col = document.createElement("div");
col.className = "col-sm-12 col-md-6 col-lg-4 p-3 mt-4 m-0 card";
let card = document.createElement("div");
card.className = "p-2";
let image = document.createElement("img");
image.setAttribute("height", "matchparent");
image.setAttribute("width", "100%");
image.src = news.urlToImage;
let cardBody = document.createElement("div");
let newsHeading = document.createElement("h5");
newsHeading.className = "card-title mt-4";
newsHeading.innerHTML = news.title;
let dateHeading = document.createElement("h6");
dateHeading.className = "text-primary";
dateHeading.innerHTML = date[0];
let description = document.createElement("p");
description.className = "text-muted";
description.innerHTML = news.description;
let link = document.createElement("a");
link.className = "btn btn-dark btn-sm";
link.setAttribute("target", "_blank");
link.href = news.url;
link.innerHTML = "Read more...";
let pagin = document.createElement("div");
cardBody.appendChild(newsHeading);
cardBody.appendChild(dateHeading);
cardBody.appendChild(description);
cardBody.appendChild(link);
card.appendChild(image);
card.appendChild(cardBody);
col.appendChild(card);
newsDetails.appendChild(col);
});
}
// let currentCategory="general";
// let count=2;
// localStorage
// function getAPIData(currentCategory) {
// const API_LINK = `https://saurav.tech/NewsAPI/top-headlines/category/${currentCategory}/in.json`;
// fetch(API_LINK)
// .then((response) => response.json())
// .then((data) => displayNews(data.articles))
// .catch((e) => console.log(e));
// }
// window.addEventListener("DOMContentLoaded", getAPIData(currentCategory));
// console.log(currentCategory);
// function getCategory() {
// const categoryArray = document.querySelectorAll(".category-item");
// console.log("Hello", categoryArray);
// categoryArray.forEach((item) => {
// console.log(item);
// item.addEventListener("click", function (e) {
// currentCategory = e.currentTarget.dataset.id;
// getAPIData(currentCategory);
// });
// });
// }
// //getAPIData(currentCategory);
// getCategory();
// function displayNews(articles) {
// document.getElementById("heading").innerHTML=currentCategory.toUpperCase();
// const plainCard = articles.map((art,i)=>{
// return `<div class="col-md-4">
// <h4>${art.title}</h4><p>
// <img src=${art.urlToImage} width=95% height=200/>
// ${art.description}</p></div>`;
// });
// document.getElementById("main-card").innerHTML = plainCard.join("");
// }