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-06-21 10:52:12 +00:00
|
|
|
@excr.command('<base> <target> {{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()
|
2023-06-21 10:52:12 +00:00
|
|
|
|
2023-06-21 11:06:42 +00:00
|
|
|
amount_str = base[:-3]
|
2023-06-21 10:52:12 +00:00
|
|
|
base_currency = base[-3:]
|
2023-06-21 11:06:42 +00:00
|
|
|
|
2023-05-21 13:39:46 +00:00
|
|
|
try:
|
2023-06-21 11:06:42 +00:00
|
|
|
if amount_str:
|
|
|
|
amount = float(amount_str)
|
|
|
|
else:
|
|
|
|
amount = 1.0
|
|
|
|
|
2023-05-21 13:39:46 +00:00
|
|
|
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'))
|
2023-06-21 10:52:12 +00:00
|
|
|
await msg.finish(await exchange(base_currency, target, amount, msg))
|
2023-05-21 13:39:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2023-07-02 07:34:12 +00:00
|
|
|
raise NoReportException(data['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:
|
2023-07-02 07:34:12 +00:00
|
|
|
raise NoReportException(data['error-type'])
|
2023-05-21 13:26:33 +00:00
|
|
|
|
|
|
|
|
2023-06-18 09:03:26 +00:00
|
|
|
@excr.regex(r"(\d+(\.\d+)?)?\s?([a-zA-Z]{3})\s?[兑|换|兌|換]\s?([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))
|