-
Notifications
You must be signed in to change notification settings - Fork 33
/
rate.py
99 lines (87 loc) · 3.76 KB
/
rate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
""" Pagermaid currency exchange rates plugin. Plugin by @fruitymelon and @xtaodada"""
import json, time
from json.decoder import JSONDecodeError
import urllib.request
from pagermaid.listener import listener, config
from pagermaid import log, version
from pagermaid.utils import alias_command
# i18n
## 默认语言
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": "数据每日更新"}
## 其他语言
if config["application_language"] == "en":
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"}
API = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies.json"
currencies = []
data = {}
inited = False
def init():
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
init()
last_init = time.time()
@listener(incoming=True, ignore_edited=True)
async def refresher(context):
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}")
@listener(is_plugin=True, outgoing=True, command="rate",
description=lang_rate["des"],
parameters=lang_rate["arg"])
async def rate(context):
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
NB = 1.0
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()
try:
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
endpoint = f"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/{FROM.lower()}.json"
with urllib.request.urlopen(endpoint) as response:
result = response.read()
try:
rate_data = json.loads(result)
await context.edit(
f'`{FROM} : {TO} = {NB} : {round(NB * rate_data[FROM.lower()][TO.lower()], 4)}`\n\n{lang_rate["warning"]}')
except Exception as e:
await context.edit(str(e))