2021-04-21 10:32:19 +00:00
|
|
|
""" Pagermaid currency exchange rates plugin. Plugin by @fruitymelon and @xtaodada"""
|
2020-08-11 17:05:21 +00:00
|
|
|
|
2021-06-16 07:09:40 +00:00
|
|
|
import json, time
|
2021-04-21 10:32:19 +00:00
|
|
|
from json.decoder import JSONDecodeError
|
2020-08-11 17:05:21 +00:00
|
|
|
import urllib.request
|
2021-04-12 15:55:08 +00:00
|
|
|
from pagermaid.listener import listener, config
|
2022-01-18 08:47:20 +00:00
|
|
|
from pagermaid import log, version
|
2021-06-16 07:09:40 +00:00
|
|
|
from pagermaid.utils import alias_command
|
2021-04-12 15:55:08 +00:00
|
|
|
|
2021-06-16 07:09:40 +00:00
|
|
|
# i18n
|
2021-04-12 15:55:08 +00:00
|
|
|
## 默认语言
|
2021-06-16 07:09:40 +00:00
|
|
|
lang_rate = {"des": "货币汇率插件", "arg": "<FROM> <TO> <NUM>",
|
|
|
|
"help": "这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NUM>,其中 <NUM> 是可省略的`\n\n支持货币: \n",
|
|
|
|
"nc": "不是支持的货币. \n\n支持货币: \n", "notice": "数据每日更新,建议使用 bc 插件查看加密货币汇率", "warning": "数据每日更新"}
|
2021-04-12 15:55:08 +00:00
|
|
|
## 其他语言
|
|
|
|
if config["application_language"] == "en":
|
2021-06-16 07:09:40 +00:00
|
|
|
lang_rate = {"des": "Currency exchange rate plugin", "arg": "<FROM> <TO> <NUM>",
|
|
|
|
"help": "Currency exchange rate plugin\n\nUsage: `-rate <FROM> <TO> <NUM> where <NUM> is "
|
|
|
|
"optional`\n\nAvailable currencies: \n",
|
|
|
|
"nc": "is not available.\n\nAvailable currencies: \n",
|
|
|
|
"notice": "Data are updated daily, for encrypted currencies we recommend to use the `bc` plugin.",
|
|
|
|
"warning": "Data are updated daily"}
|
2020-08-14 11:58:26 +00:00
|
|
|
|
2024-03-04 00:44:34 +00:00
|
|
|
API = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies.json"
|
2021-04-21 10:32:19 +00:00
|
|
|
currencies = []
|
|
|
|
data = {}
|
2020-08-14 11:58:26 +00:00
|
|
|
|
2021-04-21 10:32:19 +00:00
|
|
|
inited = False
|
2020-08-11 17:05:21 +00:00
|
|
|
|
2021-04-03 12:02:22 +00:00
|
|
|
|
2021-04-21 10:32:19 +00:00
|
|
|
def init():
|
2021-06-16 07:09:40 +00:00
|
|
|
with urllib.request.urlopen(API) as response:
|
|
|
|
result = response.read()
|
|
|
|
try:
|
|
|
|
global data
|
|
|
|
data = json.loads(result)
|
|
|
|
for key in list(enumerate(data)):
|
|
|
|
currencies.append(key[1].upper())
|
|
|
|
currencies.sort()
|
|
|
|
except JSONDecodeError as e:
|
|
|
|
raise e
|
|
|
|
global inited
|
|
|
|
inited = True
|
2021-04-03 12:02:22 +00:00
|
|
|
|
|
|
|
|
2021-04-21 10:32:19 +00:00
|
|
|
init()
|
|
|
|
last_init = time.time()
|
2021-04-03 12:02:22 +00:00
|
|
|
|
2021-06-16 07:09:40 +00:00
|
|
|
|
2021-04-21 10:32:19 +00:00
|
|
|
@listener(incoming=True, ignore_edited=True)
|
|
|
|
async def refresher(context):
|
2021-06-16 07:09:40 +00:00
|
|
|
global last_init
|
|
|
|
if time.time() - last_init > 24 * 60 * 60:
|
|
|
|
# we'd better do this to prevent ruining the log file with massive fail logs
|
|
|
|
# as this `refresher` would be called frequently
|
|
|
|
last_init = time.time()
|
|
|
|
try:
|
|
|
|
init()
|
|
|
|
except Exception as e:
|
|
|
|
await log(f"Warning: plugin rate failed to refresh rates data. {e}")
|
|
|
|
|
2021-04-03 12:02:22 +00:00
|
|
|
|
2020-08-11 17:05:21 +00:00
|
|
|
@listener(is_plugin=True, outgoing=True, command="rate",
|
2021-04-12 15:55:08 +00:00
|
|
|
description=lang_rate["des"],
|
|
|
|
parameters=lang_rate["arg"])
|
2020-08-11 17:05:21 +00:00
|
|
|
async def rate(context):
|
2021-06-16 07:09:40 +00:00
|
|
|
if not inited:
|
|
|
|
init()
|
|
|
|
if not inited:
|
|
|
|
return
|
|
|
|
if not context.parameter:
|
|
|
|
await context.edit(f"{lang_rate['help']}`{', '.join(currencies)}`\n\n{lang_rate['notice']}")
|
|
|
|
return
|
2021-04-21 10:32:19 +00:00
|
|
|
NB = 1.0
|
2021-06-16 07:09:40 +00:00
|
|
|
if len(context.parameter) != 3:
|
|
|
|
if len(context.parameter) != 2:
|
|
|
|
await context.edit(f"{lang_rate['help']}`{', '.join(currencies)}`\n\n{lang_rate['notice']}")
|
|
|
|
return
|
|
|
|
FROM = context.parameter[0].upper().strip()
|
|
|
|
TO = context.parameter[1].upper().strip()
|
2020-11-22 02:15:14 +00:00
|
|
|
try:
|
2021-06-16 07:09:40 +00:00
|
|
|
NB = NB if len(context.parameter) == 2 else float(context.parameter[2].strip())
|
|
|
|
except:
|
|
|
|
NB = 1.0
|
|
|
|
if currencies.count(FROM) == 0:
|
|
|
|
await context.edit(f"{FROM}{lang_rate['nc']}`{', '.join(currencies)}`")
|
|
|
|
return
|
|
|
|
if currencies.count(TO) == 0:
|
|
|
|
await context.edit(f"{TO}{lang_rate['nc']}{', '.join(currencies)}`")
|
|
|
|
return
|
2024-03-04 00:44:34 +00:00
|
|
|
endpoint = f"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/{FROM.lower()}.json"
|
2021-06-16 07:09:40 +00:00
|
|
|
with urllib.request.urlopen(endpoint) as response:
|
|
|
|
result = response.read()
|
|
|
|
try:
|
|
|
|
rate_data = json.loads(result)
|
|
|
|
await context.edit(
|
2024-03-04 00:44:34 +00:00
|
|
|
f'`{FROM} : {TO} = {NB} : {round(NB * rate_data[FROM.lower()][TO.lower()], 4)}`\n\n{lang_rate["warning"]}')
|
2021-06-16 07:09:40 +00:00
|
|
|
except Exception as e:
|
|
|
|
await context.edit(str(e))
|