From 1785e22ea163ee079b1b79e9d1fa12e23c51c268 Mon Sep 17 00:00:00 2001 From: anton <1537206@mail.ru> Date: Tue, 7 Oct 2025 15:06:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B8=203=20=D0=BC=D0=B8=D0=BD.=20=D0=BD=D0=B0=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=B6=D0=B4=D1=8B=D0=B5=201000=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=BE=D0=B2=20user=5Finfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 2 +- chat_reader.py | 11 ++-- reader_module.py | 158 ++++++++++++++++++++++++++--------------------- requirements.txt | 6 +- 4 files changed, 99 insertions(+), 78 deletions(-) diff --git a/LICENSE b/LICENSE index 03fb64b..5d0e329 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright © 2021-2022 https://t.me/ssleg +Copyright © 2021-2025 https://t.me/ssleg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/chat_reader.py b/chat_reader.py index ad651f7..bc21440 100755 --- a/chat_reader.py +++ b/chat_reader.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -# Telegram chat reader v1.11 -# 02/02/2022 -# https://t.me/ssleg © 2021-2022 +# Telegram chat reader v1.20 +# 07/10/2025 +# https://t.me/ssleg © 2021-2025 import logging @@ -101,7 +101,7 @@ def stat_upload(read_mess): 'Content-Type': 'application/json; charset=utf-8' } # noinspection HttpUrlsUsage - stat_upload_url = 'http://188.124.50.148/stat_up' + stat_upload_url = 'https://api.ssleg.tech/v1/stat_up' finger = str(api_id) + api_hash hash_md5 = md5(finger.encode()) request_json = {'protocol_version': '1.2', 'application': 'Chat Reader', 'app_version': get_version(), @@ -387,7 +387,8 @@ def add_private(): async for dialog in client.iter_dialogs(): entity = dialog.entity if type(entity) == types.Channel: - if entity.broadcast is False and entity.username is None and entity.has_link is False: + # if entity.broadcast is False and entity.username is None and entity.has_link is False: + if entity.broadcast is False and entity.username is None: count += 1 print(f'{count}. {entity.title} ({entity.participants_count} человек)') chats_dict[count] = (entity.id, entity.title) diff --git a/reader_module.py b/reader_module.py index 6c68566..9396996 100644 --- a/reader_module.py +++ b/reader_module.py @@ -1,6 +1,6 @@ -# Chat reader module v1.10 -# 29/01/2022 -# https://t.me/ssleg © 2021-2022 +# Chat reader module v1.20 +# 07/10/2025 +# https://t.me/ssleg © 2021-2025 import logging from asyncio import sleep @@ -11,6 +11,7 @@ from telethon import TelegramClient, types, errors user_dict = {} channel_dict = {} +get_user_count = 0 client: TelegramClient con: extensions.connection @@ -19,13 +20,22 @@ cursor: extensions.cursor # обновление информации о пользователях async def update_user(user_id): + global get_user_count + if get_user_count == 999: + levent = 'пауза по лимиту на запрос юзер инфо.' + print(levent) + logging.info(levent) + await sleep(180) + get_user_count = 0 + user_info = await client.get_entity(user_id) user_dict[user_id] = 0 + get_user_count += 1 entry = (user_id, user_info.first_name, user_info.last_name, user_info.username, user_info.phone, user_info.bot, user_info.deleted) cursor.execute('''insert into chat_reader_users (user_id, first_name, last_name, user_name, phone, is_bot, is_dead) - values (%s, %s, %s, %s, %s, %s, %s)''', entry) + values (%s, %s, %s, %s, %s, %s, %s)''', entry) con.commit() @@ -38,7 +48,7 @@ async def update_channel(channel_id): entry = (channel_id, channel_info.title, channel_info.username) cursor.execute('''insert into chat_reader_channels (channel_id, title, user_name) - values (%s, %s, %s)''', entry) + values (%s, %s, %s)''', entry) except errors.ChannelPrivateError: entry = (channel_id, 'PRIVATE_CHANNEL') @@ -56,77 +66,87 @@ async def update_channel(channel_id): async def read_messages(chat_id, start_id, limit): read_count = 0 last_read_id = 0 - async for message in client.iter_messages(chat_id, reverse=True, limit=limit, min_id=start_id): - read_count += 1 - message_id = message.id - last_read_id = message_id - message_date = message.date - message_text = message.text - if type(message.peer_id) == types.PeerChat: - message_chat_id = message.peer_id.chat_id - else: - message_chat_id = message.peer_id.channel_id - - message_from = message.from_id - user_id = None - channel_id = None - if type(message_from) == types.PeerChannel: - channel_id = message_from.channel_id - if channel_id not in channel_dict: - await update_channel(channel_id) - elif type(message_from) == types.PeerUser: - user_id = message_from.user_id - if user_id not in user_dict: - await update_user(user_id) - - media_type = None - if message.media is not None: - media_type = str(message.media) - - reply_to = None - reply_top = None - if message.reply_to is not None: - reply_to = message.reply_to.reply_to_msg_id - reply_top = message.reply_to.reply_to_top_id - if message.reply_to.reply_to_peer_id is not None: - levent = f'message_peer: {message.reply_to}, {message_id}' - print(levent) - logging.warning(levent) - - fwd_from_user_id = None - fwd_from_channel_id = None - fwd_from_post = None - fwd_from_name = None - if message.fwd_from is not None: - fwd_from = message.fwd_from.from_id - if type(fwd_from) == types.PeerChannel: - fwd_from_channel_id = message.fwd_from.from_id.channel_id - fwd_from_post = message.fwd_from.channel_post - if fwd_from_channel_id not in channel_dict: - await update_channel(fwd_from_channel_id) + try: + async for message in client.iter_messages(chat_id, reverse=True, limit=limit, min_id=start_id): + read_count += 1 + message_id = message.id + last_read_id = message_id + message_date = message.date + message_text = message.text + if type(message.peer_id) == types.PeerChat: + message_chat_id = message.peer_id.chat_id else: - if message.fwd_from.from_id is None: - fwd_from_name = message.fwd_from.from_name + message_chat_id = message.peer_id.channel_id + + message_from = message.from_id + user_id = None + channel_id = None + if type(message_from) == types.PeerChannel: + channel_id = message_from.channel_id + if channel_id not in channel_dict: + await update_channel(channel_id) + elif type(message_from) == types.PeerUser: + user_id = message_from.user_id + if user_id not in user_dict: + await update_user(user_id) + + media_type = None + if message.media is not None: + media_type = str(message.media) + + reply_to = None + reply_top = None + if message.reply_to is not None: + reply_to = message.reply_to.reply_to_msg_id + reply_top = message.reply_to.reply_to_top_id + if message.reply_to.reply_to_peer_id is not None: + levent = f'message_peer: {message.reply_to}, {message_id}' + # print(levent) + logging.warning(levent) + + fwd_from_user_id = None + fwd_from_channel_id = None + fwd_from_post = None + fwd_from_name = None + if message.fwd_from is not None: + fwd_from = message.fwd_from.from_id + if type(fwd_from) == types.PeerChannel: + fwd_from_channel_id = message.fwd_from.from_id.channel_id + fwd_from_post = message.fwd_from.channel_post + if fwd_from_channel_id not in channel_dict: + await update_channel(fwd_from_channel_id) else: - fwd_from_user_id = message.fwd_from.from_id.user_id - if fwd_from_user_id not in user_dict: - await update_user(fwd_from_user_id) + if message.fwd_from.from_id is None: + fwd_from_name = message.fwd_from.from_name + else: + fwd_from_user_id = message.fwd_from.from_id.user_id + if fwd_from_user_id not in user_dict: + await update_user(fwd_from_user_id) - action = None - if message.action is not None: - action = str(message.action) + action = None + if message.action is not None: + action = str(message.action) - mess_grouped_id = message.grouped_id + mess_grouped_id = message.grouped_id - entry = (message_chat_id, message_id, user_id, channel_id, message_date, mess_grouped_id, reply_to, reply_top, - fwd_from_channel_id, fwd_from_post, fwd_from_user_id, fwd_from_name, message_text, media_type, action) + entry = ( + message_chat_id, message_id, user_id, channel_id, message_date, mess_grouped_id, reply_to, reply_top, + fwd_from_channel_id, fwd_from_post, fwd_from_user_id, fwd_from_name, message_text, media_type, action) - cursor.execute('''insert into chat_reader_mess (chat_id, message_id, user_id, channel_id, message_date, - grouped_id, reply_to, reply_top, fwd_from_channel_id, fwd_from_channel_post, fwd_from_user_id, - fwd_from_name, message_txt, message_media, message_action) - values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', entry) + cursor.execute('''insert into chat_reader_mess (chat_id, message_id, user_id, channel_id, message_date, + grouped_id, reply_to, reply_top, fwd_from_channel_id, + fwd_from_channel_post, fwd_from_user_id, + fwd_from_name, message_txt, message_media, message_action) + values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', entry) + + con.commit() + + except errors.ChannelPrivateError: + print('чат стал приватным/удален/аккаунт для чтения забанен.') + + except errors.ChannelInvalidError: + print('приватный чат не знаком с другим аккаунтом для чтения.') - con.commit() return read_count, last_read_id diff --git a/requirements.txt b/requirements.txt index 736a266..062c759 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -psycopg2~=2.8.6 +psycopg2~=2.9.9 toml~=0.10.2 -Telethon~=1.24.0 -requests~=2.23.0 \ No newline at end of file +Telethon~=1.36.0 +requests~=2.25.1 \ No newline at end of file