From aabc6d142ffb2af09ddca865b72bed122405b979 Mon Sep 17 00:00:00 2001 From: blank X Date: Wed, 10 Mar 2021 15:27:11 +0700 Subject: [PATCH] Implement log ring --- example-config.yaml | 1 + sukuinote/__init__.py | 2 ++ sukuinote/__main__.py | 20 ++++++++++++++++++-- sukuinote/plugins/log_forwards.py | 11 ++--------- sukuinote/plugins/log_reports.py | 11 ++--------- sukuinote/plugins/log_user_joins.py | 11 ++--------- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index df5edcc..705f694 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -16,3 +16,4 @@ config: log_user_adds: true log_reports: true log_forwards: true + log_ring_maxlen: 69420 diff --git a/sukuinote/__init__.py b/sukuinote/__init__.py index f4dbf9c..80414b4 100644 --- a/sukuinote/__init__.py +++ b/sukuinote/__init__.py @@ -8,6 +8,7 @@ import functools import mimetypes import yaml import aiohttp +from collections import deque from datetime import timedelta from pyrogram import Client, StopPropagation, ContinuePropagation from pyrogram.types import Chat, User @@ -19,6 +20,7 @@ with open('config.yaml') as config: config = yaml.safe_load(config) loop = asyncio.get_event_loop() help_dict = dict() +log_ring = deque(maxlen=config['config'].get('log_ring_maxlen', 69420)) apps = [] app_user_ids = dict() diff --git a/sukuinote/__main__.py b/sukuinote/__main__.py index a49b73b..9e54c9a 100644 --- a/sukuinote/__main__.py +++ b/sukuinote/__main__.py @@ -1,6 +1,7 @@ import asyncio from pyrogram import idle -from . import loop, apps, slave, app_user_ids, session +from pyrogram.errors.exceptions.flood_420 import FloodWait +from . import loop, apps, slave, app_user_ids, session, log_ring, config async def main(): async def _start_app(app): @@ -11,9 +12,24 @@ async def main(): try: me = await app.get_me() app_user_ids[me.id] = me - except: + except BaseException: pass await asyncio.sleep(60) + async def log_ring_worker(): + while True: + await asyncio.sleep(1) + try: + text = log_ring.popleft() + except IndexError: + pass + while True: + try: + await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True) + except FloodWait as ex: + await asyncio.sleep(ex.x + 1) + else: + break + asyncio.create_task(log_ring_worker()) await asyncio.gather(*(_start_app(app) for app in apps), slave.start()) await idle() await asyncio.gather(*(app.stop() for app in apps), slave.stop()) diff --git a/sukuinote/plugins/log_forwards.py b/sukuinote/plugins/log_forwards.py index 6cf0f50..ed4e36c 100644 --- a/sukuinote/plugins/log_forwards.py +++ b/sukuinote/plugins/log_forwards.py @@ -2,8 +2,7 @@ import html import asyncio from collections import defaultdict from pyrogram import Client, filters -from pyrogram.errors.exceptions.flood_420 import FloodWait -from .. import config, slave, log_errors, app_user_ids +from .. import config, log_errors, app_user_ids, log_ring logged = defaultdict(set) lock = asyncio.Lock() @@ -87,11 +86,5 @@ async def log_forwards(client, message): if forwardee.is_scam: user_text += ' [SCAM]' text += f'{user_text} [{forwardee.id}]' - while True: - try: - await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True) - except FloodWait as ex: - await asyncio.sleep(ex.x + 1) - else: - break + log_ring.append(text) logged[message.chat.id].add(message.message_id) diff --git a/sukuinote/plugins/log_reports.py b/sukuinote/plugins/log_reports.py index 7053632..6a74cec 100644 --- a/sukuinote/plugins/log_reports.py +++ b/sukuinote/plugins/log_reports.py @@ -2,8 +2,7 @@ import html import asyncio from collections import defaultdict from pyrogram import Client, filters -from pyrogram.errors.exceptions.flood_420 import FloodWait -from .. import config, slave, log_errors +from .. import config, log_errors, log_ring reported = defaultdict(set) lock = asyncio.Lock() @@ -92,12 +91,6 @@ async def log_reports(client, message): if mtext.strip(): text += ':' text += f' {html.escape(mtext.strip()[:1000])}' - while True: - try: - reply = await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True) - except FloodWait as ex: - await asyncio.sleep(ex.x + 1) - else: - break + log_ring.append(text) reported[message.chat.id].add(message.message_id) reported[reply.chat.id].add(reply.message_id) diff --git a/sukuinote/plugins/log_user_joins.py b/sukuinote/plugins/log_user_joins.py index f760201..eb4e8b7 100644 --- a/sukuinote/plugins/log_user_joins.py +++ b/sukuinote/plugins/log_user_joins.py @@ -2,9 +2,8 @@ import html import asyncio from collections import defaultdict from pyrogram import Client, ContinuePropagation -from pyrogram.errors.exceptions.flood_420 import FloodWait from pyrogram.raw.types import UpdateNewChannelMessage, UpdateNewMessage, MessageService, PeerChat, PeerChannel, MessageActionChatAddUser, MessageActionChatJoinedByLink, PeerUser -from .. import config, log_errors, slave +from .. import config, log_errors, log_ring def sexy_user_name(user): text = user.first_name @@ -59,13 +58,7 @@ async def log_user_joins(client, update, users, chats): text += f'- Adder: {adder}\n- Added Users:\n' for user in action.users: text += f'--- {sexy_user_name(users[user])}\n' - while True: - try: - await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True) - except FloodWait as ex: - await asyncio.sleep(ex.x + 1) - else: - break + log_ring.append(text) handled[sexy_chat_id].add(message.id) return raise ContinuePropagation