Compare commits
No commits in common. "85c8e69ee42ade844de4ed0baee6117a3f5ff9a4" and "c7e955a9b6a7f0145b4566aef86ebf2afd7c1b9b" have entirely different histories.
85c8e69ee4
...
c7e955a9b6
3 changed files with 2 additions and 54 deletions
2
main.js
2
main.js
|
@ -38,7 +38,7 @@ async function main() {
|
|||
const results = await srv.parseCurrencies();
|
||||
|
||||
if (Array.isArray(results) && results.length > 0) {
|
||||
console.log(`Data received from ${srv.info.name || 'unknown service'}:`, results.length, 'items');
|
||||
console.log(`Data received from ${srv.name || 'unknown service'}:`, results.length, 'items');
|
||||
|
||||
for (const result of results) {
|
||||
try {
|
||||
|
|
|
@ -3,16 +3,12 @@ const config = require('../utils/load_config.js')();
|
|||
const { truncate_number } = require('../utils/truncate_number.js');
|
||||
|
||||
module.exports = {
|
||||
info: {
|
||||
name: 'CoinMarketCap',
|
||||
},
|
||||
parseCurrencies: async () => {
|
||||
const promises = config['currency']['crypto'].map(fromCurrency => {
|
||||
return config['currency']['crypto'].map(convCurrency => {
|
||||
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
||||
|
||||
const coinmarketcap = config['currency']['services']['coinmarketcap'];
|
||||
const serviceName = module.exports.info.name;
|
||||
|
||||
return axios.get(
|
||||
coinmarketcap['base_url'],
|
||||
|
@ -31,7 +27,7 @@ module.exports = {
|
|||
const truncatedPriceStr = truncate_number(data.price, 3);
|
||||
const rate = parseFloat(truncatedPriceStr);
|
||||
|
||||
console.log(`Data fetched from ${serviceName}: ${fromCurrency} -> ${convCurrency}, Rate: ${rate}`);
|
||||
console.log(`Data fetched from CoinMarketCap: ${fromCurrency} -> ${convCurrency}, Rate: ${rate}`);
|
||||
|
||||
return {
|
||||
from_currency: fromCurrency,
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
const axios = require('axios');
|
||||
const config = require('../utils/load_config.js')();
|
||||
const { truncate_number } = require('../utils/truncate_number.js');
|
||||
|
||||
module.exports = {
|
||||
info: {
|
||||
name: 'DuckDuckGo',
|
||||
},
|
||||
parseCurrencies: async () => {
|
||||
const promises = config['currency']['fiat'].map(fromCurrency => {
|
||||
return config['currency']['fiat'].map(convCurrency => {
|
||||
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
||||
|
||||
const serviceName = module.exports.info.name;
|
||||
|
||||
return axios.get(
|
||||
`${config['currency']['services']['duckduckgo']['base_url']}${fromCurrency}/${convCurrency}`,
|
||||
)
|
||||
.then((res) => {
|
||||
const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg');
|
||||
const data = JSON.parse(
|
||||
Array.from(res.data.matchAll(regExp))[0][1],
|
||||
);
|
||||
const truncatedPriceStr = truncate_number(data['to'][0]['mid'], 3);
|
||||
const rate = parseFloat(truncatedPriceStr);
|
||||
|
||||
console.log(`Data fetched from ${serviceName}: ${fromCurrency} -> ${convCurrency}, Rate: ${rate}`);
|
||||
|
||||
return {
|
||||
from_currency: fromCurrency,
|
||||
conv_currency: convCurrency,
|
||||
rate: rate,
|
||||
date: new Date(data['timestamp']).toISOString().substring(0, 10),
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
return null;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const flattenedPromises = promises.flat();
|
||||
const results = await Promise.all(flattenedPromises);
|
||||
|
||||
return results.filter(result => result !== null);
|
||||
}
|
||||
};
|
Loading…
Add table
Reference in a new issue