eslint, prettier fix

This commit is contained in:
Данил 2024-08-17 16:40:20 +03:00
parent bdc4450b72
commit 2679fa20d5
10 changed files with 284 additions and 208 deletions

View file

@ -8,7 +8,8 @@ const save_crypto = require('./save_crypto');
async function main() {
const config_schedule = config['currency']['collecting']['schedule'];
if (!config_schedule) throw new Error('The crontab schedule is not set');
if (!cron.isValidCron(config_schedule, {alias: true})) throw new Error('The crontab is invalid');
if (!cron.isValidCron(config_schedule, { alias: true }))
throw new Error('The crontab is invalid');
await save_fiat();
await save_crypto();
@ -21,4 +22,4 @@ async function main() {
main();
module.exports = {main};
module.exports = { main };

View file

@ -17,52 +17,62 @@ function save_crypto() {
return;
}
logger.info(`Active coinapi key: ${coinapiKeys[apiKeyIndex]} (${coinapiKeys.length-1} / ${apiKeyIndex})`);
logger.info(
`Active coinapi key: ${coinapiKeys[apiKeyIndex]} (${coinapiKeys.length - 1} / ${apiKeyIndex})`,
);
config['currency']['crypto'].forEach(
(value) => config['currency']['crypto'].forEach((pair) => {
config['currency']['crypto'].forEach((value) =>
config['currency']['crypto'].forEach((pair) => {
if (value === pair) return;
axios.get(`https://rest.coinapi.io/v1/exchangerate/${value}/${pair}`,
{
timeout: 3000,
headers: {
'X-CoinAPI-Key': coinapiKeys[apiKeyIndex],
}
}).then(async (res) => {
axios
.get(
`https://rest.coinapi.io/v1/exchangerate/${value}/${pair}`,
{
timeout: 3000,
headers: {
'X-CoinAPI-Key': coinapiKeys[apiKeyIndex],
},
},
)
.then(async (res) => {
const data = res.data;
const point = data['rate'].toString().indexOf('.') + 4;
logger.debug(JSON.stringify(data));
const db = await pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
const db = await pool.query(
'SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
[
value,
pair,
new Date(data['time']).toLocaleDateString()
]);
new Date(data['time']).toLocaleDateString(),
],
);
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)`,
[
value,
pair,
data['rate'].toString().slice(0, point),
new Date(data['time']).toLocaleDateString()
]);
}).catch((err) => {
if (err.response?.data.detail) logger.error(err.response.data.detail);
if (err.response?.data.status === 429) {
logger.info('CoinAPI rate limited, rotating token');
rotate_key(coinapiKeys);
depth--;
save_crypto();
}
});
})
[
value,
pair,
data['rate'].toString().slice(0, point),
new Date(data['time']).toLocaleDateString(),
],
);
})
.catch((err) => {
if (err.response?.data.detail)
logger.error(err.response.data.detail);
if (err.response?.data.status === 429) {
logger.info('CoinAPI rate limited, rotating token');
rotate_key(coinapiKeys);
depth--;
save_crypto();
}
});
}),
);
}
@ -76,4 +86,4 @@ function rotate_key(list) {
apiKeyIndex = list.indexOf(coinapiKeys[apiKeyIndex]) + 1;
}
module.exports = save_crypto;
module.exports = save_crypto;

View file

@ -10,43 +10,57 @@ const logger = require('../shared/logger/src/main.js');
async function save_fiat() {
if (!config['currency']['collecting']['fiat']) return;
config['currency']['fiat'].forEach(
(value) => config['currency']['fiat'].forEach(async (pair) => {
if(value === pair) return;
await axios.get(
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
{
timeout: 3000,
}).then(async (res) => {
config['currency']['fiat'].forEach((value) =>
config['currency']['fiat'].forEach(async (pair) => {
if (value === pair) return;
await axios
.get(
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
{
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 data = JSON.parse(
Array.from(res.data.matchAll(regExp))[0][1],
);
delete data['terms'];
delete data['privacy'];
logger.debug(JSON.stringify(data));
const point = data['to'][0]['mid'].toString().indexOf('.') + 4;
const point =
data['to'][0]['mid'].toString().indexOf('.') + 4;
const db = await pool.query('SELECT * FROM currency WHERE ' +
'from_currency = $1 AND conv_currency = $2 AND date = $3',
[ value, pair, new Date(data['timestamp']).toLocaleDateString() ]);
const db = await pool.query(
'SELECT * FROM currency WHERE ' +
'from_currency = $1 AND conv_currency = $2 AND date = $3',
[
value,
pair,
new Date(data['timestamp']).toLocaleDateString(),
],
);
if (db['rows'][0]) return;
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date) VALUES ($1, $2, $3, $4)`,
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()
]);
}).catch((err) => {
logger.error(err);
setTimeout(save_fiat, err.config.timeout);
});
})
new Date(data['timestamp']).toLocaleDateString(),
],
);
})
.catch((err) => {
logger.error(err);
setTimeout(save_fiat, err.config.timeout);
});
}),
);
}
module.exports = save_fiat;
module.exports = save_fiat;