Spaces:
Sleeping
Sleeping
let tweets = []; | |
function updateStats() { | |
document.getElementById('active-disasters').textContent = | |
tweets.filter(t => t.isDisaster).length; | |
document.getElementById('urgent-cases').textContent = | |
tweets.filter(t => t.sentiment === 'Urgent').length; | |
document.getElementById('total-tweets').textContent = tweets.length; | |
} | |
function getSentimentClass(sentiment) { | |
switch(sentiment) { | |
case 'Urgent': return 'sentiment-urgent'; | |
case 'Neutral': return 'sentiment-neutral'; | |
case 'Not Urgent': return 'sentiment-not-urgent'; | |
default: return ''; | |
} | |
} | |
function formatTimestamp(timestamp) { | |
return new Date(timestamp).toLocaleTimeString(); | |
} | |
function renderTweets() { | |
const feed = document.getElementById('tweet-feed'); | |
if (tweets.length === 0) { | |
feed.innerHTML = '<p>No new tweets available. This may be due to rate limiting or no recent disaster-related tweets.</p>'; | |
} else { | |
feed.innerHTML = tweets.map(tweet => ` | |
<div class="tweet"> | |
<div class="tweet-header"> | |
<strong>${tweet.isDisaster ? 'Disaster Alert' : 'Tweet'}</strong> | |
<span class="sentiment-badge ${getSentimentClass(tweet.sentiment)}"> | |
${tweet.sentiment.toUpperCase()} | |
</span> | |
</div> | |
<div class="tweet-content"> | |
${tweet.text} | |
</div> | |
<div class="tweet-footer"> | |
<div class="location"> | |
π ${tweet.location} | |
</div> | |
<div style="margin-left: auto"> | |
${formatTimestamp(tweet.timestamp)} | |
</div> | |
</div> | |
</div> | |
`).join(''); | |
} | |
} | |
function fetchTweets() { | |
fetch('/api/predict') | |
.then(response => response.json()) | |
.then(data => { | |
if (data.error) { | |
console.error('Error:', data.error); | |
return; | |
} | |
const newTweets = JSON.parse(data.data); | |
tweets = [...newTweets, ...tweets].slice(0, 100); // Keep only the latest 100 tweets | |
updateStats(); | |
renderTweets(); | |
}) | |
.catch(error => console.error('Error:', error)); | |
} | |
// Fetch tweets every 5 minutes (300000 ms) | |
setInterval(fetchTweets, 300000); | |
// Initial fetch | |
fetchTweets(); | |