diff --git a/modules/exchange_rate/__init__.py b/modules/exchange_rate/__init__.py index 03b332ac..af6257c7 100644 --- a/modules/exchange_rate/__init__.py +++ b/modules/exchange_rate/__init__.py @@ -15,39 +15,36 @@ exchange_rate = module('exchange_rate', api_key = Config('exchange_rate_api_key') -@exchange_rate.command(' [] {{exchange_rate.help}}') +@exchange_rate.command(' {{exchange_rate.help}}') async def _(msg: Bot.MessageSession): base_currency = msg.parsed_msg[''].upper() target_currency = msg.parsed_msg[''].upper() - # url = f'https://v6.exchangerate-api.com/v6/{api_key}/codes' - # response = requests.get(url) - # if response.status_code == 200: - # data = response.json() - # supported_currencies = data['supported_codes'] - # unsupported_currencies = [] - # if base_currency not in supported_currencies: - # unsupported_currencies.append(base_currency) - # if target_currency not in supported_currencies: - # unsupported_currencies.append(target_currency) - # if unsupported_currencies: - # await msg.finish(f"{msg.locale.t('exchange_rate.message.error.invalid')}{' '.join(unsupported_currencies)}") - # else: - # data = response.json() - # error_type = data['error-type'] - # raise NoReportException(f"{error_type}") - - amount = msg.parsed_msg.get('', None) - if amount is None: - amount = 1 + url = f'https://v6.exchangerate-api.com/v6/{api_key}/codes' + response = await get_url(url, fmt='read') + if response.status_code == 200: + data = response.json() + supported_currencies = data['supported_codes'] + unsupported_currencies = [] + if base_currency not in supported_currencies: + unsupported_currencies.append(base_currency) + if target_currency not in supported_currencies: + unsupported_currencies.append(target_currency) + if unsupported_currencies: + await msg.finish(f"{msg.locale.t('exchange_rate.message.error.invalid')}{', '.join(unsupported_currencies)}") else: - while True: - try: - amount = float(amount) - if amount <= 0: - await msg.finish(msg.locale.t('exchange_rate.message.error.non_positive')) - except ValueError: - await msg.finish(msg.locale.t('exchange_rate.message.error.non_digital')) + data = response.json() + error_type = data['error-type'] + raise NoReportException(f"{error_type}") + + amount = None + while amount is None: + try: + amount = float(msg.parsed_msg['']) + if amount <= 0: + await msg.finish(msg.locale.t('exchange_rate.message.error.non_positive')) + except ValueError: + await msg.finish(msg.locale.t('exchange_rate.message.error.non_digital')) url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}' response = await get_url(url, fmt='read')