mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
Ранее выбивалась ошибка из-за таймаута (> 3с) и часть валюты не сохранялась. Исправлено, небольшой рефакторинг кода
This commit is contained in:
parent
2c9a28f80b
commit
403c471db0
1 changed files with 23 additions and 25 deletions
|
@ -13,40 +13,38 @@ 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) return;
|
if(value === pair) return;
|
||||||
const res = await axios.get(
|
await axios.get(
|
||||||
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
|
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
|
||||||
{
|
{
|
||||||
timeout: 3000,
|
timeout: 3000,
|
||||||
});
|
}).then(async (res) => {
|
||||||
|
const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg');
|
||||||
|
const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1]);
|
||||||
|
|
||||||
const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg');
|
delete data['terms'];
|
||||||
const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1]);
|
delete data['privacy'];
|
||||||
|
|
||||||
delete data['terms'];
|
logger.debug(JSON.stringify(data));
|
||||||
delete data['privacy'];
|
|
||||||
|
|
||||||
logger.debug(JSON.stringify(data));
|
const point = data['to'][0]['mid'].toString().indexOf('.') + 4;
|
||||||
|
|
||||||
if (res.status !== 200) {
|
const db = await pool.query('SELECT * FROM currency WHERE ' +
|
||||||
logger.error(`DuckDuckGo ended with the status ${res.status}`);
|
'from_currency = $1 AND conv_currency = $2 AND date = $3',
|
||||||
return;
|
[ value, pair, new Date(data['timestamp']).toLocaleDateString() ]);
|
||||||
}
|
|
||||||
|
|
||||||
const point = data['to'][0]['mid'].toString().indexOf('.') + 4;
|
if (db['rows'][0]) return;
|
||||||
|
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) VALUES ($1, $2, $3, $4)`,
|
||||||
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,
|
||||||
if (db['rows'][0]) return;
|
data['to'][0]['mid'].toString().slice(0, point),
|
||||||
|
new Date(data['timestamp']).toLocaleDateString()
|
||||||
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) VALUES ($1, $2, $3, $4)`,
|
]);
|
||||||
[
|
}).catch((err) => {
|
||||||
value,
|
logger.error(err)
|
||||||
pair,
|
setTimeout(save_fiat, err.config.timeout);
|
||||||
data['to'][0]['mid'].toString().slice(0, point),
|
|
||||||
new Date(data['timestamp']).toLocaleDateString()
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue