-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewsupdate.html
More file actions
314 lines (261 loc) · 10.2 KB
/
newsupdate.html
File metadata and controls
314 lines (261 loc) · 10.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Disaster Alert System - News Updates</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
:root {
--primary: #e74c3c;
--secondary: #3498db;
--dark: #2c3e50;
--light: #ecf0f1;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
background-color: #f5f5f5;
color: #333;
line-height: 1.6;
}
.container {
max-width: 1000px;
margin: 2rem auto;
padding: 0 1.5rem;
}
.controls {
background: white;
padding: 1.5rem;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
margin-bottom: 2rem;
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
select, button {
padding: 0.8rem 1.2rem;
border-radius: 6px;
border: 1px solid #ddd;
font-size: 1rem;
}
button {
background-color: var(--primary);
color: white;
border: none;
cursor: pointer;
transition: background 0.3s;
display: flex;
align-items: center;
gap: 0.5rem;
}
button:hover {
background-color: #c0392b;
}
.news-container {
background: white;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
overflow: hidden;
}
.news-header {
padding: 1rem 1.5rem;
background-color: var(--dark);
color: white;
display: flex;
justify-content: space-between;
align-items: center;
}
.news-item {
padding: 1.5rem;
border-bottom: 1px solid #eee;
transition: background 0.3s;
}
.news-item:hover {
background-color: #f9f9f9;
}
.news-item:last-child {
border-bottom: none;
}
.news-title {
color: var(--dark);
margin-bottom: 0.5rem;
font-size: 1.2rem;
}
.news-desc {
color: #666;
margin-bottom: 1rem;
}
.news-meta {
display: flex;
justify-content: space-between;
color: #777;
font-size: 0.9rem;
}
.news-source {
font-weight: 600;
color: var(--secondary);
}
.read-more {
color: var(--primary);
text-decoration: none;
font-weight: 600;
}
.loading {
padding: 2rem;
text-align: center;
color: #777;
}
.error {
color: var(--primary);
padding: 1.5rem;
text-align: center;
}
@media (max-width: 768px) {
.controls {
flex-direction: column;
}
select, button {
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<div class="controls">
<select id="disasterTypeFilter">
<option value="">All Disaster Types</option>
</select>
<select id="regionFilter">
<option value="">All Regions</option>
</select>
<button id="refreshUpdates">
<i class="fas fa-sync-alt"></i>
Refresh Updates
</button>
</div>
<div class="news-container">
<div class="news-header">
<h2><i class="fas fa-newspaper"></i> Latest Disaster Reports</h2>
<div id="lastUpdated">Last updated: -</div>
</div>
<div id="newsFeed">
<div class="loading">
<i class="fas fa-spinner fa-spin"></i> Loading disaster updates...
</div>
</div>
</div>
</div>
<script>
// News API Configuration
const NEWS_API_KEY = 'a8bccd912c7f4cca81254588ad081783'; // Replace with your actual API key
const DISASTER_KEYWORDS = 'disaster OR earthquake OR flood OR wildfire OR hurricane OR cyclone OR tsunami';
const newsCache = {};
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
setupEventListeners();
fetchDisasterNewsUpdates();
});
function setupEventListeners() {
document.getElementById('refreshUpdates').addEventListener('click', fetchDisasterNewsUpdates);
document.getElementById('disasterTypeFilter').addEventListener('change', fetchDisasterNewsUpdates);
document.getElementById('regionFilter').addEventListener('change', fetchDisasterNewsUpdates);
}
async function fetchDisasterNewsUpdates() {
const disasterType = document.getElementById('disasterTypeFilter').value;
const region = document.getElementById('regionFilter').value;
const newsFeed = document.getElementById('newsFeed');
const cacheKey = `${disasterType}-${region}`;
// Show loading state
newsFeed.innerHTML = '<div class="loading"><i class="fas fa-spinner fa-spin"></i> Loading disaster updates...</div>';
// Check cache first (5 minute cache)
if (newsCache[cacheKey] && Date.now() - newsCache[cacheKey].timestamp < 300000) {
displayNewsUpdates(newsCache[cacheKey].data);
return;
}
try {
let url = `https://newsapi.org/v2/everything?q=${encodeURIComponent(DISASTER_KEYWORDS)}&sortBy=publishedAt&pageSize=10&apiKey=${NEWS_API_KEY}`;
if (disasterType) {
url += `+${encodeURIComponent(disasterType)}`;
}
if (region) {
url += `&language=en&country=${region}`;
}
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.status !== 'ok' || !data.articles) {
throw new Error(data.message || 'No articles found');
}
// Cache the results
newsCache[cacheKey] = {
data: data.articles,
timestamp: Date.now()
};
displayNewsUpdates(data.articles);
} catch (error) {
console.error('Failed to fetch news:', error);
showErrorMessage(error.message);
}
}
function displayNewsUpdates(articles) {
const newsFeed = document.getElementById('newsFeed');
if (!articles || articles.length === 0) {
newsFeed.innerHTML = `
<div class="error">
<i class="fas fa-exclamation-triangle"></i>
No recent disaster reports found. Try refreshing or changing filters.
</div>
`;
updateLastUpdated();
return;
}
newsFeed.innerHTML = articles.map(article => `
<div class="news-item">
<h3 class="news-title">${article.title || 'No title available'}</h3>
<p class="news-desc">${article.description || 'No description available'}</p>
<div class="news-meta">
<span class="news-source">
<i class="fas fa-newspaper"></i> ${article.source?.name || 'Unknown source'}
</span>
<span class="news-time">
<i class="far fa-clock"></i> ${formatDate(article.publishedAt)}
</span>
</div>
<a href="${article.url}" target="_blank" class="read-more">
Read full report <i class="fas fa-external-link-alt"></i>
</a>
</div>
`).join('');
updateLastUpdated();
}
function showErrorMessage(message) {
const newsFeed = document.getElementById('newsFeed');
newsFeed.innerHTML = `
<div class="error">
<i class="fas fa-exclamation-triangle"></i>
${message || 'Failed to load updates'}
</div>
`;
updateLastUpdated();
}
function updateLastUpdated() {
document.getElementById('lastUpdated').textContent =
`Last updated: ${new Date().toLocaleString()}`;
}
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
}
</script>
</body>
</html>