mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
теперь можно отключить сбор фиата, удален мусор, удаления питоновской функции через регулярное выражение
This commit is contained in:
parent
615f3be62a
commit
b90a969a1c
1 changed files with 9 additions and 24 deletions
53
utils/save_fiat.js
Normal file
53
utils/save_fiat.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const pool = require('../postgresql.js');
|
||||
const yaml = require('yaml');
|
||||
const fs = require('fs');
|
||||
const axios = require('axios');
|
||||
const config = yaml.parse(fs.readFileSync('./config.yaml', 'utf-8'));
|
||||
|
||||
async function save_fiat() {
|
||||
if (!config['currency']['collecting']['fiat']) return;
|
||||
|
||||
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])
|
||||
console.log(data)
|
||||
|
||||
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]) {
|
||||
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()
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = save_fiat;
|
Loading…
Add table
Reference in a new issue