diff --git a/config.json b/config.json index 926b00a27a6ede992f9c4d72e7f1338c1c17aafc..ac823c6d609693baefae8bc79290dbf5e133a3c0 100644 --- a/config.json +++ b/config.json @@ -22,7 +22,20 @@ "tingo":{ "path":"./data" }, - "currency_api_sources":["currency_data-api", "exchangerates_data-api"], + "currency_api_sources": [ + { + "name": "currency_data-api", + "url": "https://apilayer.com/marketplace/description/currency_data-api" + }, + { + "name": "exchangerates_data-api", + "url": "https://apilayer.com/marketplace/exchangerates_data-api" + }, + { + "name": "fawazahmed0-api", + "url": "https://github.com/fawazahmed0/currency-api/tree/1" + } + ], "currency_api":{ "source": "currency_data-api", "api_key": "Ni4XdOuK5OnUpYONlCbqOTzVbWSYrW2l" diff --git a/modules/cash/lib/scheduler.js b/modules/cash/lib/scheduler.js index c4fc1d5185d204c56da0039a9e9bd341e393b944..f1966ebbb1ad18f8a678df141d9dad00740b32aa 100644 --- a/modules/cash/lib/scheduler.js +++ b/modules/cash/lib/scheduler.js @@ -190,12 +190,77 @@ module.exports.exchangeRate = function () { }); }, cb); }, + /* currency rate from "fawazahmed0-api" */ + function (cb) { + if(cfg.currency_api.source != "fawazahmed0-api") + return cb(); + if (!_.keys(pairs).length) + return cb('Currency not found'); + async.forEach(groups, function (group, cb) { + group.source = group.source.toLowerCase(); + group.target = group.target.toLowerCase(); + console.log('Do check rates with currencies: ' + JSON.stringify(group) + " via fawazahmed0-api"); + /* api source + https://github.com/fawazahmed0/currency-api/tree/1 + */ + async.waterfall([ + function(cb){ + // main url + const url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/" + group.source + ".json"; + request({ + url: url, + method: 'GET' + }, function (err, response, body) { + if(err){ + console.log("Failed to fetch url", url, err); + cb(null, null); + } else if (response.statusCode == 200 && body) + cb(null, JSON.parse(body)); + else cb(null, null); + }); + }, function(obj_currencies, cb){ + // fallback url 1 + const url = "http://raw.githubusercontent.com/fawazahmed0/currency-api/1/latest/currencies/" + group.source + ".json"; + if(obj_currencies) + return cb(null, obj_currencies); + request({ + url: url, + method: 'GET' + }, function (err, response, body) { + if(err){ + console.log("Failed to fetch url", url, err); + cb(null, null); + } else if (response.statusCode == 200 && body) + cb(null, JSON.parse(body)); + else cb(null, null); + }); + } + ], function(err, obj_currencies){ + if(!obj_currencies) + return cb("Failed to check rates with currencies"); + if(!curency["date"]){ + curency["date"] = moment(obj_currencies.date); + } + for(var _pair in pairs){ + var _parts = _pair.split("-"); + var _src = _parts[0].toLowerCase(), _dest = _parts[1].toLowerCase(); + if(obj_currencies[_src] && obj_currencies[_src][_dest]){ + curency[_pair] = obj_currencies[_src][_dest]; + } else if(obj_currencies[_dest] && obj_currencies[_dest][_src]){ + curency[_pair] = 1/obj_currencies[_dest][_src]; + } + } + cb(); + }); + }, cb); + }, /* crypto currency rate */ function (cb) { - // test test test !!! - // pairs["BTC-RUB"] = {}; if (!_.keys(pairs).length) return cb('Currency not found'); + // crypto currency rate is in fawazahmed0-api + if(cfg.currency_api.source == "fawazahmed0-api") + return cb(); console.log('Do check rates with crypto currencies: ' + _.keys(pairs)); /* api source https://coinlayer.com/quickstart