From 61fc956e1966f2aaa9e4d4ce34526cb8a3902540 Mon Sep 17 00:00:00 2001 From: Redume Date: Sat, 17 Feb 2024 11:34:17 +0300 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B4?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D1=8F=D1=8E=D1=82=D1=81=D1=8F=20=D0=B2=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/saveCourse.js | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/utils/saveCourse.js b/utils/saveCourse.js index 6f55d82..96b1c2d 100644 --- a/utils/saveCourse.js +++ b/utils/saveCourse.js @@ -5,20 +5,43 @@ const axios = require("axios"); const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8")); async function saveCourse() { - - let pairs = []; - let currencies = ["USD", "RUB", "UAH", "JDF"] config['currency'].forEach( (value) => config['currency'].forEach(async (pair) => { if(value !== pair) { const res = await axios.get( - `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}` + `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, + { + timeout: 3000 + } ) - console.log(res.data) + const data = JSON.parse(res.data.replace('ddg_spice_currency(', '').replace(');', '')) + delete data['terms']; + delete data['privacy']; + console.log(data) + + const point = data['to'][0]['mid'].toString().indexOf('.') + 4 + + pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2', + [ + value, + data['timestamp'] + ] + ).then(async (db) => { + if (!db['rows'][0]) { + await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) + VALUES ($1, $2, $3, $4) RETURNING *`, + [ + value, + pair, + data['to'][0]['mid'].toString().slice(0, point), + data['timestamp'] + ] + ) + } + }) } }) ); - } module.exports = saveCourse; \ No newline at end of file