-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
424 lines (236 loc) · 9.79 KB
/
Copy pathscript.js
File metadata and controls
424 lines (236 loc) · 9.79 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
let date = document.getElementById("date");
let time = document.getElementById("time");
let temperature = document.getElementById("temperature");
let articles_container = document.getElementById("articles-container");
let search_input = document.getElementById("search-input");
let loader_1 = document.getElementById("loader-1");
let loader_2 = document.getElementById("loader-2");
let category_buttons = document.querySelectorAll(".category-button");
let indian_news_container = document.getElementById("indian-news-container");
// ------------------------------------------------------------------------------------------------
const news_key = "61da66450c8e42fdbf543e90f2d33c1a";
const news_url = `https://newsapi.org/v2/top-headlines?language=en&pageSize=50&apiKey=${news_key}`;
let articles_collection = [];
// ------------------------------------------------------------------------------------------------
window.addEventListener("load", () => {
let get_date = new Date();
date.textContent = get_date.toDateString();
setInterval(() => {
let get_time = new Date();
time.textContent = get_time.toLocaleTimeString();
}, 1000);
setTimeout(() => {
get_weather();
}, 1000);
start_loading_news_feed();
start_loading_indian_news();
setTimeout(() => {
load_news();
indian_news();
}, 1000);
});
// --------------------------------------------------------------------------------------------------
async function get_weather() {
let weather_key = "4ec67467a9b9f37556f4543c9a731e20";
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(async (position) => {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
let weather_url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${weather_key}&units=metric`;
try {
let promise = await fetch(weather_url);
let data = await promise.json();
temperature.textContent = data.main.temp + " °C";
} catch (error) {
temperature.textContent = "Failed To Load Temperature!";
}
}, async (error) => {
temperature.textContent = "Location Denied By User";
});
}
else {
temperature.textContent = "Failed To Locate Your Location!";
}
};
// ------------------------------------------------------------------------------------------------
function start_loading_news_feed() {
articles_container.innerHTML = "";
loader_1.style.display = "inline-block";
};
function stop_loading_news_feed() {
loader_1.style.display = "none";
};
function start_loading_indian_news() {
indian_news_container.innerHTML = "";
loader_2.style.display = "inline-block";
};
function stop_loading_indian_news() {
loader_2.style.display = "none";
};
// ------------------------------------------------------------------------------------------------
async function load_news() {
try {
let promise = await fetch(news_url);
let data = await promise.json();
articles_collection = data.articles;
load_articles(articles_collection);
}
catch (error) {
articles_container.textContent = "Failed To Load News! Please Try Again Later!";
}
finally {
stop_loading_news_feed();
}
};
// ------------------------------------------------------------------------------------------------
function load_articles(articles) {
articles.forEach((article) => {
let article_element = document.createElement('article');
let image = document.createElement("img");
image.src = article.urlToImage;
image.alt = "Image Not Available";
let title = document.createElement('h3');
let link = document.createElement("a");
link.textContent = article.title || "No Title Available!";
link.href = article.url || "#";
link.target = "_blank";
title.appendChild(link);
let hr = document.createElement('hr');
let description = document.createElement('p');
description.textContent = article.description || "No Description Available!";
article_element.appendChild(image);
article_element.appendChild(title);
article_element.appendChild(hr);
article_element.appendChild(description);
articles_container.appendChild(article_element);
});
};
// ------------------------------------------------------------------------------------------------
function debounce(search_function, delay) {
let time_out;
return function (...arguments) {
clearTimeout(time_out);
time_out = setTimeout(() => {
search_function.apply(this, arguments);
}, delay);
};
};
// ------------------------------------------------------------------------------------------------
let debounced_search_news = debounce(search_news, 1000);
let debounce_categoriize_news = debounce(categorize_news, 1000);
// ------------------------------------------------------------------------------------------------
search_input.addEventListener("input", () => {
start_loading_news_feed();
debounced_search_news();
});
// ------------------------------------------------------------------------------------------------
async function search_news() {
let search_query = search_input.value.toLowerCase();
if (search_query != "") {
try {
let search_url = `https://newsapi.org/v2/everything?q=${encodeURIComponent(search_query)}&language=en&pageSize=50&apiKey=${news_key}`;
let promise = await fetch(search_url);
let data = await promise.json();
if (data.articles.length == 0) {
articles_container.textContent = "No Articles Found!";
}
else {
load_articles(data.articles);
}
}
catch (error) {
articles_container.textContent = "Failed To Search News! Please Try Again Later!";
}
}
else {
load_articles(articles_collection);
}
stop_loading_news_feed();
};
// ------------------------------------------------------------------------------------------------
category_buttons.forEach((button) => {
button.addEventListener("click", () => {
let selected_category = button.getAttribute("data-category");
start_loading_news_feed();
debounce_categoriize_news(selected_category);
highlight_active_category(button);
});
});
// ------------------------------------------------------------------------------------------------
async function categorize_news(category) {
if (category === "all") {
load_articles(articles_collection);
}
else {
try {
let category_url = `https://newsapi.org/v2/top-headlines?country=us&pageSize=50&category=${category}&apiKey=${news_key}`;
let promise = await fetch(category_url);
let data = await promise.json();
if (data.articles.length == 0) {
articles_container.textContent = "No Articles Found In This Category!";
}
else {
load_articles(data.articles);
}
}
catch (error) {
articles_container.textContent = "Failed To Categorize News! Please Try Again Later!";
}
}
stop_loading_news_feed();
};
// ------------------------------------------------------------------------------------------------
function highlight_active_category(active_button) {
category_buttons.forEach((btn) => {
btn.classList.remove("active-category");
});
active_button.classList.add("active-category");
};
// ------------------------------------------------------------------------------------------------
async function indian_news() {
try {
let indian_url = `https://newsapi.org/v2/everything?q=india&language=en&pageSize=10&apiKey=${news_key}`;
let promise = await fetch(indian_url);
let data = await promise.json();
if (data.articles.length == 0) {
indian_news_container.textContent = "No Articles Found!";
}
else {
let sorted_articles = data.articles.sort((a, b) => {
let date_a = new Date(a.publishedAt);
let date_b = new Date(b.publishedAt);
return date_b - date_a;
});
load_indian_news(sorted_articles);
}
}
catch (error) {
indian_news_container.textContent = "Failed To Find Indian News! Please Try Again Later!"
}
finally {
stop_loading_indian_news();
}
};
// ------------------------------------------------------------------------------------------------
function load_indian_news(articles) {
articles.forEach((article) => {
let article_element = document.createElement('article');
let image = document.createElement("img");
image.src = article.urlToImage;
image.alt = "Image Not Available";
let title = document.createElement('h3');
let link = document.createElement("a");
link.textContent = article.title || "No Title Available!";
link.href = article.url || "#";
link.target = "_blank";
title.appendChild(link);
let hr = document.createElement('hr');
let description = document.createElement('p');
description.textContent = article.description || "No Description Available!";
article_element.appendChild(image);
article_element.appendChild(title);
article_element.appendChild(hr);
article_element.appendChild(description);
indian_news_container.appendChild(article_element);
});
};