2024-03-14 11:18:50 +03:00
|
|
|
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'));
|
2024-02-15 22:19:33 +03:00
|
|
|
|
2024-06-25 01:10:49 +03:00
|
|
|
async function save_fiat() {
|
|
|
|
if (!config['currency']['collecting']['fiat']) return;
|
|
|
|
|
|
|
|
config['currency']['fiat'].forEach(
|
|
|
|
(value) => config['currency']['fiat'].forEach(async (pair) => {
|
2024-02-16 16:34:15 +03:00
|
|
|
if(value !== pair) {
|
|
|
|
const res = await axios.get(
|
2024-02-17 11:34:17 +03:00
|
|
|
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
|
|
|
|
{
|
2024-02-17 14:17:13 +03:00
|
|
|
timeout: 3000,
|
2024-02-17 11:34:17 +03:00
|
|
|
}
|
2024-02-17 14:17:13 +03:00
|
|
|
);
|
|
|
|
|
2024-06-25 01:10:49 +03:00
|
|
|
const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg');
|
|
|
|
const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1])
|
|
|
|
console.log(data)
|
2024-03-14 11:18:50 +03:00
|
|
|
|
2024-02-17 11:34:17 +03:00
|
|
|
delete data['terms'];
|
|
|
|
delete data['privacy'];
|
|
|
|
|
2024-02-17 14:17:13 +03:00
|
|
|
const point = data['to'][0]['mid'].toString().indexOf('.') + 4;
|
2024-02-17 11:34:17 +03:00
|
|
|
|
2024-02-18 14:23:12 +03:00
|
|
|
pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
|
2024-02-17 11:34:17 +03:00
|
|
|
[
|
|
|
|
value,
|
2024-02-18 14:23:12 +03:00
|
|
|
pair,
|
2024-02-17 14:17:13 +03:00
|
|
|
new Date(data['timestamp']).toLocaleDateString()
|
2024-02-17 11:34:17 +03:00
|
|
|
]
|
|
|
|
).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),
|
2024-02-17 14:17:13 +03:00
|
|
|
new Date(data['timestamp']).toLocaleDateString()
|
2024-02-17 11:34:17 +03:00
|
|
|
]
|
2024-02-17 14:17:13 +03:00
|
|
|
);
|
2024-02-17 11:34:17 +03:00
|
|
|
}
|
2024-02-17 14:17:13 +03:00
|
|
|
});
|
2024-02-16 16:34:15 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2024-02-15 22:19:33 +03:00
|
|
|
}
|
|
|
|
|
2024-06-25 01:10:49 +03:00
|
|
|
module.exports = save_fiat;
|