Добавление задержки 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,8 +20,17 @@ 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)
@ -56,6 +66,7 @@ async def update_channel(channel_id):
async def read_messages(chat_id, start_id, limit):
read_count = 0
last_read_id = 0
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
@ -90,7 +101,7 @@ async def read_messages(chat_id, start_id, limit):
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)
# print(levent)
logging.warning(levent)
fwd_from_user_id = None
@ -118,15 +129,24 @@ async def read_messages(chat_id, start_id, limit):
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,
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,
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('приватный чат не знаком с другим аккаунтом для чтения.')
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