Files
vesti/crawler/auth_telegram.py

50 lines
1.7 KiB
Python

# VESTI — разовая интерактивная авторизация Telethon-сессии (MTProto).
# Запуск: .venv/bin/python -m crawler.auth_telegram
# После успеха сессия /opt/vesti/telegram/vesti.session сохраняется (644? -> 600).
import asyncio
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from config import TG_API_ID, TG_API_HASH, TG_PROXY, TG_SESSION_DIR, ensure_dirs
from crawler.telegram_crawler import make_client
from telethon import errors
import logging
logging.basicConfig(level=logging.INFO)
async def main():
ensure_dirs()
TG_SESSION_DIR.mkdir(parents=True, exist_ok=True)
client = make_client(TG_SESSION_DIR)
await client.connect()
if await client.is_user_authorized():
me = await client.get_me()
print(f"Уже авторизован: @{me.username or me.phone}")
await client.disconnect()
return
phone = input("Телефон (с кодом страны, +7...): ").strip()
if not phone:
print("Нет телефона.")
return
try:
await client.send_code_request(phone)
code = input("Код из Telegram: ").strip()
# 2FA пароль, если включён
try:
await client.sign_in(phone, code)
except errors.SessionPasswordNeededError:
pwd = input("2FA-пароль: ").strip()
await client.sign_in(password=pwd)
me = await client.get_me()
print(f"Авторизован: @{me.username or me.phone}")
except Exception as e:
print(f"Ошибка: {e}")
finally:
await client.disconnect()
if __name__ == "__main__":
asyncio.run(main())