From e58b04f7d823ea1a671e26ebdb134edf9058e37e Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 26 Dec 2019 14:42:34 +0100 Subject: [PATCH] Separate await_cmd for eah module --- TelegramEDT/__init__.py | 2 +- TelegramEDT/await_cmd.py | 105 --------------------------------------- TelegramEDT/edt.py | 59 ++++++++++++++++++++-- TelegramEDT/kfet.py | 26 ++++++++++ TelegramEDT/notif.py | 32 +++++++++++- TelegramEDT/tomuss.py | 25 ++++++++++ 6 files changed, 139 insertions(+), 110 deletions(-) delete mode 100644 TelegramEDT/await_cmd.py diff --git a/TelegramEDT/__init__.py b/TelegramEDT/__init__.py index f2cd541..e54622c 100644 --- a/TelegramEDT/__init__.py +++ b/TelegramEDT/__init__.py @@ -69,6 +69,6 @@ dp.register_message_handler(load_cmd, commands="load") dp.register_message_handler(unload_cmd, commands="unload") logger.info("Start loading modules") -for m in ["basic", "edt", "kfet", "tomuss", "notif", "await_cmd", "tools"]: +for m in ["basic", "edt", "kfet", "tomuss", "notif", "tools"]: load_module(m) logger.info("Modules loading finish") diff --git a/TelegramEDT/await_cmd.py b/TelegramEDT/await_cmd.py deleted file mode 100644 index 9de987d..0000000 --- a/TelegramEDT/await_cmd.py +++ /dev/null @@ -1,105 +0,0 @@ -import re - -import requests -from PIL import Image -from aiogram import types -from aiogram.types import ParseMode, ContentType -from feedparser import parse -from ics.parse import ParseError -from pyzbar.pyzbar import decode -from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema - -from TelegramEDT import API_TOKEN, bot, dbL, dp, key, logger, session, check_id -from TelegramEDT.EDTcalendar import Calendar -from TelegramEDT.base import User -from TelegramEDT.lang import lang - -re_url = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+") -logger = logger.getChild("await_cmd") - - -def have_await_cmd(msg: types.Message): - with dbL: - user = session.query(User).filter_by(id=msg.from_user.id).first() - return user and user.await_cmd - - -async def await_cmd(message: types.message): - check_id(message.from_user) - await message.chat.do(types.ChatActions.TYPING) - msg = None - with dbL: - user = session.query(User).filter_by(id=message.from_user.id).first() - logger.info(f"{message.from_user.username} do awaited commande: {user.await_cmd}") - if user.await_cmd == "setedt": - url = str() - if message.photo: - file_path = await bot.get_file(message.photo[0].file_id) - file_url = f"https://api.telegram.org/file/bot{API_TOKEN}/{file_path['file_path']}" - qr = decode(Image.open(requests.get(file_url, stream=True).raw)) - if qr: - url = str(qr[0].data) - elif message.text: - msg_url = re_url.findall(message.text) - if msg_url: - url = msg_url[0] - - if url: - resources = url[url.find("resources") + 10:][:4] - elif message.text: - resources = message.text - - try: - Calendar("", int(resources)) - except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError, UnboundLocalError): - msg = lang(user, "setedt_err_res") - else: - user.resources = int(resources) - msg = lang(user, "setedt") - - elif user.await_cmd == "setkfet": - try: - int(message.text) - except ValueError: - msg = lang(user, "err_num") - else: - user.kfet = int(message.text) - msg = lang(user, "kfet_set") - - elif user.await_cmd == "settomuss": - if not len(parse(message.text).entries): - msg = lang(user, "settomuss_error") - else: - user.tomuss_rss = message.text - msg = lang(user, "settomuss") - - elif user.await_cmd in ["time", "cooldown"]: - try: - value = int(message.text) - except ValueError: - msg = lang(user, "err_num") - else: - if user.await_cmd == "time": - user.nt_time = value - else: - user.nt_cooldown = value - - msg = lang(user, "notif_time_cooldown").format(user.await_cmd[6:], value) - - if user.await_cmd: - user.await_cmd = str() - session.commit() - - if msg: - await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) - - -def load(): - logger.info("Load await_cmd module") - dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg),content_types=[ContentType.TEXT, - ContentType.PHOTO]) - - -def unload(): - logger.info("Unload await_cmd module") - dp.message_handlers.unregister(await_cmd) diff --git a/TelegramEDT/edt.py b/TelegramEDT/edt.py index 47edf33..bb84ec1 100644 --- a/TelegramEDT/edt.py +++ b/TelegramEDT/edt.py @@ -1,14 +1,23 @@ import hashlib +import re +import requests +from PIL import Image from aiogram import types -from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode, InputTextMessageContent, \ - InlineQueryResultArticle, InlineQuery +from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InputTextMessageContent, \ + InlineQueryResultArticle, InlineQuery, ContentType +from aiogram.types import ParseMode +from ics.parse import ParseError +from pyzbar.pyzbar import decode +from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema -from TelegramEDT import dbL, dp, key, logger, posts_cb, session, TIMES, bot, check_id +from TelegramEDT import API_TOKEN, TIMES, bot, dbL, dp, key, logger, session, check_id, posts_cb +from TelegramEDT.EDTcalendar import Calendar from TelegramEDT.base import User from TelegramEDT.lang import lang logger = logger.getChild("edt") +re_url = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+") def calendar(time: str, user_id: int): @@ -28,6 +37,12 @@ def edt_key(): return keys +def have_await_cmd(msg: types.Message): + with dbL: + user = session.query(User).filter_by(id=msg.from_user.id).first() + return user and user.await_cmd == "setedt" + + async def edt_cmd(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) @@ -69,6 +84,41 @@ async def edt_await(message: types.Message): await message.reply(lang(user, "setedt_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) +async def await_cmd(message: types.message): + check_id(message.from_user) + await message.chat.do(types.ChatActions.TYPING) + with dbL: + user = session.query(User).filter_by(id=message.from_user.id).first() + logger.info(f"{message.from_user.username} do edt awaited command") + url = str() + if message.photo: + file_path = await bot.get_file(message.photo[0].file_id) + file_url = f"https://api.telegram.org/file/bot{API_TOKEN}/{file_path['file_path']}" + qr = decode(Image.open(requests.get(file_url, stream=True).raw)) + if qr: + url = str(qr[0].data) + elif message.text: + msg_url = re_url.findall(message.text) + if msg_url: + url = msg_url[0] + + if url: + resources = url[url.find("resources") + 10:][:4] + elif message.text: + resources = message.text + + try: + Calendar("", int(resources)) + except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError, UnboundLocalError): + msg = lang(user, "setedt_err_res") + else: + user.resources = int(resources) + msg = lang(user, "setedt") + user.await_cmd = str() + session.commit() + await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + async def edt_geturl(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) @@ -87,6 +137,8 @@ def load(): dp.register_inline_handler(inline_edt) dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"])) dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt") + dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg), content_types=[ContentType.TEXT, + ContentType.PHOTO]) dp.register_message_handler(edt_geturl, commands="getedt") @@ -96,4 +148,5 @@ def unload(): dp.inline_query_handlers.unregister(inline_edt) dp.callback_query_handlers.unregister(edt_query) dp.message_handlers.unregister(edt_await) + dp.message_handlers.unregister(await_cmd) dp.message_handlers.unregister(edt_geturl) diff --git a/TelegramEDT/kfet.py b/TelegramEDT/kfet.py index 2367ec4..69727de 100644 --- a/TelegramEDT/kfet.py +++ b/TelegramEDT/kfet.py @@ -16,6 +16,12 @@ def get_now(): return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None) +def have_await_cmd(msg: types.Message): + with dbL: + user = session.query(User).filter_by(id=msg.from_user.id).first() + return user and user.await_cmd == "setkfet" + + async def kfet(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) @@ -49,13 +55,33 @@ async def kfet_set(message: types.Message): await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) +async def await_cmd(message: types.message): + check_id(message.from_user) + await message.chat.do(types.ChatActions.TYPING) + with dbL: + user = session.query(User).filter_by(id=message.from_user.id).first() + logger.info(f"{message.from_user.username} do awaited command") + try: + int(message.text) + except ValueError: + msg = lang(user, "err_num") + else: + user.kfet = int(message.text) + msg = lang(user, "kfet_set") + user.await_cmd = str() + session.commit() + await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + def load(): logger.info("Load kfet module") dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet") dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet") + dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg)) def unload(): logger.info("Unload kfet module") dp.message_handlers.unregister(kfet) dp.message_handlers.unregister(kfet_set) + dp.message_handlers.unregister(await_cmd) diff --git a/TelegramEDT/notif.py b/TelegramEDT/notif.py index b79f000..fce448e 100644 --- a/TelegramEDT/notif.py +++ b/TelegramEDT/notif.py @@ -4,13 +4,19 @@ from aiogram import types from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode from aiogram.utils import markdown -from TelegramEDT import bot, dbL, dp, logger, posts_cb, session, check_id +from TelegramEDT import bot, dbL, dp, logger, posts_cb, session, check_id, key from TelegramEDT.base import User from TelegramEDT.lang import lang logger = logger.getChild("notif") +def have_await_cmd(msg: types.Message): + with dbL: + user = session.query(User).filter_by(id=msg.from_user.id).first() + return user and user.await_cmd in ["time", "cooldown"] + + async def notif(): while True: with dbL: @@ -85,13 +91,37 @@ async def notif_query(query: types.CallbackQuery, callback_data: dict): await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN) +async def await_cmd(message: types.message): + check_id(message.from_user) + await message.chat.do(types.ChatActions.TYPING) + with dbL: + user = session.query(User).filter_by(id=message.from_user.id).first() + logger.info(f"{message.from_user.username} do awaited command") + try: + value = int(message.text) + except ValueError: + msg = lang(user, "err_num") + else: + if user.await_cmd == "time": + user.nt_time = value + else: + user.nt_cooldown = value + + msg = lang(user, "notif_time_cooldown").format(user.await_cmd[6:], value) + user.await_cmd = str() + session.commit() + await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + def load(): logger.info("Load notif module") dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif") dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"])) + dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg)) def unload(): logger.info("Unload notif module") dp.message_handlers.unregister(notif_cmd) dp.callback_query_handlers.unregister(notif_query) + dp.message_handlers.unregister(await_cmd) diff --git a/TelegramEDT/tomuss.py b/TelegramEDT/tomuss.py index 04973ec..1f3aabf 100644 --- a/TelegramEDT/tomuss.py +++ b/TelegramEDT/tomuss.py @@ -1,5 +1,6 @@ from aiogram import types from aiogram.types import ParseMode +from feedparser import parse from TelegramEDT import dbL, dp, key, logger, session, check_id from TelegramEDT.base import User @@ -8,6 +9,12 @@ from TelegramEDT.lang import lang logger = logger.getChild("tomuss") +def have_await_cmd(msg: types.Message): + with dbL: + user = session.query(User).filter_by(id=msg.from_user.id).first() + return user and user.await_cmd == "settomuss" + + async def settomuss(message: types.Message): check_id(message.from_user) await message.chat.do(types.ChatActions.TYPING) @@ -20,11 +27,29 @@ async def settomuss(message: types.Message): await message.reply(lang(user, "settomuss_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) +async def await_cmd(message: types.message): + check_id(message.from_user) + await message.chat.do(types.ChatActions.TYPING) + with dbL: + user = session.query(User).filter_by(id=message.from_user.id).first() + logger.info(f"{message.from_user.username} do awaited command") + if not len(parse(message.text).entries): + msg = lang(user, "settomuss_error") + else: + user.tomuss_rss = message.text + msg = lang(user, "settomuss") + user.await_cmd = str() + session.commit() + await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + def load(): logger.info("Load tomuss module") dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss") + dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg)) def unload(): logger.info("Unload tomuss module") dp.message_handlers.unregister(settomuss) + dp.message_handlers.unregister(await_cmd)