-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (58 loc) · 2.21 KB
/
app.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
// Initialize the countdown timer to 1 hour (3600 seconds)
let countdown = 3600;
function updateTimer() {
const timerElement = document.getElementById('timer');
const minutes = Math.floor(countdown / 60);
const seconds = countdown % 60;
// Display the countdown in "MM:SS" format
timerElement.textContent = `Next update in: ${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
if (countdown > 0) {
countdown--;
} else {
// Reset the countdown timer and fetch stock data
countdown = 3600;
fetchStockData();
}
}
// Update the timer display every second
setInterval(updateTimer, 1000);
async function fetchStockData() {
// Using a CORS proxy
const proxyUrl = 'https://proxy.cors.sh/'; // Example proxy
const apiUrl = 'https://tradestie.com/api/v1/apps/reddit';
try {
const response = await fetch(proxyUrl + apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
console.log('API Data:', data); // Debugging: Check the data received from the API
if (!Array.isArray(data)) {
console.error('Unexpected API response format:', data);
return;
}
const stocks = data.slice(0, 50); // Get top 50 stocks
const tbody = document.querySelector('#stocksTable tbody');
tbody.innerHTML = ''; // Clear existing content
stocks.forEach((stock, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${index + 1}</td>
<td>${stock.ticker}</td>
<td>${stock.sentiment}</td>
<td>${stock.no_of_comments}</td>
`;
tbody.appendChild(row);
});
} catch (error) {
console.error('Error fetching stock data:', error);
}
}
// Fetch data every hour
fetchStockData();
setInterval(fetchStockData, 60 * 60 * 1000); // 1 hour interval