mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 12:43:12 +03:00
Пофиксил ротацию ключей. Рефакторинг кода
This commit is contained in:
parent
dba02b0490
commit
b6d50bb4e1
1 changed files with 50 additions and 37 deletions
|
@ -1,53 +1,67 @@
|
||||||
const config = require('../config/main.js')();
|
const config = require('../config/main.js')();
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const pool = require('../database/postgresql.js');
|
const pool = require('../database/postgresql.js');
|
||||||
|
const logger = require('../logger/main.js');
|
||||||
|
|
||||||
|
const coinapiKeys = config['currency']['coinapiKeys'];
|
||||||
|
let apiKeyIndex = 0;
|
||||||
|
let depth = coinapiKeys.length;
|
||||||
|
|
||||||
/**
|
function save_crypto() {
|
||||||
* Saves exchange rate of the cryptocurrency
|
|
||||||
* @return {Object}
|
|
||||||
*/
|
|
||||||
function save_fiat() {
|
|
||||||
if (!config['currency']['collecting']['crypto']) return;
|
if (!config['currency']['collecting']['crypto']) return;
|
||||||
const depth = config['currency']['coinapiKeys']
|
if (coinapiKeys[apiKeyIndex] === undefined) return;
|
||||||
|
|
||||||
if (depth <= 0) new Error('Rate limit on all API tokens');
|
if (depth <= 0) {
|
||||||
|
logger.info('Rate limit on all coinapi API keys');
|
||||||
|
new Error('Rate limit on all coinapi API keys');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Active coinapi key: ${coinapiKeys[apiKeyIndex]} (${coinapiKeys.length-1} / ${apiKeyIndex})`);
|
||||||
|
|
||||||
config['currency']['crypto'].forEach(
|
config['currency']['crypto'].forEach(
|
||||||
(value) => config['currency']['crypto'].forEach(async (pair) => {
|
(value) => config['currency']['crypto'].forEach((pair) => {
|
||||||
if (value !== pair) {
|
if (value === pair) return;
|
||||||
const coinapiKey = [0];
|
|
||||||
const res = await axios.get(`https://rest.coinapi.io/v1/exchangerate/${value}/${pair}`, {
|
axios.get(`https://rest.coinapi.io/v1/exchangerate/${value}/${pair}`,
|
||||||
|
{
|
||||||
timeout: 3000,
|
timeout: 3000,
|
||||||
headers: {
|
headers: {
|
||||||
'X-CoinAPI-Key': config['currency']['coinapiKeys'][coinapiKey[0]],
|
'X-CoinAPI-Key': coinapiKeys[apiKeyIndex],
|
||||||
}
|
}
|
||||||
});
|
}).then(async (res) => {
|
||||||
|
|
||||||
if (res.status === 429) rotate_key(config['currency']['coinapiKeys'], coinapiKey);
|
|
||||||
|
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
const point = data['rate'].toString().indexOf('.') + 4;
|
const point = data['rate'].toString().indexOf('.') + 4;
|
||||||
|
|
||||||
pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
|
logger.debug(JSON.stringify(data));
|
||||||
|
|
||||||
|
const db = pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
|
||||||
[
|
[
|
||||||
value,
|
value,
|
||||||
pair,
|
pair,
|
||||||
new Date(data['time']).toLocaleDateString()
|
new Date(data['time']).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) RETURNING *`,
|
VALUES ($1, $2, $3, $4)`,
|
||||||
[
|
[
|
||||||
value,
|
value,
|
||||||
pair,
|
pair,
|
||||||
data['rate'].toString().slice(0, point),
|
data['rate'].toString().slice(0, point),
|
||||||
new Date(data['time']).toLocaleDateString()
|
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();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -55,12 +69,11 @@ function save_fiat() {
|
||||||
/**
|
/**
|
||||||
* Changing API keys
|
* Changing API keys
|
||||||
* @param {Array} list - List of all keys
|
* @param {Array} list - List of all keys
|
||||||
* @param active - Active API key
|
|
||||||
* @returns {number} - Outputs the number of the key that should work
|
* @returns {number} - Outputs the number of the key that should work
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function rotate_key(list, active) {
|
function rotate_key(list) {
|
||||||
return active[0] = (active + 1) % list.length;
|
apiKeyIndex = list.indexOf(coinapiKeys[apiKeyIndex]) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = save_fiat;
|
module.exports = save_crypto;
|
Loading…
Add table
Reference in a new issue