From d7dc5362c04d911e7fe78280e4e6d01963de491d Mon Sep 17 00:00:00 2001 From: Pavel Kirilin <win10@list.ru> Date: Tue, 30 Jun 2020 03:58:04 +0400 Subject: [PATCH] Added currency autoconversion. Signed-off-by: Pavel Kirilin <win10@list.ru> --- src/actions/__init__.py | 1 + src/actions/converter.py | 56 ++++++++++++++++++++++++++++++++++++++++ static_messages/help.txt | 1 + 3 files changed, 58 insertions(+) create mode 100644 src/actions/converter.py diff --git a/src/actions/__init__.py b/src/actions/__init__.py index 147caf4..e7d88e2 100644 --- a/src/actions/__init__.py +++ b/src/actions/__init__.py @@ -6,6 +6,7 @@ def finish(): importlib.import_module("src.actions.basic") importlib.import_module("src.actions.fun") importlib.import_module("src.actions.replies") + importlib.import_module("src.actions.converter") importlib.import_module("src.actions.package_indexes") importlib.import_module("src.actions.search_engines") logging.info("All actions loaded") diff --git a/src/actions/converter.py b/src/actions/converter.py new file mode 100644 index 0000000..8876470 --- /dev/null +++ b/src/actions/converter.py @@ -0,0 +1,56 @@ +import logging +import re + +import httpx +from src.config import config +from src.utils.responses import mark_unread +from telethon import events + +logger = logging.getLogger(__name__) + +SUPPORTED_CURRENCIES = [ + "GBP", + "HUF", + "USD", + "EUR", + "CNY", + "NOK", + "UAH", + "SEK", + "CHF", + "KRW", + "JPY", +] +CURRENCY_PATTERN = rf"\s*((\d+)(\.\d+)?)\s+({'|'.join(SUPPORTED_CURRENCIES)})" + + +@config.telegram_client.on(events.NewMessage(pattern=f".*{CURRENCY_PATTERN}.*")) +@mark_unread +async def replace_currency(event): + async with httpx.AsyncClient() as f: + current_currency = await f.get("https://www.cbr-xml-daily.ru/daily_json.js") + response_json = current_currency.json() + currencies = response_json["Valute"] + groups = re.findall(CURRENCY_PATTERN, event.message.message) + response = "**ПолагаÑÑÑŒ на текщий ÐºÑƒÑ€Ñ Ð²Ð°Ð»ÑŽÑ‚ могу Ñказать Ñледующее:**\n\n" + for group in groups: + name = group[3] + val = float(group[0]) + new_val = val * currencies[name]["Value"] + response += f"`{val:.2f} {name} = {new_val:.2f} RUB`\n" + + await event.reply(response) + + +@config.telegram_client.on(events.NewMessage(pattern="^.sc$")) +@mark_unread +async def supported_currencies(event): + async with httpx.AsyncClient() as f: + current_currency = await f.get("https://www.cbr-xml-daily.ru/daily_json.js") + response_json = current_currency.json() + currencies = response_json["Valute"] + response = "**Поддерживаемые курÑÑ‹ валют**:\n\n" + for curr in SUPPORTED_CURRENCIES: + response += f"{curr} ({currencies[curr]['Name']})\n" + + await event.reply(response) diff --git a/static_messages/help.txt b/static_messages/help.txt index 6d11802..8a18520 100644 --- a/static_messages/help.txt +++ b/static_messages/help.txt @@ -13,6 +13,7 @@ * `.brew` -- Ð´Ð»Ñ MacOs Также умею ещё пару забавных штук. +`.sc` поÑмотреть поддерживаемые курÑÑ‹ валют Ð´Ð»Ñ Ð°Ð²Ñ‚Ð¾Ð¼Ð°Ñ‚Ð¸Ñ‡ÐµÑкой конвертации. `.i <запроÑ>` вернет пачку картинок Ñ Ð¸Ð½Ñ‚ÐµÑ€Ð½ÐµÑ‚Ð¸ÐºÐ¾Ð². * `.bl` <любой текÑÑ‚> - заменит вÑе запÑтые на вÑÑкие гадоÑти. * `.t` - показать текущее Ð²Ñ€ÐµÐ¼Ñ -- GitLab