Не много уменьшил размер кода, добавил вывод в консоль если код статуса DDG не 200

This commit is contained in:
Данил 2024-08-11 17:23:23 +03:00
parent 98cb0aaad9
commit dba02b0490

View file

@ -1,6 +1,7 @@
const pool = require('../database/postgresql.js'); const pool = require('../database/postgresql.js');
const axios = require('axios'); const axios = require('axios');
const config = require('../config/main.js')(); const config = require('../config/main.js')();
const logger = require('../logger/main.js');
/** /**
* Saves exchange rate of the fiat currency * Saves exchange rate of the fiat currency
@ -11,7 +12,7 @@ async function save_fiat() {
config['currency']['fiat'].forEach( config['currency']['fiat'].forEach(
(value) => config['currency']['fiat'].forEach(async (pair) => { (value) => config['currency']['fiat'].forEach(async (pair) => {
if(value !== pair) { if(value === pair) return;
const res = await axios.get( const res = await axios.get(
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
{ {
@ -24,27 +25,28 @@ async function save_fiat() {
delete data['terms']; delete data['terms'];
delete data['privacy']; 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; 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', pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
[ [ value, pair, new Date(data['timestamp']).toLocaleDateString() ]
value, ).then(async (db) => {
pair,
new Date(data['timestamp']).toLocaleDateString()
]).then(async (db) => {
if (db['rows'][0]) return; if (db['rows'][0]) return;
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) VALUES ($1, $2, $3, $4)`,
VALUES ($1, $2, $3, $4) RETURNING *`,
[ [
value, value,
pair, pair,
data['to'][0]['mid'].toString().slice(0, point), data['to'][0]['mid'].toString().slice(0, point),
new Date(data['timestamp']).toLocaleDateString() new Date(data['timestamp']).toLocaleDateString()
], ]);
);
}); });
}
}) })
); );
} }