From 949df1e1025b6ce1448fe72acabfaca78f7275cf Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 28 Dec 2019 02:23:30 +0100 Subject: [PATCH] Upgrade modules --- TelegramEDT/__init__.py | 3 +- TelegramEDT/basic.py | 11 ++-- TelegramEDT/edt.py | 11 ++-- TelegramEDT/{notif.py => edt_notif.py} | 78 +++++++++++++------------- TelegramEDT/kfet.py | 11 ++-- TelegramEDT/loop.py | 9 +++ TelegramEDT/modules.py | 11 ++-- TelegramEDT/tomuss.py | 11 ++-- TelegramEDT/tools.py | 13 +++-- main.py | 5 +- 10 files changed, 94 insertions(+), 69 deletions(-) rename TelegramEDT/{notif.py => edt_notif.py} (67%) create mode 100644 TelegramEDT/loop.py diff --git a/TelegramEDT/__init__.py b/TelegramEDT/__init__.py index 9fe0620..42d6fb5 100644 --- a/TelegramEDT/__init__.py +++ b/TelegramEDT/__init__.py @@ -48,6 +48,7 @@ def check_id(user: types.User): logger.info("Start loading modules") from TelegramEDT.modules import load_module -for m in ["modules", "basic", "edt", "kfet", "tomuss", "notif", "tools"]: +modules = list() +for m in ["modules", "basic", "edt", "kfet", "tomuss", "edt_notif", "tools"]: load_module(m) logger.info("Modules loading finish") diff --git a/TelegramEDT/basic.py b/TelegramEDT/basic.py index 1e3b074..274bac5 100644 --- a/TelegramEDT/basic.py +++ b/TelegramEDT/basic.py @@ -1,11 +1,12 @@ from aiogram import types from aiogram.types import ParseMode -from TelegramEDT import dp, key, logger, Session, check_id +from TelegramEDT import dp, key, logger, Session, check_id, modules from TelegramEDT.base import User from TelegramEDT.lang import lang -logger = logger.getChild("basic") +module_name = "basic" +logger = logger.getChild(module_name) async def start(message: types.Message): @@ -31,12 +32,14 @@ async def help_cmd(message: types.Message): def load(): - logger.info("Load basic module") + logger.info(f"Load {module_name} module") dp.register_message_handler(start, commands="start") dp.register_message_handler(help_cmd, commands="help") + modules.append(module_name) def unload(): - logger.info("Unload basic module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(start) dp.message_handlers.unregister(help_cmd) + modules.remove(module_name) diff --git a/TelegramEDT/edt.py b/TelegramEDT/edt.py index 44d3c05..e3b2a54 100644 --- a/TelegramEDT/edt.py +++ b/TelegramEDT/edt.py @@ -11,12 +11,13 @@ from ics.parse import ParseError, string_to_container from pyzbar.pyzbar import decode from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema -from TelegramEDT import API_TOKEN, TIMES, bot, dp, key, logger, Session, check_id, posts_cb +from TelegramEDT import API_TOKEN, TIMES, bot, dp, key, logger, Session, check_id, posts_cb, modules from TelegramEDT.EDTcalendar import Calendar from TelegramEDT.base import User from TelegramEDT.lang import lang -logger = logger.getChild("edt") +module_name = "edt" +logger = logger.getChild(module_name) re_url = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+") @@ -136,7 +137,7 @@ async def edt_geturl(message: types.Message): def load(): - logger.info("Load edt module") + logger.info(f"Load {module_name} module") dp.register_message_handler(edt_cmd, lambda msg: msg.text.lower() == "edt") dp.register_inline_handler(inline_edt) dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"])) @@ -144,13 +145,15 @@ def load(): 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") + modules.append(module_name) def unload(): - logger.info("Unload edt module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(edt_cmd) 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) + modules.remove(module_name) diff --git a/TelegramEDT/notif.py b/TelegramEDT/edt_notif.py similarity index 67% rename from TelegramEDT/notif.py rename to TelegramEDT/edt_notif.py index 172e3ca..746078f 100644 --- a/TelegramEDT/notif.py +++ b/TelegramEDT/edt_notif.py @@ -1,14 +1,13 @@ -from asyncio import sleep - from aiogram import types from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode from aiogram.utils import markdown -from TelegramEDT import bot, dp, logger, posts_cb, Session, check_id, key +from TelegramEDT import bot, dp, logger, posts_cb, Session, check_id, key, modules from TelegramEDT.base import User from TelegramEDT.lang import lang -logger = logger.getChild("notif") +module_name = "edt_notif" +logger = logger.getChild(module_name) def have_await_cmd(msg: types.Message): @@ -18,41 +17,38 @@ def have_await_cmd(msg: types.Message): async def notif(): - while True: - with Session as session: - for u in session.query(User).all(): - nt = None - kf = None - tm = None - try: - nt = u.get_notif() - kf = u.get_kfet() - tm = u.get_tomuss() - except Exception as e: - logger.error(e) + with Session as session: + for u in session.query(User).all(): + nt = None + kf = None + tm = None + try: + nt = u.get_notif() + kf = u.get_kfet() + tm = u.get_tomuss() + except Exception as e: + logger.error(e) - if nt: - await bot.send_message(u.id, lang(u, "notif_event")+str(nt), parse_mode=ParseMode.MARKDOWN) - if kf: - if kf == 1: - kf = lang(u, "kfet") - elif kf == 2: - kf = lang(u, "kfet_prb") - else: - kf = lang(u, "kfet_err") - await bot.send_message(u.id, kf, parse_mode=ParseMode.MARKDOWN) - if tm: - for i in tm: - msg = markdown.text( - markdown.bold(i.title), - markdown.code(i.summary.replace("
", "\n").replace("", "").replace("", "")), - sep="\n" - ) - await bot.send_message(u.id, msg, parse_mode=ParseMode.MARKDOWN) - u.tomuss_last = str(i) - session.commit() - - await sleep(60) + if nt: + await bot.send_message(u.id, lang(u, "notif_event")+str(nt), parse_mode=ParseMode.MARKDOWN) + if kf: + if kf == 1: + kf = lang(u, "kfet") + elif kf == 2: + kf = lang(u, "kfet_prb") + else: + kf = lang(u, "kfet_err") + await bot.send_message(u.id, kf, parse_mode=ParseMode.MARKDOWN) + if tm: + for i in tm: + msg = markdown.text( + markdown.bold(i.title), + markdown.code(i.summary.replace("
", "\n").replace("", "").replace("", "")), + sep="\n" + ) + await bot.send_message(u.id, msg, parse_mode=ParseMode.MARKDOWN) + u.tomuss_last = str(i) + session.commit() async def notif_cmd(message: types.Message): @@ -114,14 +110,16 @@ async def await_cmd(message: types.message): def load(): - logger.info("Load notif module") + logger.info(f"Load {module_name} 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)) + modules.append(module_name) def unload(): - logger.info("Unload notif module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(notif_cmd) dp.callback_query_handlers.unregister(notif_query) dp.message_handlers.unregister(await_cmd) + modules.remove(module_name) diff --git a/TelegramEDT/kfet.py b/TelegramEDT/kfet.py index a84ac4d..22e43f8 100644 --- a/TelegramEDT/kfet.py +++ b/TelegramEDT/kfet.py @@ -5,11 +5,12 @@ from aiogram import types from aiogram.types import ParseMode from aiogram.utils import markdown -from TelegramEDT import dp, key, logger, Session, check_id +from TelegramEDT import dp, key, logger, Session, check_id, modules from TelegramEDT.base import User, KFET_URL from TelegramEDT.lang import lang -logger = logger.getChild("kfet") +module_name = "kfet" +logger = logger.getChild(module_name) def get_now(): @@ -78,14 +79,16 @@ async def await_cmd(message: types.message): def load(): - logger.info("Load kfet module") + logger.info(f"Load {module_name} 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)) + modules.append(module_name) def unload(): - logger.info("Unload kfet module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(kfet) dp.message_handlers.unregister(kfet_set) dp.message_handlers.unregister(await_cmd) + modules.remove(module_name) diff --git a/TelegramEDT/loop.py b/TelegramEDT/loop.py new file mode 100644 index 0000000..8efd044 --- /dev/null +++ b/TelegramEDT/loop.py @@ -0,0 +1,9 @@ +from asyncio import sleep + +from TelegramEDT.edt_notif import notif + + +async def main(): + while True: + await notif() + await sleep(30) diff --git a/TelegramEDT/modules.py b/TelegramEDT/modules.py index a5ec952..7c5dad4 100644 --- a/TelegramEDT/modules.py +++ b/TelegramEDT/modules.py @@ -2,9 +2,10 @@ from importlib import import_module from aiogram.types import Message -from TelegramEDT import ADMIN_ID, dp, logger +from TelegramEDT import ADMIN_ID, dp, logger, modules -logger = logger.getChild("modules") +module_name = "modules" +logger = logger.getChild(module_name) def load_module(module: str) -> bool: @@ -62,12 +63,14 @@ async def unload_cmd(message: Message): def load(): - logger.info("Load modules module") + logger.info(f"Load {module_name} module") dp.register_message_handler(load_cmd, commands="load") dp.register_message_handler(unload_cmd, commands="unload") + modules.append(module_name) def unload(): - logger.info("Unload tools module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(load_cmd) dp.message_handlers.unregister(unload_cmd) + modules.remove(module_name) diff --git a/TelegramEDT/tomuss.py b/TelegramEDT/tomuss.py index f49b9e4..903a8b7 100644 --- a/TelegramEDT/tomuss.py +++ b/TelegramEDT/tomuss.py @@ -2,11 +2,12 @@ from aiogram import types from aiogram.types import ParseMode from feedparser import parse -from TelegramEDT import dp, key, logger, Session, check_id +from TelegramEDT import dp, key, logger, Session, check_id, modules from TelegramEDT.base import User from TelegramEDT.lang import lang -logger = logger.getChild("tomuss") +module_name = "tomuss" +logger = logger.getChild(module_name) def have_await_cmd(msg: types.Message): @@ -46,12 +47,14 @@ async def await_cmd(message: types.message): def load(): - logger.info("Load tomuss module") + logger.info(f"Load {module_name} module") dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss") dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg)) + modules.append(module_name) def unload(): - logger.info("Unload tomuss module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(settomuss) dp.message_handlers.unregister(await_cmd) + modules.remove(module_name) diff --git a/TelegramEDT/tools.py b/TelegramEDT/tools.py index 435af5d..bbae2bb 100644 --- a/TelegramEDT/tools.py +++ b/TelegramEDT/tools.py @@ -1,14 +1,13 @@ -import datetime - from aiogram import types from aiogram.types import ParseMode from aiogram.utils import markdown from aiogram.utils.exceptions import MessageIsTooLong -from TelegramEDT import ADMIN_ID, bot, dp, key, logger, Session, check_id +from TelegramEDT import ADMIN_ID, bot, dp, key, logger, Session, check_id, modules from TelegramEDT.base import User -logger = logger.getChild("tools") +module_name = "tools" +logger = logger.getChild(module_name) async def get_id(message: types.Message): @@ -87,18 +86,20 @@ async def errors(*args, **partial_data): def load(): - logger.info("Load tools module") + logger.info(f"Load {module_name} module") dp.register_message_handler(get_id, commands="getid") dp.register_message_handler(get_logs, commands="getlogs") dp.register_message_handler(get_db, commands="getdb") dp.register_message_handler(eval_cmd, commands="eval") dp.register_errors_handler(errors) + modules.append(module_name) def unload(): - logger.info("Unload tools module") + logger.info(f"Unload {module_name} module") dp.message_handlers.unregister(get_id) dp.message_handlers.unregister(get_logs) dp.message_handlers.unregister(get_db) dp.message_handlers.unregister(eval_cmd) dp.errors_handlers.unregister(errors) + modules.remove(module_name) diff --git a/main.py b/main.py index ec7c3de..c8c7f4d 100644 --- a/main.py +++ b/main.py @@ -2,9 +2,10 @@ import asyncio from aiogram.utils import executor -from TelegramEDT.notif import notif, dp +from TelegramEDT import dp +from TelegramEDT.loop import main loop = asyncio.get_event_loop() -loop.create_task(notif()) +loop.create_task(main()) loop.create_task(executor.start_polling(dp, skip_updates=True)) loop.run_forever()