From a0c6ae52bcb55090b70d5294035905d761577952 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 14 Aug 2020 19:58:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A9=20rate=20=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list.json | 2 +- rate.py | 68 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/list.json b/list.json index a8960cc..951cf9e 100644 --- a/list.json +++ b/list.json @@ -92,7 +92,7 @@ }, { "name": "rate", - "version": "1.0", + "version": "1.1", "section": "daily", "maintainer": "fruitymelon", "size": "1.7 kb", diff --git a/rate.py b/rate.py index f77a76c..6cdbceb 100644 --- a/rate.py +++ b/rate.py @@ -3,8 +3,6 @@ import asyncio, json from json.decoder import JSONDecodeError import urllib.request - -from pagermaid import bot, log from pagermaid.listener import listener API = "https://api.exchangeratesapi.io/latest" @@ -13,42 +11,46 @@ data = {} inited = False + def init(): - with urllib.request.urlopen(API) as response: - result = response.read() - try: - global data - data = json.loads(result) - data["rates"][data["base"]] = 1.0 - for key in list(enumerate(data["rates"])): - currencies.append(key[1]) - currencies.sort() - except JSONDecodeError as e: - raise e - global inited - inited = True + with urllib.request.urlopen(API) as response: + result = response.read() + try: + global data + data = json.loads(result) + data["rates"][data["base"]] = 1.0 + for key in list(enumerate(data["rates"])): + currencies.append(key[1]) + currencies.sort() + except JSONDecodeError as e: + raise e + global inited + inited = True + init() + @listener(is_plugin=True, outgoing=True, command="rate", description="Currency exchange rate plugin.", parameters=" ") async def rate(context): - while not inited: - await asyncio.sleep(1) - if not context.parameter: - await context.edit(f"This is the currency exchange rate plugin.\n\nUsage: `-rate `\n\nAvailable currencies: {', '.join(currencies)}") - return - if len(context.parameter) != 2: - await context.edit(f"Usage: `-rate `\n\n`{', '.join(currencies)}`") - return - FROM = context.parameter[0].upper().strip() - TO = context.parameter[1].upper().strip() - if currencies.count(FROM) == 0: - await context.edit(f"Currency type {FROM} is not supported. Choose one among `{', '.join(currencies)}` instead.") - return - if currencies.count(TO) == 0: - await context.edit(f"Currency type {TO} is not supported. Choose one among `{', '.join(currencies)}` instead.") - return - await context.edit(f'{FROM} : {TO} = 1 : {int(10000*data["rates"][FROM]/data["rates"][TO])/10000}') - + while not inited: + await asyncio.sleep(1) + if not context.parameter: + await context.edit( + f"This is the currency exchange rate plugin.\n\nUsage: `-rate `\n\nAvailable currencies: {', '.join(currencies)}") + return + if len(context.parameter) != 2: + await context.edit(f"Usage: `-rate `\n\n`{', '.join(currencies)}`") + return + FROM = context.parameter[0].upper().strip() + TO = context.parameter[1].upper().strip() + if currencies.count(FROM) == 0: + await context.edit( + f"Currency type {FROM} is not supported. Choose one among `{', '.join(currencies)}` instead.") + return + if currencies.count(TO) == 0: + await context.edit(f"Currency type {TO} is not supported. Choose one among `{', '.join(currencies)}` instead.") + return + await context.edit(f'{FROM} : {TO} = 1 : {int(10000*data["rates"][TO]/data["rates"][FROM])/10000}')