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

73 lines
2.9 KiB
Python
Raw Normal View History

2023-04-18 01:49:11 +00:00
import datetime
2023-04-17 16:25:06 +00:00
2023-04-19 07:45:29 +00:00
from config import Config
2023-04-17 16:25:06 +00:00
from core.builtins import Bot
from core.component import module
2023-04-21 14:58:22 +00:00
from core.exceptions import NoReportException
2023-04-19 07:45:29 +00:00
from core.utils.http import get_url
2023-04-17 16:25:06 +00:00
2023-05-21 09:11:39 +00:00
api_key = Config('exchange_rate_api_key')
2023-05-21 13:26:33 +00:00
excr = module('exchange_rate',
2023-06-09 16:53:12 +00:00
desc='{exchange_rate.help.desc}',
alias=['exchangerate', 'excr'],
developers=['DoroWolf'])
2023-04-17 16:25:06 +00:00
2023-04-19 08:29:46 +00:00
2023-05-21 13:26:33 +00:00
@excr.command('<base> <target> [<amount>] {{exchange_rate.help}}')
2023-05-21 13:43:00 +00:00
async def _(msg: Bot.MessageSession):
2023-05-21 13:39:46 +00:00
base = msg.parsed_msg['<base>'].upper()
target = msg.parsed_msg['<target>'].upper()
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'))
await msg.finish(await exchange(base, target, amount, msg))
async def exchange(base_currency, target_currency, amount: float, msg):
2023-04-21 15:59:25 +00:00
url = f'https://v6.exchangerate-api.com/v6/{api_key}/codes'
2023-05-04 12:04:51 +00:00
data = await get_url(url, 200, fmt='json')
2023-04-21 15:59:25 +00:00
supported_currencies = data['supported_codes']
unsupported_currencies = []
if data['result'] == "success":
for currencie_names in supported_currencies:
if base_currency in currencie_names:
break
else:
unsupported_currencies.append(base_currency)
for currencie_names in supported_currencies:
if target_currency in currencie_names:
2023-04-30 03:30:59 +00:00
break
2023-04-21 15:59:25 +00:00
else:
unsupported_currencies.append(target_currency)
if unsupported_currencies:
2023-04-30 03:30:59 +00:00
await msg.finish(f"{msg.locale.t('exchange_rate.message.error.invalid')}{' '.join(unsupported_currencies)}")
2023-04-21 15:59:25 +00:00
else:
error_type = data['error-type']
raise NoReportException(f"{error_type}")
2023-04-21 06:20:13 +00:00
2023-04-17 16:25:06 +00:00
url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}'
2023-05-04 12:04:51 +00:00
data = await get_url(url, 200, fmt='json')
2023-04-19 11:50:05 +00:00
current_time = datetime.datetime.now().strftime("%Y-%m-%d")
2023-04-21 15:07:51 +00:00
if data['result'] == "success":
2023-04-21 15:09:37 +00:00
exchange_rate = data['conversion_result']
await msg.finish(
2023-04-21 14:58:22 +00:00
msg.locale.t('exchange_rate.message', amount=amount, base=base_currency, exchange_rate=exchange_rate,
target=target_currency, time=current_time))
else:
error_type = data['error-type']
raise NoReportException(f"{error_type}")
2023-05-21 13:26:33 +00:00
2023-05-22 07:43:27 +00:00
@excr.regex(r"(\d+(\.\d+)?)?\s?([a-zA-Z]{3})[兑|换|兌|換]([a-zA-Z]{3})", desc='{exchange_rate.help.regex.desc}')
2023-05-21 13:30:17 +00:00
async def _(msg: Bot.MessageSession):
2023-05-21 13:49:19 +00:00
groups = msg.matched_msg.groups()
2023-05-22 04:38:33 +00:00
amount = groups[0] if groups[0] else '1'
2023-05-21 13:46:41 +00:00
base = groups[2].upper()
2023-05-22 04:42:09 +00:00
target = groups[3].upper()
2023-05-22 02:02:30 +00:00
await msg.finish(await exchange(base, target, amount, msg))