Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/exchange_rate/__init__.py
2023-04-21 13:54:19 +08:00

51 lines
2.2 KiB
Python

import datetime
import json
from config import Config
from core.builtins import Bot
from core.component import module
from core.utils.http import get_url
exchange_rate = module('exchange_rate',
desc='{exchange_rate.help.desc}',
alias={'exchangerate': 'exchange_rate',
'excr': 'exchange_rate'},
developers=['DoroWolf'])
api_key = Config('exchange_rate_api_key')
@exchange_rate.command('<base> <target> [<amount>] {{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 = await get_url(url, fmt='read', status_code=200)
# response_str = response.decode('utf-8')
# data = json.loads(response_str)
# 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)}")
amount = msg.parsed_msg.get('<amount>', '1')
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'))
url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}'
response = await get_url(url, fmt='read', status_code=200)
response_str = response.decode('utf-8')
data = json.loads(response_str)
exchange_rate = data['conversion_result']
current_time = datetime.datetime.now().strftime("%Y-%m-%d")
await msg.finish(
msg.locale.t('exchange_rate.message', amount=amount, base=base_currency, exchange_rate=exchange_rate,
target=target_currency, time=current_time))