Добавление задержки 3 мин. на каждые 1000 запросов user_info

master
anton 2025-10-07 15:06:51 +03:00
parent fd9d23f2d2
commit 1785e22ea1
Signed by: anton
GPG Key ID: 50F7E97F96C07ECF
4 changed files with 99 additions and 78 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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

View File

@ -1,4 +1,4 @@
psycopg2~=2.8.6
psycopg2~=2.9.9
toml~=0.10.2
Telethon~=1.24.0
requests~=2.23.0
Telethon~=1.36.0
requests~=2.25.1