mirror of
https://github.com/TeamPGM/PagerMaid_Plugins.git
synced 2024-11-25 10:47:14 +00:00
🚚 修复上游 api 跑路 (#143)
This commit is contained in:
parent
531c4bcc1a
commit
3b919a4224
47
rate.py
47
rate.py
@ -1,42 +1,52 @@
|
|||||||
""" Pagermaid currency exchange rates plugin. Plugin by @fruitymelon """
|
""" Pagermaid currency exchange rates plugin. Plugin by @fruitymelon and @xtaodada """
|
||||||
|
|
||||||
import asyncio, json
|
import sys
|
||||||
from json.decoder import JSONDecodeError
|
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from pagermaid.listener import listener
|
from pagermaid.listener import listener
|
||||||
|
|
||||||
API = "https://api.exchangeratesapi.io/latest"
|
imported = True
|
||||||
|
API = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
|
||||||
currencies = []
|
currencies = []
|
||||||
data = {}
|
rates = {}
|
||||||
|
|
||||||
inited = False
|
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
with urllib.request.urlopen(API) as response:
|
with urllib.request.urlopen(API) as response:
|
||||||
result = response.read()
|
result = response.read()
|
||||||
try:
|
try:
|
||||||
global data
|
global rate_data, rates
|
||||||
data = json.loads(result)
|
rate_data = xmltodict.parse(result)
|
||||||
data["rates"][data["base"]] = 1.0
|
rate_data = rate_data['gesmes:Envelope']['Cube']['Cube']['Cube']
|
||||||
for key in list(enumerate(data["rates"])):
|
for i in rate_data:
|
||||||
currencies.append(key[1])
|
currencies.append(i['@currency'])
|
||||||
|
rates[i['@currency']] = float(i['@rate'])
|
||||||
currencies.sort()
|
currencies.sort()
|
||||||
except JSONDecodeError as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
global inited
|
|
||||||
inited = True
|
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
import xmltodict
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
except ImportError:
|
||||||
|
imported = False
|
||||||
|
|
||||||
|
|
||||||
|
def logsync(message):
|
||||||
|
sys.stdout.writelines(f"{message}\n")
|
||||||
|
|
||||||
|
|
||||||
|
logsync("rate: loading... If failed, please install xmltodict first.")
|
||||||
|
|
||||||
|
|
||||||
@listener(is_plugin=True, outgoing=True, command="rate",
|
@listener(is_plugin=True, outgoing=True, command="rate",
|
||||||
description="货币汇率插件",
|
description="货币汇率插件",
|
||||||
parameters="<FROM> <TO> <NB>")
|
parameters="<FROM> <TO> <NB>")
|
||||||
async def rate(context):
|
async def rate(context):
|
||||||
while not inited:
|
if not imported:
|
||||||
await asyncio.sleep(1)
|
await context.edit("请先安装依赖:`python3 -m pip install xmltodict`\n随后,请重启 pagermaid。")
|
||||||
|
return
|
||||||
if not context.parameter:
|
if not context.parameter:
|
||||||
await context.edit(
|
await context.edit(
|
||||||
f"这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n{', '.join(currencies)}")
|
f"这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n{', '.join(currencies)}")
|
||||||
@ -57,4 +67,5 @@ async def rate(context):
|
|||||||
if currencies.count(TO) == 0:
|
if currencies.count(TO) == 0:
|
||||||
await context.edit(f"{TO}不是支持的货币. \n\n支持货币: \n{', '.join(currencies)}")
|
await context.edit(f"{TO}不是支持的货币. \n\n支持货币: \n{', '.join(currencies)}")
|
||||||
return
|
return
|
||||||
await context.edit(f'{FROM} : {TO} = {NB} : {round(data["rates"][TO]/data["rates"][FROM]*NB,2)}')
|
rate_num = round(rates[TO] / rates[FROM] * NB, 2)
|
||||||
|
await context.edit(f'{FROM} : {TO} = {NB} : {rate_num}')
|
||||||
|
Loading…
Reference in New Issue
Block a user