From 8e2e4656121083245f6c46b1e28f954ed31223ce Mon Sep 17 00:00:00 2001
From: denis <denisg@pushok.com>
Date: Mon, 18 Sep 2023 10:55:13 -0700
Subject: [PATCH 1/3] issue #2 use fawazahmed0 to get currencies rate

---
 config.json                   | 15 +++++++++++++-
 modules/cash/lib/scheduler.js | 39 +++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/config.json b/config.json
index 926b00a..ac823c6 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 c4fc1d5..7b4471e 100644
--- a/modules/cash/lib/scheduler.js
+++ b/modules/cash/lib/scheduler.js
@@ -190,10 +190,45 @@ 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
+				*/
+				const request_options = {
+					url: "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/" + group.source + ".json",
+					method: 'GET'
+				};
+				request(request_options, function (err, response, body) {
+					if (response.statusCode == 200 && body) {
+						var obj_currencies = JSON.parse(body);
+						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];
+							}
+						}
+					} else return cb("Failed to check rates with currencies");
+					cb();
+				});
+			}, cb);
+		},
 		/* crypto currency rate */
 		function (cb) {
-			// test test test !!!
-			// pairs["BTC-RUB"] = {};
 			if (!_.keys(pairs).length)
 				return cb('Currency not found');
 			console.log('Do check rates with crypto currencies: ' + _.keys(pairs));
-- 
GitLab


From 9f733f0e07b70c62fc427fedab650dfb84b0908c Mon Sep 17 00:00:00 2001
From: denis <denisg@pushok.com>
Date: Tue, 19 Sep 2023 03:42:14 -0700
Subject: [PATCH 2/3] issue #2 dont fetch extra crypto currency rates for
 fawazahmed0-api

---
 modules/cash/lib/scheduler.js | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/modules/cash/lib/scheduler.js b/modules/cash/lib/scheduler.js
index 7b4471e..c5bf09b 100644
--- a/modules/cash/lib/scheduler.js
+++ b/modules/cash/lib/scheduler.js
@@ -231,6 +231,9 @@ module.exports.exchangeRate = function () {
 		function (cb) {
 			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
-- 
GitLab


From 0f1f8c3973ee287cd48de7c6e562f55cb08d611e Mon Sep 17 00:00:00 2001
From: denis <denisg@pushok.com>
Date: Wed, 20 Sep 2023 07:38:53 -0700
Subject: [PATCH 3/3] issue #2 use fallback url(s) for fawazahmed0-api

---
 modules/cash/lib/scheduler.js | 65 +++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 19 deletions(-)

diff --git a/modules/cash/lib/scheduler.js b/modules/cash/lib/scheduler.js
index c5bf09b..f1966eb 100644
--- a/modules/cash/lib/scheduler.js
+++ b/modules/cash/lib/scheduler.js
@@ -203,26 +203,53 @@ module.exports.exchangeRate = function () {
 				/* api source
 					https://github.com/fawazahmed0/currency-api/tree/1
 				*/
-				const request_options = {
-					url: "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/" + group.source + ".json",
-					method: 'GET'
-				};
-				request(request_options, function (err, response, body) {
-					if (response.statusCode == 200 && body) {
-						var obj_currencies = JSON.parse(body);
-						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];
-							}
+				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];
 						}
-					} else return cb("Failed to check rates with currencies");
+					}
 					cb();
 				});
 			}, cb);
-- 
GitLab