From e57d0d8617b1d683f2d5555cb76bb0440f1adfa2 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 28 Dec 2019 02:41:51 +0100 Subject: [PATCH] Separate notification for support module unload --- TelegramEDT/edt_notif.py | 29 +++-------------------------- TelegramEDT/kfet.py | 20 +++++++++++++++++++- TelegramEDT/loop.py | 12 ++++++++++-- TelegramEDT/tomuss.py | 24 +++++++++++++++++++++++- 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/TelegramEDT/edt_notif.py b/TelegramEDT/edt_notif.py index 746078f..3e1d6d0 100644 --- a/TelegramEDT/edt_notif.py +++ b/TelegramEDT/edt_notif.py @@ -19,36 +19,13 @@ def have_await_cmd(msg: types.Message): async def notif(): 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() + else: + if nt: + await bot.send_message(u.id, lang(u, "notif_event")+str(nt), parse_mode=ParseMode.MARKDOWN) async def notif_cmd(message: types.Message): diff --git a/TelegramEDT/kfet.py b/TelegramEDT/kfet.py index 22e43f8..e9522db 100644 --- a/TelegramEDT/kfet.py +++ b/TelegramEDT/kfet.py @@ -5,7 +5,7 @@ from aiogram import types from aiogram.types import ParseMode from aiogram.utils import markdown -from TelegramEDT import dp, key, logger, Session, check_id, modules +from TelegramEDT import dp, key, logger, Session, check_id, modules, bot from TelegramEDT.base import User, KFET_URL from TelegramEDT.lang import lang @@ -78,6 +78,24 @@ async def await_cmd(message: types.message): await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) +async def notif(): + with Session as session: + for u in session.query(User).all(): + try: + kf = u.get_kfet() + except Exception as e: + logger.error(e) + else: + if kf is not None: + 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) + + def load(): logger.info(f"Load {module_name} module") dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet") diff --git a/TelegramEDT/loop.py b/TelegramEDT/loop.py index 8efd044..59798a0 100644 --- a/TelegramEDT/loop.py +++ b/TelegramEDT/loop.py @@ -1,9 +1,17 @@ from asyncio import sleep -from TelegramEDT.edt_notif import notif +from TelegramEDT import modules +from TelegramEDT.edt_notif import notif as edt_notif +from TelegramEDT.kfet import notif as kfet_notif +from TelegramEDT.tomuss import notif as tomuss_notif async def main(): while True: - await notif() + if "edt_notif" in modules: + await edt_notif() + if "kfet" in modules: + await kfet_notif() + if "tomuss" in modules: + await tomuss_notif() await sleep(30) diff --git a/TelegramEDT/tomuss.py b/TelegramEDT/tomuss.py index 903a8b7..ff1e601 100644 --- a/TelegramEDT/tomuss.py +++ b/TelegramEDT/tomuss.py @@ -1,8 +1,9 @@ from aiogram import types from aiogram.types import ParseMode +from aiogram.utils import markdown from feedparser import parse -from TelegramEDT import dp, key, logger, Session, check_id, modules +from TelegramEDT import dp, key, logger, Session, check_id, modules, bot from TelegramEDT.base import User from TelegramEDT.lang import lang @@ -39,6 +40,7 @@ async def await_cmd(message: types.message): msg = lang(user, "settomuss_error") else: user.tomuss_rss = message.text + user.tomuss_last = str() msg = lang(user, "settomuss") user.await_cmd = str() session.commit() @@ -46,6 +48,26 @@ async def await_cmd(message: types.message): await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) +async def notif(): + with Session as session: + for u in session.query(User).all(): + try: + tm = u.get_tomuss() + except Exception as e: + logger.error(e) + else: + 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() + + def load(): logger.info(f"Load {module_name} module") dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss")