db replenishment scripts

This commit is contained in:
Serafim 2025-01-13 01:15:31 +03:00
parent 3d53a6ced5
commit ddea84624b
7 changed files with 82 additions and 33 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
node_modules */node_modules

BIN
db

Binary file not shown.

6
repl/create.js Normal file
View File

@ -0,0 +1,6 @@
const sqlite3 = require('sqlite3').verbose()
const db = new sqlite3.Database('../db')
db.serialize(async () => {
db.run("CREATE TABLE assets (asset TEXT, price VALUE, date DATE)")
})

View File

@ -1,7 +1,43 @@
const sqlite3 = require('sqlite3').verbose(); const sqlite3 = require('sqlite3').verbose()
const db = new sqlite3.Database('./db'); const db = new sqlite3.Database('../db')
const dates = `2024-09-03 const dates =
`2024-07-30
2024-07-31
2024-08-01
2024-08-02
2024-08-03
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
2024-09-04 2024-09-04
2024-09-05 2024-09-05
2024-09-06 2024-09-06
@ -131,54 +167,51 @@ const dates = `2024-09-03
2025-01-08 2025-01-08
2025-01-09 2025-01-09
2025-01-10 2025-01-10
2025-01-11` 2025-01-11
2025-01-12`
function formatDate(date) { function formatDate(date) {
// Extract the day, month, and year from the date let day = date.getDate()
let day = date.getDate(); // Get the day let month = date.getMonth() + 1
let month = date.getMonth() + 1; // Get the month (0-based index, so add 1) let year = date.getFullYear()
let year = date.getFullYear(); // Get the full year
// Add leading zero to day and month if needed day = day < 10 ? '0' + day : day
day = day < 10 ? '0' + day : day; month = month < 10 ? '0' + month : month
month = month < 10 ? '0' + month : month;
// Return the formatted date return `${day}-${month}-${year}`
return `${day}-${month}-${year}`;
} }
const wait = async function() { const wait = async function() {
await new Promise((res) => { await new Promise((res) => {
setTimeout(() => { setTimeout(() => {
res(); res()
}, 60000); }, 125000)
}) })
} }
db.serialize(async () => { 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(i in params) { for(i in params) {
if(i % 5 == 0) { if(i % 5 == 0) {
await wait(); await wait()
} }
const param = params[i] const param = params[i]
const stmt = db.prepare("INSERT INTO assets VALUES (?, ?, ?)")
const date = new Date(param) const date = new Date(param)
const formattedDate= formatDate(date); const formattedDate= formatDate(date)
const response = await fetch(`https://api.coingecko.com/api/v3/coins/ethereum/history?date=${formattedDate}`) const response = await fetch(`https://api.coingecko.com/api/v3/coins/ethereum/history?date=${formattedDate}`)
const data = await response.json() const data = await response.json()
const price = Number(data.market_data.current_price.rub)
stmt.run("ETH", price, date) try {
stmt.finalize(); const stmt = db.prepare("INSERT INTO assets VALUES (?, ?, ?)")
console.log(param); const price = Number(data.market_data.current_price.rub)
stmt.run("ETH", price, date)
stmt.finalize()
console.log(param)
} catch {
console.log(data)
break
}
} }
})
db.each("SELECT * FROM assets", (err, row) => {
console.log(row);
});
});

10
repl/select.js Normal file
View File

@ -0,0 +1,10 @@
const sqlite3 = require('sqlite3').verbose()
const db = new sqlite3.Database('../db')
db.serialize(async () => {
db.each("SELECT * FROM assets", (err, row) => {
row.date = new Date(row.date).toISOString()
row.date = row.date.substring(0, row.date.length - 14)
console.log(row)
})
})