-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
38 lines (34 loc) · 1.08 KB
/
Copy pathdatabase.js
File metadata and controls
38 lines (34 loc) · 1.08 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
const { Client } = require('pg');
const fetch = require('node-fetch');
const dbConfig = {
host: 'localhost',
user: 'postgres',
port: 5432,
password: 'Insert',
database: 'Insert'
};
const formatDate = () => {
const now = new Date();
const day = String(now.getDate()).padStart(2, '0');
const month = String(now.getMonth() + 1).padStart(2, '0');
const year = now.getFullYear();
return `${day}-${month}-${year}`;
};
const logBitcoinPrice = async () => {
const client = new Client(dbConfig);
try {
await client.connect();
const response = await fetch('https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT');
const data = await response.json();
const price = Math.round(parseFloat(data.price));
const date = formatDate();
await client.query('INSERT INTO history (date, value) VALUES ($1, $2)', [date, price]);
console.log(`Logged BTC price ${price} USD on ${date}`);
} catch (err) {
console.error('Error logging price:', err.message);
} finally {
await client.end();
}
};
// Run every 10 seconds
setInterval(logBitcoinPrice, 10000);