-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (149 loc) · 5.98 KB
/
script.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
let url = 'https://newsdata.io/api/1/news?apikey=';
let API_KEY = apiKeyData[0];
let api_key_tracker = 0;
let nextPageId = null;
async function fetchNews(category,requiredArticles=5,json=[]) {
localStorage.removeItem("newsArticles");
loadBtn = document.querySelector("#loadMoreBtn");
document.getElementById("footer").removeAttribute("style");
document.getElementById("main").innerHTML = "";
loadBtn.innerHTML = "Loading";
let result;
if(category == "Latest News"){
result = await fetch(url + API_KEY + `&country=in&language=en`);
}
else{
result = await fetch(url + API_KEY + `&country=in&language=en&category=`+category);
console.log("Fetch : Category");
}
let response_code = result.status;
let data = await result.json();
console.log("FetchNews : ",data);
console.log("Response Code : ",response_code);
if(response_code == 429){
api_key_tracker = (api_key_tracker + 1) % 42;
API_KEY = apiKeyData[api_key_tracker];
fetchNews(category);
}
else if(response_code == 422){
document.write("Something Went Wrong !!");
}
else{
if(data.nextPage != null){
nextPageId = data.nextPage;
loadBtn.innerHTML = "Load More";
loadBtn.setAttribute("style", "display : block");
}
else{
loadBtn.setAttribute("style","display : none");
nextPageId = null;
}
data = data.results;
let noOfArticles = 0;
for(let i=0; i<data.length; i++){
if (!(data[i].image_url === null || data[i].description === null || data[i].title === null || data[i].content === null)) {
noOfArticles++;
json.push(data[i]);
}
}
if((requiredArticles - noOfArticles > 0) && nextPageId != null){
loadMoreNews(category,requiredArticles-noOfArticles,json);
}
else{
toLocalStorage(json);
fillNews();
}
}
}
function fillNews() {
let data = JSON.parse(localStorage.getItem("newsArticles"));
document.getElementById("main").innerHTML = '';
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
let creatorDetails = data[i][j].creator;
if (creatorDetails == null)
creatorDetails = `<b>Anonymous</b> • `+ data[i][j].pubDate.slice(0, 10);
else
creatorDetails = `<b>`+data[i][j].creator+`</b> • `+ data[i][j].pubDate.slice(0, 10);
// Id for news card elements
let news_article_id = data[i][j].article_id;
document.getElementById("main").innerHTML +=
`<div class="news-card" id='`+ news_article_id + `'onclick="document.location.href='`+data[i][j].link+`'">
<div class="news-card-left">
<div class="news-provider-info">`+creatorDetails+`</div>
<div class="news-title">
<p>`+ data[i][j].title + `</p>
</div>
<div class="news-description">
<p>`+ data[i][j].description.slice(0,200) + `...` + `</p>
</div>
</div>
<div class="news-card-right">
<img src=`+ data[i][j].image_url + ` alt="News-Image" class="news-image">
</div>
</div>`
}
}
document.getElementById("goToTopIcon").innerHTML = `<a href="#"><i class="fa fa-arrow-up" aria-hidden="true"></i></a>`;
document.getElementById("loadMoreBtn").setAttribute("onclick",`loadMoreNews("`+document.getElementById(currentCategory).innerHTML+`")`);
document.getElementById("footer").setAttribute("style","display : block");
}
function toLocalStorage(data) {
let storedData = localStorage.getItem("newsArticles");
if (storedData === null) {
// If no data exists, create a new array and store it in localStorage
localStorage.setItem("newsArticles", JSON.stringify([data]));
} else {
// If data exists, parse it, update the array, and store it back
let existingData = JSON.parse(storedData);
existingData.push(data);
localStorage.setItem("newsArticles", JSON.stringify(existingData));
}
}
async function loadMoreNews(category,requiredArticles=5,json=[]) {
let loadBtn = document.querySelector("#loadMoreBtn");
document.getElementById("footer").removeAttribute("style");
loadBtn.innerHTML = "Loading";
let result;
if(category == "Latest News")
result = await fetch(url + API_KEY + `&country=in&language=en&page=`+nextPageId);
else
result = await fetch(url + API_KEY + `&country=in&language=en&category=`+category+`&page=`+nextPageId);
let response_code = result.status;
let data = await result.json();
if(response_code == 429){
api_key_tracker = (api_key_tracker + 1) % 42;
API_KEY = apiKeyData[api_key_tracker];
loadMoreNews(category,requiredArticles,json);
}
else if(response_code == 422){
document.write("Something Went Wrong !!");
}
else{
console.log("Load More data : ",data);
if(data.nextPage != null){
nextPageId = data.nextPage;
loadBtn.setAttribute("style", "display : block");
loadBtn.innerHTML = "Load More";
}
else{
nextPageId = null;
loadBtn.setAttribute("style","display : none");
}
data = data.results;
let noOfArticles = 0;
for(let i=0; i<data.length; i++){
if (!(data[i].image_url === null || data[i].description === null || data[i].title === null || data[i].content === null)) {
noOfArticles++;
json.push(data[i]);
}
}
if((requiredArticles - noOfArticles > 0) && nextPageId != null){
loadMoreNews(category,requiredArticles-noOfArticles,json);
}
else{
toLocalStorage(json);
fillNews();
}
}
}