From dba02b0490a1dec926bd190c6cb3fa0d6c1d3077 Mon Sep 17 00:00:00 2001 From: Redume Date: Sun, 11 Aug 2024 17:23:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D1=83=D0=BC=D0=B5=D0=BD=D1=8C=D1=88=D0=B8=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=20=D0=BA=D0=BE=D0=B4=D0=B0,=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B2=D1=8B=D0=B2?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=81=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BA=D0=BE=D0=B4=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=B0=20DDG=20=D0=BD=D0=B5=20200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- collect-currency/save_fiat.js | 66 ++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/collect-currency/save_fiat.js b/collect-currency/save_fiat.js index 112edc8..c34d64c 100644 --- a/collect-currency/save_fiat.js +++ b/collect-currency/save_fiat.js @@ -1,6 +1,7 @@ const pool = require('../database/postgresql.js'); const axios = require('axios'); const config = require('../config/main.js')(); +const logger = require('../logger/main.js'); /** * Saves exchange rate of the fiat currency @@ -11,40 +12,41 @@ async function save_fiat() { config['currency']['fiat'].forEach( (value) => config['currency']['fiat'].forEach(async (pair) => { - if(value !== pair) { - const res = await axios.get( - `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, - { - timeout: 3000, - }); - - const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg'); - const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1]); - - delete data['terms']; - delete data['privacy']; - - const point = data['to'][0]['mid'].toString().indexOf('.') + 4; - - pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', - [ - value, - pair, - new Date(data['timestamp']).toLocaleDateString() - ]).then(async (db) => { - if (db['rows'][0]) return; - - 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), - new Date(data['timestamp']).toLocaleDateString() - ], - ); + if(value === pair) return; + const res = await axios.get( + `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, + { + timeout: 3000, }); + + const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg'); + const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1]); + + delete data['terms']; + delete data['privacy']; + + logger.debug(JSON.stringify(data)); + + if (res.status !== 200) { + logger.error(`DuckDuckGo ended with the status ${res.status}`); + return; } + + const point = data['to'][0]['mid'].toString().indexOf('.') + 4; + + pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', + [ value, pair, new Date(data['timestamp']).toLocaleDateString() ] + ).then(async (db) => { + if (db['rows'][0]) return; + + await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) VALUES ($1, $2, $3, $4)`, + [ + value, + pair, + data['to'][0]['mid'].toString().slice(0, point), + new Date(data['timestamp']).toLocaleDateString() + ]); + }); }) ); }