Archived
1
0
Fork 0
This commit is contained in:
多羅狼 2023-04-19 19:04:25 +08:00 committed by GitHub
parent 60df57613a
commit 15b66e2d64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,39 +15,36 @@ exchange_rate = module('exchange_rate',
api_key = Config('exchange_rate_api_key')
@exchange_rate.command('<base> <target> [<amount>] {{exchange_rate.help}}')
@exchange_rate.command('<amount> <base> <target> {{exchange_rate.help}}')
async def _(msg: Bot.MessageSession):
base_currency = msg.parsed_msg['<base>'].upper()
target_currency = msg.parsed_msg['<target>'].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('<amount>', 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['<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'))
url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}'
response = await get_url(url, fmt='read')