Compare commits
3 commits
c7e955a9b6
...
85c8e69ee4
Author | SHA1 | Date | |
---|---|---|---|
85c8e69ee4 | |||
16994607bb | |||
9d85aba937 |
3 changed files with 54 additions and 2 deletions
2
main.js
2
main.js
|
@ -38,7 +38,7 @@ 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');
|
console.log(`Data received from ${srv.info.name || 'unknown service'}:`, results.length, 'items');
|
||||||
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -3,12 +3,16 @@ const config = require('../utils/load_config.js')();
|
||||||
const { truncate_number } = require('../utils/truncate_number.js');
|
const { truncate_number } = require('../utils/truncate_number.js');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
info: {
|
||||||
|
name: 'CoinMarketCap',
|
||||||
|
},
|
||||||
parseCurrencies: async () => {
|
parseCurrencies: async () => {
|
||||||
const promises = config['currency']['crypto'].map(fromCurrency => {
|
const promises = config['currency']['crypto'].map(fromCurrency => {
|
||||||
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'];
|
const coinmarketcap = config['currency']['services']['coinmarketcap'];
|
||||||
|
const serviceName = module.exports.info.name;
|
||||||
|
|
||||||
return axios.get(
|
return axios.get(
|
||||||
coinmarketcap['base_url'],
|
coinmarketcap['base_url'],
|
||||||
|
@ -27,7 +31,7 @@ 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}`);
|
console.log(`Data fetched from ${serviceName}: ${fromCurrency} -> ${convCurrency}, Rate: ${rate}`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from_currency: fromCurrency,
|
from_currency: fromCurrency,
|
||||||
|
|
48
services/duckduckgo.js
Normal file
48
services/duckduckgo.js
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
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