From 58015d3ca0767d9dd87f9aac575c8992f62ea726 Mon Sep 17 00:00:00 2001 From: qqwbarta <157809264+qqwbarta@users.noreply.github.com> Date: Fri, 26 Jan 2024 19:19:26 +0300 Subject: [PATCH 1/2] update for pyrogram v2 --- cleaner.py | 78 ++++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/cleaner.py b/cleaner.py index befaaa3..6078b9e 100644 --- a/cleaner.py +++ b/cleaner.py @@ -24,7 +24,6 @@ API_HASH = os.getenv('API_HASH', None) or input('Enter your Telegram API hash: ') app = Client("client", api_id=API_ID, api_hash=API_HASH) -app.start() if not os.path.exists(cachePath): with open(cachePath, "w") as cacheFile: @@ -53,19 +52,16 @@ def chunks(l, n): yield l[i:i + n] @staticmethod - def get_all_chats(): - dialogs = app.get_dialogs(pinned_only=True) + async def get_all_chats(): + async with app: + dialogs = [] + async for dialog in app.get_dialogs(): + dialogs.append(dialog.chat) + return dialogs - dialog_chunk = app.get_dialogs() - while len(dialog_chunk) > 0: - dialogs.extend(dialog_chunk) - dialog_chunk = app.get_dialogs(offset_date=dialogs[-1].top_message.date-1) - - return [d.chat for d in dialogs] - - def select_groups(self, recursive=0): - chats = self.get_all_chats() - groups = [c for c in chats if c.type in ('group', 'supergroup')] + async def select_groups(self, recursive=0): + chats = await self.get_all_chats() + groups = [c for c in chats if c.type.name in ('GROUP, SUPERGROUP')] print('Delete all your messages in') for i, group in enumerate(groups): @@ -101,60 +97,48 @@ def select_groups(self, recursive=0): if recursive == 1: self.run() - def run(self): + async def run(self): for chat in self.chats: - peer = app.resolve_peer(chat.id) + chat_id = chat.id message_ids = [] add_offset = 0 while True: - q = self.search_messages(peer, add_offset) - message_ids.extend(msg.id for msg in q['messages']) - messages_count = len(q['messages']) - print(f'Found {messages_count} of your messages in "{chat.title}"') + q = await self.search_messages(chat_id, add_offset) + message_ids.extend(msg.id for msg in q) + messages_count = len(q) + print(f'Found {len(message_ids)} of your messages in "{chat.title}"') if messages_count < self.search_chunk_size: break add_offset += self.search_chunk_size - self.delete_messages(chat.id, message_ids) + await self.delete_messages(chat_id=chat.id, message_ids=message_ids) - def delete_messages(self, chat_id, message_ids): + async def delete_messages(self, chat_id, message_ids): print(f'Deleting {len(message_ids)} messages with message IDs:') print(message_ids) for chunk in self.chunks(message_ids, self.delete_chunk_size): try: - app.delete_messages(chat_id=chat_id, message_ids=chunk) + async with app: + await app.delete_messages(chat_id=chat_id, message_ids=chunk) except FloodWait as flood_exception: sleep(flood_exception.x) - def search_messages(self, peer, add_offset): - print(f'Searching messages. OFFSET: {add_offset}') - return app.send( - Search( - peer=peer, - q='', - filter=InputMessagesFilterEmpty(), - min_date=0, - max_date=0, - offset_id=0, - add_offset=add_offset, - limit=self.search_chunk_size, - max_id=0, - min_id=0, - hash=0, - from_id=InputPeerSelf() - ), - sleep_threshold=60 - ) + async def search_messages(self, chat_id, add_offset): + async with app: + messages = [] + print(f'Searching messages. OFFSET: {add_offset}') + async for message in app.search_messages(chat_id=chat_id, offset=add_offset, from_user="me", limit=100): + messages.append(message) + return messages - -if __name__ == '__main__': +async def main(): try: deleter = Cleaner() - deleter.select_groups() - deleter.run() + await deleter.select_groups() + await deleter.run() except UnknownError as e: print(f'UnknownError occured: {e}') print('Probably API has changed, ask developers to update this utility') - finally: - app.stop() + +app.run(main()) \ No newline at end of file From 858554e3bb4e093a6ed7b820f26e6f8f56efed3c Mon Sep 17 00:00:00 2001 From: qqwbarta <157809264+qqwbarta@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:23:09 +0300 Subject: [PATCH 2/2] update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2cbc440..baedf99 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -pyrogram==1.4 +pyrogram==2.0.106 tgcrypto # Optional, for faster cleaning. You could remove this line \ No newline at end of file