54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
import asyncio
|
|
from datetime import datetime, timezone, timedelta
|
|
from telethon import TelegramClient
|
|
from telethon.tl.types import UserStatusOnline, UserStatusOffline
|
|
|
|
import utils.config
|
|
|
|
from utils.logging_setup import configure_global_logging
|
|
|
|
logger = configure_global_logging()
|
|
config = utils.config
|
|
|
|
# Список пользователей, которых отслеживаем
|
|
|
|
|
|
async def check_status(client, username):
|
|
try:
|
|
entity = await client.get_entity(username)
|
|
user_id = entity.id
|
|
username = entity.username or ''
|
|
user = await client.get_entity(user_id)
|
|
status = user.status
|
|
|
|
# 1. Получаем тип статуса
|
|
status_type = type(status).__name__
|
|
|
|
logger.info(f"\nСледим за: {username} (ID: {user_id})")
|
|
|
|
if status_type == 'UserStatusOffline':
|
|
was_online_utc = status.was_online
|
|
kiev_time = was_online_utc.astimezone(timezone(timedelta(hours=3)))
|
|
year, month, day = kiev_time.year, kiev_time.month, kiev_time.day
|
|
hour, minute, second = kiev_time.hour, kiev_time.minute, kiev_time.second
|
|
|
|
logger.info(f"Статус: {status_type}")
|
|
logger.info(f"Последний онлайн (Киев): {year}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}")
|
|
|
|
elif status_type == 'UserStatusOnline':
|
|
logger.info(f"Статус: {status_type} — Сейчас онлайн")
|
|
|
|
else:
|
|
logger.info(f"Статус: {status_type} — Время скрыто или неизвестно")
|
|
|
|
except Exception as e:
|
|
logger.info(f"⚠️ Ошибка при проверке {username}: {e}")
|
|
|
|
async def main():
|
|
async with TelegramClient('session', config.api_id, config.api_hash) as client:
|
|
tasks = [check_status(client, username) for username in config.target_usernames]
|
|
await asyncio.gather(*tasks)
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|