added timeout for crypto api requests
This commit is contained in:
parent
7cdfa3d8b0
commit
3d53a6ced5
68
index.js
68
index.js
|
@ -1,37 +1,7 @@
|
|||
const sqlite3 = require('sqlite3').verbose();
|
||||
const db = new sqlite3.Database('./db');
|
||||
|
||||
const dates = `2024-08-04
|
||||
2024-08-05
|
||||
2024-08-06
|
||||
2024-08-07
|
||||
2024-08-08
|
||||
2024-08-09
|
||||
2024-08-10
|
||||
2024-08-11
|
||||
2024-08-12
|
||||
2024-08-13
|
||||
2024-08-14
|
||||
2024-08-15
|
||||
2024-08-16
|
||||
2024-08-17
|
||||
2024-08-18
|
||||
2024-08-19
|
||||
2024-08-20
|
||||
2024-08-21
|
||||
2024-08-22
|
||||
2024-08-23
|
||||
2024-08-24
|
||||
2024-08-25
|
||||
2024-08-26
|
||||
2024-08-27
|
||||
2024-08-28
|
||||
2024-08-29
|
||||
2024-08-30
|
||||
2024-08-31
|
||||
2024-09-01
|
||||
2024-09-02
|
||||
2024-09-03
|
||||
const dates = `2024-09-03
|
||||
2024-09-04
|
||||
2024-09-05
|
||||
2024-09-06
|
||||
|
@ -177,21 +147,35 @@ function formatDate(date) {
|
|||
return `${day}-${month}-${year}`;
|
||||
}
|
||||
|
||||
const wait = async function() {
|
||||
await new Promise((res) => {
|
||||
setTimeout(() => {
|
||||
res();
|
||||
}, 60000);
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
db.serialize(async () => {
|
||||
// db.run("CREATE TABLE assets (asset TEXT, price VALUE, date DATE)");
|
||||
|
||||
// const params = dates.split('\n');
|
||||
const params = dates.split('\n');
|
||||
|
||||
// for(param of params) {
|
||||
// const stmt = db.prepare("INSERT INTO assets VALUES (?, ?, ?)")
|
||||
// const date = new Date(param)
|
||||
// const formattedDate= formatDate(date);
|
||||
// const response = await fetch(`https://api.coingecko.com/api/v3/coins/ethereum/history?date=${formattedDate}`)
|
||||
// const data = await response.json()
|
||||
// const price = Number(data.market_data.current_price.rub)
|
||||
// stmt.run("ETH", price, date)
|
||||
// stmt.finalize();
|
||||
// }
|
||||
for(i in params) {
|
||||
if(i % 5 == 0) {
|
||||
await wait();
|
||||
}
|
||||
const param = params[i]
|
||||
const stmt = db.prepare("INSERT INTO assets VALUES (?, ?, ?)")
|
||||
const date = new Date(param)
|
||||
const formattedDate= formatDate(date);
|
||||
const response = await fetch(`https://api.coingecko.com/api/v3/coins/ethereum/history?date=${formattedDate}`)
|
||||
const data = await response.json()
|
||||
const price = Number(data.market_data.current_price.rub)
|
||||
stmt.run("ETH", price, date)
|
||||
stmt.finalize();
|
||||
console.log(param);
|
||||
}
|
||||
|
||||
|
||||
db.each("SELECT * FROM assets", (err, row) => {
|
||||
|
|
Loading…
Reference in New Issue