From fd5411b96dabd6c82e75c6a272be82e28589a9af Mon Sep 17 00:00:00 2001 From: root Date: Mon, 13 Jan 2025 03:08:57 +0300 Subject: [PATCH] script for every day repl --- repl/fetch_past.js | 39 +++++++++++++++++++ repl/package-lock.json | 85 ++++++++++++++++++++++++++++++++++++++++++ repl/package.json | 4 +- repl/select.js | 3 +- 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 repl/fetch_past.js diff --git a/repl/fetch_past.js b/repl/fetch_past.js new file mode 100644 index 0000000..cbf13a9 --- /dev/null +++ b/repl/fetch_past.js @@ -0,0 +1,39 @@ +import sqlite3 from 'sqlite3' +import fetch from 'node-fetch' +sqlite3.verbose() +const db = new sqlite3.Database('../db') + +function formatDate(date) { + let day = date.getDate() + let month = date.getMonth() + 1 + let year = date.getFullYear() + + day = day < 10 ? '0' + day : day + month = month < 10 ? '0' + month : month + + return `${day}-${month}-${year}` +} + +async function fetchAndInsert(stmt, asset) { + const assets = { + 'TRX': 'tron', + 'ETH': 'ethereum' + } + + const date = new Date(Number(new Date()) - 24 * 3600 * 1000) + const formattedDate= formatDate(date) + const response = await fetch(`https://api.coingecko.com/api/v3/coins/${assets[asset]}/history?date=${formattedDate}`) + const data = await response.json() + const price = Number(data.market_data.current_price.rub) + stmt.run(asset, price, formattedDate) + console.log(asset, price, date) +} + +async function main() { + const stmt = db.prepare("INSERT INTO assets VALUES (?, ?, ?)") + await fetchAndInsert(stmt, 'ETH') + await fetchAndInsert(stmt, 'TRX') + stmt.finalize() +} + +main() diff --git a/repl/package-lock.json b/repl/package-lock.json index 0a94bd4..1c945bb 100644 --- a/repl/package-lock.json +++ b/repl/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "node-fetch": "^3.3.2", "sqlite3": "^5.1.7" } }, @@ -289,6 +290,14 @@ "license": "ISC", "optional": true }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "engines": { + "node": ">= 12" + } + }, "node_modules/debug": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.0.tgz", @@ -399,12 +408,45 @@ "node": ">=6" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", "license": "MIT" }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/fs-constants": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", @@ -897,6 +939,41 @@ "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==", "license": "MIT" }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, "node_modules/node-gyp": { "version": "8.4.1", "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.1.tgz", @@ -1430,6 +1507,14 @@ "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "license": "MIT" }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "engines": { + "node": ">= 8" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/repl/package.json b/repl/package.json index 6834032..78ef424 100644 --- a/repl/package.json +++ b/repl/package.json @@ -10,6 +10,8 @@ "license": "ISC", "description": "", "dependencies": { + "node-fetch": "^3.3.2", "sqlite3": "^5.1.7" - } + }, + "type": "module" } diff --git a/repl/select.js b/repl/select.js index dc70d2f..c51060d 100644 --- a/repl/select.js +++ b/repl/select.js @@ -1,4 +1,5 @@ -const sqlite3 = require('sqlite3').verbose() +import sqlite3 from 'sqlite3' +sqlite3.verbose() const db = new sqlite3.Database('../db') db.serialize(async () => {