-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
253 lines (161 loc) · 5.88 KB
/
script.js
File metadata and controls
253 lines (161 loc) · 5.88 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js";
import { getAuth, onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-auth.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-analytics.js";
const firebaseConfig = {
apiKey: "AIzaSyD4hql8ApZFCZXTCqAswRXNY9AQ7TEZMCs",
authDomain: "news-app-6fb3f.firebaseapp.com",
projectId: "news-app-6fb3f",
storageBucket: "news-app-6fb3f.firebasestorage.app",
messagingSenderId: "1013648179384",
appId: "1:1013648179384:web:7a00150cac5e10f940b44d",
measurementId: "G-JJH9XLVB3Y"
};
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth(app);
onAuthStateChanged(auth, (user) => {
if (user) {
console.log("User is logged in:", user);
} else {
if (window.location.pathname == '/index.html') {
alert("You need to log in first!");
window.location.href = "index.html";
}
}
});
// Logout function
const logout = () => {
signOut(auth)
.then(() => {
alert("User logged out");
window.location.href = "index.html"; // Redirect to login page
})
.catch((error) => {
console.error("Error:", error.message);
alert(error.message);
});
};
let log_out=document.querySelector("#logout");
log_out.addEventListener('click',()=>{
logout();
});
const BASE_URL="https://newsapi.org/v2/top-headlines?country=us&category=general&apiKey=931db298d52a40acaaee96cf9ab116a9"
let box=document.querySelector('.box'); //category part
let new_country="us";
let category_list = document.querySelector('#exampleFormControlSelect1');
let category=category_list.value;
category_list.addEventListener('change',()=>{
category=category_list.value;
console.log(category);
box.innerHTML='';
news();
})
console.log(category);
let arr=[];
let final_tone;
let preference_div=document.querySelector(".preferences");
console.log(preference_div);
let preferences_button=document.querySelectorAll('.genre');//this is my preference selector part
console.log(preferences_button);
preferences_button.forEach((e)=>{
e.addEventListener("click",()=>{
arr.push(e.value);
e.classList.add("btn-warning");
console.log(arr);
});
})
let proceed=document.querySelector('#proceed');
console.log(proceed);
proceed.addEventListener('click',()=>{
proceed.style.display='none';
preferences_button.forEach((e)=>{
e.style.display="none";
});
let tone= document.querySelectorAll('.tone');
tone.forEach((e)=>{
e.style.display="flex";
e.addEventListener('click',()=>{
final_tone=e.innerText;
news();
});
});
box.innerHTML='';
for(let i in arr){
category=arr[i];
console.log(category);
news();
}
});
pre_nav_btn.addEventListener('click', () => {
if (preference_div.style.display === 'none') {
preference_div.style.display = 'grid'; // Show preferences div
} else {
preference_div.style.display = 'none'; // Hide preferences div
}
});
const news=async()=>{//the news function
const new_BASE_URL=`https://newsapi.org/v2/top-headlines?country=us&category=${category}&apiKey=931db298d52a40acaaee96cf9ab116a9`;
let response=await fetch(new_BASE_URL);
let data= await response.json();
data.articles.forEach(travel);
function travel(need,i){
let news_title=data.articles[i].title;
let url_href=data.articles[i].url;
let img_url=data.articles[i].urlToImage;
let content=data.articles[i].content;
if(final_tone!=undefined){
const options = {
method: "POST",
url: "https://api.edenai.run/v2/text/sentiment_analysis",
headers: {
authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiM2Q0ZTBjZDEtYjMwMy00NzU0LTk0NmItYzc2MzVmYzk4ZWViIiwidHlwZSI6ImFwaV90b2tlbiJ9.IyMlTuLI7ipESCCGPPMY5wRWrWxcvzLkADG6dfm7UTI",
},
data: JSON.stringify({
providers: ["google"],
text: content,
language: "en",
}),
};
axios(options)
.then((response) => {
// console.log(response.data);
})
.catch((error) => {
// console.error(error);
});
if(final_tone===options.general_sentiment){
cardMaker();
}
}
else{
cardMaker();
}
function cardMaker(){
let card=document.createElement("div");
card.classList.add("card");
card.style.width="30rem";
card.style.backgroundColor="#17202a";
document.body.appendChild(card);
if(img_url){
let image=document.createElement('img');
image.classList.add("card-img-fluid");
image.src=img_url;
card.appendChild(image);
}
let title=document.createElement("h5");
title.classList.add("card-title");
title.innerText=news_title;
title.style.color="white";
card.appendChild(title);
let link=document.createElement("button");
link.classList.add("btn");
link.classList.add("btn-warning");
link.innerText="Read the Complete Page here"
link.addEventListener('click',()=>{
window.location.href = url_href;
});
card.appendChild(link);
box.appendChild(card);
}
}}
news();