From 6baf4bdd130e8f05b69d68469aaca205f88d083b Mon Sep 17 00:00:00 2001 From: fxxk-research <41339997+chaseyoungcn@users.noreply.github.com> Date: Thu, 19 Jun 2025 20:42:10 +0800 Subject: [PATCH] get total_messages speed up O(n) -> O(1) --- telegram-scraper.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/telegram-scraper.py b/telegram-scraper.py index 0b4f291..d90a3d0 100644 --- a/telegram-scraper.py +++ b/telegram-scraper.py @@ -162,11 +162,8 @@ async def scrape_channel(channel, offset_id): total_messages = 0 processed_messages = 0 - async for message in client.iter_messages(entity, offset_id=offset_id, reverse=True): - sys.stdout.write("\r\033[K") - sys.stdout.write(f"Counting messages in: {channel} - Messages found: {total_messages}") - sys.stdout.flush() - total_messages += 1 + result = await client.get_messages(entity, offset_id=offset_id, reverse=True, limit=0) + total_messages = result.total if total_messages == 0: print(f"No messages found in channel {channel}.")