From b1750e4a35ead4b8c300be24b84155308d139f2c Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Fri, 13 Dec 2019 20:59:49 -0300 Subject: [PATCH 1/6] requests installed --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 36a73a2..4667e31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ python-decouple==3.3 mongoengine==0.18.2 python-telegram-bot==12.2.0 websocket-client==0.56.0 -pendulum==2.0.5 \ No newline at end of file +pendulum==2.0.5 +requests==2.22.0 \ No newline at end of file From be7dd73685ae07b028e785dc588d7e9214959f15 Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Fri, 13 Dec 2019 21:00:03 -0300 Subject: [PATCH 2/6] convert dcr utils --- bot/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/bot/utils.py b/bot/utils.py index d9574a4..d386367 100644 --- a/bot/utils.py +++ b/bot/utils.py @@ -1,3 +1,6 @@ +import json + +import requests def build_menu(buttons, @@ -10,3 +13,24 @@ def build_menu(buttons, if footer_buttons: menu.append([footer_buttons]) return menu + + +def convert_dcr(dcr_amount: float, target_coin: str): + dcrdata_response = requests.get("https://dcrdata.decred.org/api/exchanges") + if dcrdata_response.status_code != 200: + raise Exception() + + dcr_to_usd_value = json.loads(dcrdata_response.content) + dcr_to_usd_value = dcr_to_usd_value.get("price") + + if target_coin == 'USD': + return dcr_amount*dcr_to_usd_value + + exchangerate_response = requests.get(f"https://api.exchangeratesapi.io/latest?base=USD&symbols={target_coin}") + if exchangerate_response.status_code != 200: + raise Exception() + + usd_to_target_value = json.loads(exchangerate_response.content) + usd_to_target_value = usd_to_target_value.get("rates").get(target_coin) + + return dcr_amount*dcr_to_usd_value*usd_to_target_value From 7344e7fd9fded675a3f81655c6b9b08053a4b727 Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Fri, 13 Dec 2019 21:00:09 -0300 Subject: [PATCH 3/6] exchange handlers --- bot/commands/exchange.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 bot/commands/exchange.py diff --git a/bot/commands/exchange.py b/bot/commands/exchange.py new file mode 100644 index 0000000..d8f0443 --- /dev/null +++ b/bot/commands/exchange.py @@ -0,0 +1,36 @@ +import logging + +from telegram import Update +from telegram.ext import CommandHandler, CallbackContext + +from bot.core import BotTelegramCore +from bot.utils import convert_dcr + + +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +logger = logging.getLogger(__name__) + + +def dcr(update: Update, context: CallbackContext): + try: + target_coin = context.args[0] + except TypeError: + target_coin = 'USD' + + try: + dcr_amount = context.args[1] + except TypeError: + dcr_amount = 1 + + target_value = convert_dcr(dcr_amount, target_coin) + + update.effective_message.reply_text(target_value) + + +def config_handlers(instance: BotTelegramCore): + logger.info('Setting exchange commands...') + + From 790a5465f824cfbe05131fd8ac020071cb5a73ef Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Sat, 14 Dec 2019 20:15:20 -0300 Subject: [PATCH 4/6] Custom API Errors --- bot/commands/exchange.py | 18 +++++++++++++++--- bot/exceptions.py | 7 +++++++ bot/utils.py | 15 +++++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 bot/exceptions.py diff --git a/bot/commands/exchange.py b/bot/commands/exchange.py index d8f0443..e1e21a8 100644 --- a/bot/commands/exchange.py +++ b/bot/commands/exchange.py @@ -5,6 +5,7 @@ from bot.core import BotTelegramCore from bot.utils import convert_dcr +from bot.exceptions import DcrDataAPIError, ExchangeAPIError logging.basicConfig( @@ -25,12 +26,23 @@ def dcr(update: Update, context: CallbackContext): except TypeError: dcr_amount = 1 - target_value = convert_dcr(dcr_amount, target_coin) + try: + target_value = convert_dcr(dcr_amount, target_currency) + except ExchangeAPIError as e: + update.effective_message.reply_text(f"{e}") + return + except DcrDataAPIError as e: + update.effective_message.reply_text("Error requests data from DCRData API.\n" + "Please contact my managers!") + update.effective_message.reply_text(f"{e}") + return + + message = f"{dcr_amount} DCR => {target_value:.2f} {target_currency}" - update.effective_message.reply_text(target_value) + update.effective_message.reply_text(message) def config_handlers(instance: BotTelegramCore): logger.info('Setting exchange commands...') - + instance.add_handler(CommandHandler("dcr", dcr)) diff --git a/bot/exceptions.py b/bot/exceptions.py new file mode 100644 index 0000000..fdee208 --- /dev/null +++ b/bot/exceptions.py @@ -0,0 +1,7 @@ + +class ExchangeAPIError(Exception): + pass + + +class DcrDataAPIError(Exception): + pass diff --git a/bot/utils.py b/bot/utils.py index d386367..a232c44 100644 --- a/bot/utils.py +++ b/bot/utils.py @@ -2,6 +2,8 @@ import requests +from bot.exceptions import DcrDataAPIError, ExchangeAPIError + def build_menu(buttons, n_cols, @@ -15,22 +17,23 @@ def build_menu(buttons, return menu -def convert_dcr(dcr_amount: float, target_coin: str): +def convert_dcr(dcr_amount: float, target_currency: str): dcrdata_response = requests.get("https://dcrdata.decred.org/api/exchanges") if dcrdata_response.status_code != 200: - raise Exception() + raise DcrDataAPIError(dcrdata_response.content) dcr_to_usd_value = json.loads(dcrdata_response.content) dcr_to_usd_value = dcr_to_usd_value.get("price") - if target_coin == 'USD': + if target_currency == 'USD': return dcr_amount*dcr_to_usd_value - exchangerate_response = requests.get(f"https://api.exchangeratesapi.io/latest?base=USD&symbols={target_coin}") + exchangerate_response = requests.get(f"https://api.exchangeratesapi.io/latest?base=USD&symbols={target_currency}") if exchangerate_response.status_code != 200: - raise Exception() + raise ExchangeAPIError(f"Currency {target_currency} is not valid!\n" + f"Choose one from https://api.exchangeratesapi.io/latest") usd_to_target_value = json.loads(exchangerate_response.content) - usd_to_target_value = usd_to_target_value.get("rates").get(target_coin) + usd_to_target_value = usd_to_target_value.get("rates").get(target_currency) return dcr_amount*dcr_to_usd_value*usd_to_target_value From 784215e2b64c478f711c4be029bd00a1e6f89d72 Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Sat, 14 Dec 2019 20:15:31 -0300 Subject: [PATCH 5/6] command args & handler --- bot/commands/__init__.py | 5 +++-- bot/commands/exchange.py | 27 +++++++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/bot/commands/__init__.py b/bot/commands/__init__.py index 0777435..681fe5d 100644 --- a/bot/commands/__init__.py +++ b/bot/commands/__init__.py @@ -1,10 +1,11 @@ from bot.commands.base import config_handlers as base_handlers from bot.commands.subscription import config_handlers as subscription_handlers from bot.commands.callback import config_handlers as callback_handlers - +from bot.commands.exchange import config_handlers as exchange_handlers handlers = [ base_handlers, subscription_handlers, - callback_handlers + callback_handlers, + exchange_handlers ] diff --git a/bot/commands/exchange.py b/bot/commands/exchange.py index e1e21a8..ec2e71d 100644 --- a/bot/commands/exchange.py +++ b/bot/commands/exchange.py @@ -16,15 +16,30 @@ def dcr(update: Update, context: CallbackContext): - try: - target_coin = context.args[0] - except TypeError: - target_coin = 'USD' + target_currency = None + dcr_amount = None try: - dcr_amount = context.args[1] - except TypeError: + arg_0 = context.args[0] + if arg_0.isdigit(): + dcr_amount = float(arg_0) + else: + target_currency = arg_0.upper() + except IndexError: dcr_amount = 1 + target_currency = "USD" + + try: + arg_1 = context.args[1] + if arg_1.isdigit(): + dcr_amount = float(arg_1) + else: + target_currency = arg_1.upper() + except IndexError: + if target_currency is None: + target_currency = "USD" + if dcr_amount is None: + dcr_amount = 1 try: target_value = convert_dcr(dcr_amount, target_currency) From b5d28f3ff18ca5d4334a25d5ef1bfa8499b251f8 Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Sat, 14 Dec 2019 20:19:51 -0300 Subject: [PATCH 6/6] flake8 --- bot/commands/exchange.py | 3 ++- bot/utils.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bot/commands/exchange.py b/bot/commands/exchange.py index ec2e71d..1f7d11e 100644 --- a/bot/commands/exchange.py +++ b/bot/commands/exchange.py @@ -47,7 +47,8 @@ def dcr(update: Update, context: CallbackContext): update.effective_message.reply_text(f"{e}") return except DcrDataAPIError as e: - update.effective_message.reply_text("Error requests data from DCRData API.\n" + update.effective_message.reply_text("Error requests data from " + "DCRData API.\n " "Please contact my managers!") update.effective_message.reply_text(f"{e}") return diff --git a/bot/utils.py b/bot/utils.py index a232c44..d248b88 100644 --- a/bot/utils.py +++ b/bot/utils.py @@ -28,10 +28,13 @@ def convert_dcr(dcr_amount: float, target_currency: str): if target_currency == 'USD': return dcr_amount*dcr_to_usd_value - exchangerate_response = requests.get(f"https://api.exchangeratesapi.io/latest?base=USD&symbols={target_currency}") + exchangerate_response = requests.get(f"https://api.exchangeratesapi.io/" + f"latest?base=USD" + f"&symbols={target_currency}") if exchangerate_response.status_code != 200: raise ExchangeAPIError(f"Currency {target_currency} is not valid!\n" - f"Choose one from https://api.exchangeratesapi.io/latest") + f"Choose one from " + f"https://api.exchangeratesapi.io/latest") usd_to_target_value = json.loads(exchangerate_response.content) usd_to_target_value = usd_to_target_value.get("rates").get(target_currency)