-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.js
292 lines (243 loc) · 9.51 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
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
// variables
const generalBtn = document.getElementById("general");
const businessBtn = document.getElementById("business");
const sportsBtn = document.getElementById("sport");
const entertainmentBtn = document.getElementById("entertainment");
const technologyBtn = document.getElementById("technology");
const stocksBtn = document.getElementById("stocks");
const cryptoBtn = document.getElementById("crypto");
const headlinesBtn = document.getElementById("headlines");
const countryBtn = document.getElementsByClassName("country");
const searchBtn = document.getElementById("searchBtn");
const newsQuery = document.getElementById("newsQuery");
const newsType = document.getElementById("newsType");
const newsdetails = document.getElementById("newsdetails");
var country="in";
var countryhtml="India";
// Array
var newsDataArr = [];
datePickerId.max = new Date().toISOString().split("T")[0];
datePickerId1.max = new Date().toISOString().split("T")[0];
datePickerId.value = new Date().toISOString().split("T")[0];
datePickerId1.value = new Date().toISOString().split("T")[0];
var currentDate = new Date().toISOString().split("T")[0];
const datefrm = document.getElementById("datePickerId1");
const dateto = document.getElementById("datePickerId");
console.log(datefrm.value);
console.log(currentDate);
// apis
const API_KEY = "1d0aab6daec4437a8af1b64bae66506f";
window.onload = function () {
newsType.innerHTML = "<h4>Headlines "+countryhtml+"</h4>";
fetchHeadlines1();
};
headlinesBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Headlines "+countryhtml+"</h4>";
fetchHeadlines1();
});
generalBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>General news "+countryhtml+"</h4>";
fetchGeneralNews();
});
businessBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Business "+countryhtml+"</h4>";
fetchBusinessNews();
});
sportsBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Sports "+countryhtml+"</h4>";
fetchSportsNews();
});
entertainmentBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Entertainment "+countryhtml+"</h4>";
fetchEntertainmentNews();
});
technologyBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Technology "+countryhtml+"</h4>";
fetchTechnologyNews();
});
stocksBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>stocks</h4>";
fetchstocksNews();
});
cryptoBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>crypto</h4>";
fetchcryptoNews();
});
searchBtn.addEventListener("click", function () {
newsType.innerHTML = "<h4>Search : " + newsQuery.value + "</h4>";
fetchQueryNews();
});
const date1 = async() => {
datePickerId.min = datefrm.value;
}
const fetchHeadlines = async (e) => {
console.log(e.target.value);
countryhtml=e.target.innerHTML;
newsType.innerHTML = "<h4>Headlines "+countryhtml+"</h4>";
country=e.target.value;
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchHeadlines1 = async () => {
newsType.innerHTML = "<h4>Headlines "+countryhtml+"</h4>";
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchcryptoNews = async () => {
const response = await fetch("https://newsapi.org/v2/everything?q=crypto&from="+datefrm.value+"&to="+dateto.value+"&apiKey="+API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchstocksNews = async () => {
console.log(datefrm.value);
const response = await fetch("https://newsapi.org/v2/everything?q=stocks&from="+datefrm.value+"&to="+dateto.value+"&apiKey="+API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchGeneralNews = async () => {
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&category=general&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchBusinessNews = async () => {
console.log(country)
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&category=business&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchEntertainmentNews = async () => {
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&category=entertainment&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchSportsNews = async () => {
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&category=sports&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchTechnologyNews = async () => {
const response = await fetch("https://newsapi.org/v2/top-headlines?country=" + country + "&category=technology&pageSize=8&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
const fetchQueryNews = async () => {
if (newsQuery.value == null)
return;
const response = await fetch("https://newsapi.org/v2/everything?q=" + encodeURIComponent(newsQuery.value) + "&from=" + datefrm.value + "&to="+dateto.value+"&apiKey=" + API_KEY);
newsDataArr = [];
if (response.status >= 200 && response.status < 300) {
const myJson = await response.json();
newsDataArr = myJson.articles;
} else {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
displayNews();
}
function displayNews() {
console.log(newsDataArr)
newsdetails.innerHTML = "";
if(newsDataArr.length == 0) {
newsdetails.innerHTML = "<h5>No data found.</h5>"
return;
}
newsDataArr.forEach(news => {
var date = news.publishedAt.split("T");
var col = document.createElement('div');
col.className = "col-sm-12 col-md-4 col-lg-3 p-2 card mb-3";
var card = document.createElement('div');
card.className = "p-2";
var image = document.createElement('img');
image.setAttribute("height", "matchparent");
image.setAttribute("width", "100%");
image.src = news.urlToImage;
var cardBody = document.createElement('div');
var newsHeading = document.createElement('h5');
newsHeading.className = "card-title text-dark";
newsHeading.innerHTML = news.title;
var dateHeading = document.createElement('h6');
dateHeading.className = "text-primary";
dateHeading.innerHTML = date[0];
var discription = document.createElement('p');
discription.className = "text-muted";
discription.innerHTML = news.description;
var link = document.createElement('a');
link.className = "btn btn-dark";
link.setAttribute("target", "_blank");
link.href = news.url;
link.innerHTML = "Read more";
cardBody.appendChild(newsHeading);
cardBody.appendChild(dateHeading);
cardBody.appendChild(discription);
cardBody.appendChild(link);
card.appendChild(image);
card.appendChild(cardBody);
col.appendChild(card);
newsdetails.appendChild(col);
});
}