2023-04-18 01:49:11 +00:00
|
|
|
|
import datetime
|
2023-04-17 16:25:06 +00:00
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
from core.builtins import Bot
|
|
|
|
|
from core.component import module
|
2023-04-18 05:25:46 +00:00
|
|
|
|
from core.exceptions import NoReportException
|
2023-04-17 16:25:06 +00:00
|
|
|
|
|
|
|
|
|
exchange_rate = module('exchange_rate', required_superuser = True)
|
|
|
|
|
|
|
|
|
|
api_key = 'd31697e581d5c35b038c625c'
|
|
|
|
|
|
|
|
|
|
@exchange_rate.command('<amount> <base> <target>')
|
|
|
|
|
async def _(msg: Bot.MessageSession):
|
2023-04-18 01:56:31 +00:00
|
|
|
|
base_currency = msg.parsed_msg['<base>'].upper()
|
|
|
|
|
target_currency = msg.parsed_msg['<target>'].upper()
|
2023-04-18 06:12:07 +00:00
|
|
|
|
"""
|
2023-04-18 04:44:54 +00:00
|
|
|
|
url = f'https://v6.exchangerate-api.com/v6/{api_key}/codes'
|
|
|
|
|
response = requests.get(url)
|
|
|
|
|
if response.status_code == 200:
|
2023-04-18 04:52:38 +00:00
|
|
|
|
data = response.json()
|
2023-04-18 04:44:54 +00:00
|
|
|
|
supported_currencies = data['supported_codes']
|
2023-04-18 04:59:32 +00:00
|
|
|
|
if base_currency not in supported_currencies or target_currency not in supported_currencies:
|
|
|
|
|
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)
|
|
|
|
|
await msg.finish(f"发生错误:无效的货币单位:{' '.join(unsupported_currencies)}")
|
2023-04-18 04:44:54 +00:00
|
|
|
|
else:
|
2023-04-18 05:25:46 +00:00
|
|
|
|
raise NoReportException(f"{response.text}")
|
2023-04-18 06:12:07 +00:00
|
|
|
|
"""
|
2023-04-18 01:49:11 +00:00
|
|
|
|
amount = None
|
|
|
|
|
while amount is None:
|
|
|
|
|
try:
|
|
|
|
|
amount = float(msg.parsed_msg['<amount>'])
|
|
|
|
|
if amount <= 0:
|
2023-04-18 01:56:31 +00:00
|
|
|
|
await msg.finish("发生错误:金额必须为正数。")
|
2023-04-18 01:49:11 +00:00
|
|
|
|
except ValueError:
|
2023-04-18 01:51:30 +00:00
|
|
|
|
await msg.finish("发生错误:无效的金额。")
|
2023-04-17 16:25:06 +00:00
|
|
|
|
|
|
|
|
|
url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}'
|
|
|
|
|
response = requests.get(url)
|
|
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
data = response.json()
|
2023-04-18 04:39:17 +00:00
|
|
|
|
exchange_rate = data['conversion_result']
|
|
|
|
|
current_time = datetime.datetime.now().strftime("%Y-%m-%d")
|
|
|
|
|
await msg.finish(f'{amount} {base_currency} -> {exchange_rate} {target_currency}\n({current_time})')
|
2023-04-17 16:25:06 +00:00
|
|
|
|
else:
|
2023-04-18 05:25:46 +00:00
|
|
|
|
raise NoReportException(f"{response.text}")
|