Compare commits
No commits in common. "1a63649bef1362b9817a7aa0accf8347495db864" and "6125f99ec6fb9442c4f743fc3c6b084ba468573b" have entirely different histories.
1a63649bef
...
6125f99ec6
3 changed files with 15 additions and 34 deletions
|
@ -10,15 +10,6 @@
|
||||||
fiat: true
|
fiat: true
|
||||||
crypto: false
|
crypto: false
|
||||||
}
|
}
|
||||||
services: {
|
|
||||||
enabled: [
|
|
||||||
'coinmarketcap'
|
|
||||||
]
|
|
||||||
coinmarketcap: {
|
|
||||||
"api_key": TOKEN_COINMARKETCAP"
|
|
||||||
"base_url": https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fiat:
|
fiat:
|
||||||
[
|
[
|
||||||
USD,
|
USD,
|
||||||
|
|
30
main.js
30
main.js
|
@ -7,28 +7,26 @@ const { validateCurrency } = require('./models/Currency.js');
|
||||||
const { create_table, pool } = require('./database/data.js');
|
const { create_table, pool } = require('./database/data.js');
|
||||||
const config = require('./utils/load_config.js')();
|
const config = require('./utils/load_config.js')();
|
||||||
|
|
||||||
const services = [];
|
|
||||||
const servicesDir = path.join(__dirname, 'services');
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
if (!config['schedule'])
|
if (!config['schedule'])
|
||||||
throw new Error('The crontab schedule is not set.');
|
throw new Error('The crontab schedule is not set.');
|
||||||
else if (!cron.isValidCron(config['schedule'], { alias: true }))
|
else if (!cron.isValidCron(config['schedule'], { alias: true }))
|
||||||
throw new Error('The crontab is invalid.');
|
throw new Error('The crontab is invalid.');
|
||||||
|
|
||||||
|
const servicesDir = path.join(__dirname, 'services');
|
||||||
|
const serviceFiles = fs.readdirSync(servicesDir)
|
||||||
|
.filter(filename => filename.endsWith('.js'));
|
||||||
|
|
||||||
console.log('Loading services...');
|
const services = [];
|
||||||
config['currency']['services']['enabled'].forEach(serviceName => {
|
for (const file of serviceFiles) {
|
||||||
const servicePath = path.join(servicesDir, `${serviceName}.js`);
|
const filePath = path.join(servicesDir, file);
|
||||||
if (fs.existsSync(servicePath)) {
|
const moduleLoaded = require(filePath);
|
||||||
const serviceModule = require(servicePath);
|
|
||||||
|
|
||||||
services.push(serviceModule);
|
if (typeof moduleLoaded.parseCurrencies === 'function')
|
||||||
console.log(`Service ${serviceName} loaded successfully`);
|
services.push(moduleLoaded);
|
||||||
} else {
|
}
|
||||||
console.error(`Service file for ${serviceName} not found at ${servicePath}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
console.log('Loaded parser services:', serviceFiles);
|
||||||
await create_table();
|
await create_table();
|
||||||
|
|
||||||
schedule.scheduleJob(config['schedule'], async () => {
|
schedule.scheduleJob(config['schedule'], async () => {
|
||||||
|
@ -38,8 +36,6 @@ async function main() {
|
||||||
const results = await srv.parseCurrencies();
|
const results = await srv.parseCurrencies();
|
||||||
|
|
||||||
if (Array.isArray(results) && results.length > 0) {
|
if (Array.isArray(results) && results.length > 0) {
|
||||||
console.log(`Data received from ${srv.name || 'unknown service'}:`, results.length, 'items');
|
|
||||||
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
try {
|
try {
|
||||||
const currency = await validateCurrency(result);
|
const currency = await validateCurrency(result);
|
||||||
|
@ -53,9 +49,7 @@ async function main() {
|
||||||
currency.date,
|
currency.date,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
console.log(`Inserted data for ${currency.from_currency} -> ${currency.conv_currency}, Rate: ${currency.rate}`);
|
|
||||||
} catch (validationError) {
|
} catch (validationError) {
|
||||||
console.error('Validation failed for data:', result);
|
|
||||||
console.error(validationError);
|
console.error(validationError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,17 +8,15 @@ module.exports = {
|
||||||
return config['currency']['crypto'].map(convCurrency => {
|
return config['currency']['crypto'].map(convCurrency => {
|
||||||
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
||||||
|
|
||||||
const coinmarketcap = config['currency']['services']['coinmarketcap'];
|
|
||||||
|
|
||||||
return axios.get(
|
return axios.get(
|
||||||
coinmarketcap['base_url'],
|
'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
symbol: fromCurrency,
|
symbol: fromCurrency,
|
||||||
convert: convCurrency,
|
convert: convCurrency,
|
||||||
},
|
},
|
||||||
headers: {
|
headers: {
|
||||||
'X-CMC_PRO_API_KEY': coinmarketcap['api_key'],
|
'X-CMC_PRO_API_KEY': config['currency']['api_keys']['coinmarketcap'],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -27,8 +25,6 @@ module.exports = {
|
||||||
const truncatedPriceStr = truncate_number(data.price, 3);
|
const truncatedPriceStr = truncate_number(data.price, 3);
|
||||||
const rate = parseFloat(truncatedPriceStr);
|
const rate = parseFloat(truncatedPriceStr);
|
||||||
|
|
||||||
console.log(`Data fetched from CoinMarketCap: ${fromCurrency} -> ${convCurrency}, Rate: ${rate}`);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from_currency: fromCurrency,
|
from_currency: fromCurrency,
|
||||||
conv_currency: convCurrency,
|
conv_currency: convCurrency,
|
||||||
|
@ -37,7 +33,7 @@ module.exports = {
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err.respone.data);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue