diff --git a/config.sample.hjson b/config.sample.hjson index ca3e562..dcc9d87 100644 --- a/config.sample.hjson +++ b/config.sample.hjson @@ -10,6 +10,15 @@ fiat: true crypto: false } + services: { + enabled: [ + 'coinmarketcap' + ] + coinmarketcap: { + "api_key": TOKEN_COINMARKETCAP" + "base_url": https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest + } + } fiat: [ USD, diff --git a/main.js b/main.js index e6b8848..88acc39 100644 --- a/main.js +++ b/main.js @@ -7,26 +7,28 @@ const { validateCurrency } = require('./models/Currency.js'); const { create_table, pool } = require('./database/data.js'); const config = require('./utils/load_config.js')(); +const services = []; +const servicesDir = path.join(__dirname, 'services'); + async function main() { if (!config['schedule']) throw new Error('The crontab schedule is not set.'); else if (!cron.isValidCron(config['schedule'], { alias: true })) throw new Error('The crontab is invalid.'); - - const servicesDir = path.join(__dirname, 'services'); - const serviceFiles = fs.readdirSync(servicesDir) - .filter(filename => filename.endsWith('.js')); - const services = []; - for (const file of serviceFiles) { - const filePath = path.join(servicesDir, file); - const moduleLoaded = require(filePath); + console.log('Loading services...'); + config['currency']['services']['enabled'].forEach(serviceName => { + const servicePath = path.join(servicesDir, `${serviceName}.js`); + if (fs.existsSync(servicePath)) { + const serviceModule = require(servicePath); - if (typeof moduleLoaded.parseCurrencies === 'function') - services.push(moduleLoaded); - } + services.push(serviceModule); + console.log(`Service ${serviceName} loaded successfully`); + } else { + console.error(`Service file for ${serviceName} not found at ${servicePath}`); + } + }); - console.log('Loaded parser services:', serviceFiles); await create_table(); schedule.scheduleJob(config['schedule'], async () => { diff --git a/services/coinmarketcap.js b/services/coinmarketcap.js index 66d8615..9d86659 100644 --- a/services/coinmarketcap.js +++ b/services/coinmarketcap.js @@ -8,15 +8,17 @@ module.exports = { return config['currency']['crypto'].map(convCurrency => { if (fromCurrency === convCurrency) return Promise.resolve(null); + const coinmarketcap = config['currency']['services']['coinmarketcap']; + return axios.get( - 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest', + coinmarketcap['base_url'], { params: { symbol: fromCurrency, convert: convCurrency, }, headers: { - 'X-CMC_PRO_API_KEY': config['currency']['api_keys']['coinmarketcap'], + 'X-CMC_PRO_API_KEY': coinmarketcap['api_key'], } } ) @@ -33,7 +35,7 @@ module.exports = { }; }) .catch((err) => { - console.error(err.respone.data); + console.error(err); return null; }); });