PagerMaid_Plugins/rate.py

76 lines
2.6 KiB
Python
Raw Normal View History

2021-04-03 12:02:22 +00:00
""" Pagermaid currency exchange rates plugin. Plugin by @fruitymelon and @xtaodada """
2020-08-11 17:05:21 +00:00
2021-04-03 12:02:22 +00:00
import sys
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
# i18n
## 默认语言
lang_rate = {"des": "货币汇率插件", "arg": "<FROM> <TO> <NUM>", "not": "请先安装依赖:`python3 -m pip install xmltodict`\n随后,请重启 pagermaid。", "help": "这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n", "nc": "不是支持的货币. \n\n支持货币: \n"}
## 其他语言
if config["application_language"] == "en":
lang_rate = {"des": "Currency exchange rate plugin", "arg": "<FROM> <TO> <NUM>", "not": "Please run: `python3 -m pip install xmltodict`\nand restart pagermaid。", "help": "Currency exchange rate plugin\n\nUsage: `-rate <FROM> <TO> <NB>`\n\nSupport: \n", "nc": "is not support\n\nSupport: \n"}
2020-08-11 17:05:21 +00:00
2021-04-03 12:02:22 +00:00
imported = True
API = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
2020-08-11 17:05:21 +00:00
currencies = []
2021-04-03 12:02:22 +00:00
rates = {}
2020-08-11 17:05:21 +00:00
2020-08-14 11:58:26 +00:00
2020-08-11 17:05:21 +00:00
def init():
2020-08-14 11:58:26 +00:00
with urllib.request.urlopen(API) as response:
result = response.read()
try:
2021-04-03 12:02:22 +00:00
global rate_data, rates
rate_data = xmltodict.parse(result)
rate_data = rate_data['gesmes:Envelope']['Cube']['Cube']['Cube']
for i in rate_data:
currencies.append(i['@currency'])
rates[i['@currency']] = float(i['@rate'])
2020-08-14 11:58:26 +00:00
currencies.sort()
2021-04-03 12:02:22 +00:00
except Exception as e:
2020-08-14 11:58:26 +00:00
raise e
2020-08-11 17:05:21 +00:00
2021-04-03 12:02:22 +00:00
try:
import xmltodict
init()
except ImportError:
imported = False
def logsync(message):
sys.stdout.writelines(f"{message}\n")
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-04-03 12:02:22 +00:00
if not imported:
2021-04-12 15:55:08 +00:00
await context.edit(lang_rate["not"])
2021-04-03 12:02:22 +00:00
return
2020-08-14 11:58:26 +00:00
if not context.parameter:
await context.edit(
2021-04-12 15:55:08 +00:00
f"{lang_rate["help"]}{', '.join(currencies)}")
2020-08-14 11:58:26 +00:00
return
2020-11-22 02:15:14 +00:00
if len(context.parameter) != 3:
2021-04-12 15:55:08 +00:00
await context.edit(f"{lang_rate["help"]}{', '.join(currencies)}")
2020-08-14 11:58:26 +00:00
return
FROM = context.parameter[0].upper().strip()
TO = context.parameter[1].upper().strip()
2020-11-22 02:15:14 +00:00
try:
NB = float(context.parameter[2].strip())
except:
NB = 1.0
2020-08-14 11:58:26 +00:00
if currencies.count(FROM) == 0:
await context.edit(
2021-04-12 15:55:08 +00:00
f"{FROM}{lang_rate["nc"]}{', '.join(currencies)}")
2020-08-14 11:58:26 +00:00
return
if currencies.count(TO) == 0:
2021-04-12 15:55:08 +00:00
await context.edit(f"{TO}{lang_rate["nc"]}{', '.join(currencies)}")
2020-08-14 11:58:26 +00:00
return
2021-04-03 12:02:22 +00:00
rate_num = round(rates[TO] / rates[FROM] * NB, 2)
await context.edit(f'{FROM} : {TO} = {NB} : {rate_num}')