Therealobe, Так перепиши в чем проблема, согласен что на телетон последнее время сессии слетают сам пирограм давно юзаю
import logging
from pirogram import Client, filters
from datetime import datetime, timedelta
import asyncio
# Define the necessary variables: API ID, API Hash, phone number, and session name
api_id = 'тут id'
api_hash = 'тут хэш'
phone_number = 'тут номер'
session_name = 'Parser'
# Configure the logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Create the client
client = Client(session_name, api_id=api_id, api_hash=api_hash, phone_number=phone_number)
async def main():
# Connect to Telegram
await client.start()
if not await client.is_user_authorized():
await client.send_code(phone_number)
code = input("Enter the code received: ")
await client.sign_in(phone_number, code)
# Get information about all chats
dialogs = await client.get_dialogs(limit=10)
# Display the list of chats and prompt for a selection
print("Chats:")
for i, dialog in enumerate(dialogs, start=1):
print(f"{i}. {dialog.chat.title}")
chat_index = int(input("Enter the number of the chat to parse: ")) - 1
# Get the selected chat
selected_chat = dialogs[chat_index].chat
# Calculate the date one week ago from today
one_week_ago = datetime.now() - timedelta(days=7)
# Get all messages in the chat within the past week
usernames = []
limit = 100
# Open the file for writing
with open('usernames.txt', 'w') as f:
try:
async for message in client.iter_history(selected_chat.id, limit=limit, offset_date=one_week_ago):
if message.from_user is not None:
username = message.from_user.username
if username and username not in usernames:
usernames.append(username)
# Log the username in real-time
logging.info(f"Username found: @{username}")
# Write the usernames to the file, each on a new line with "@" symbol
f.write('@' + username + '\n')
# Display a message about successful writing
print('Usernames saved to file usernames.txt')
except Exception as e:
print(f"An error occurred: {str(e)}")
# Log the successful execution of the script
logging.info('Script executed successfully.')
# Disconnect the client
await client.stop()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())