This commit is contained in:
+67
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
首次登录脚本 —— 完成 Telegram 手机号 + 验证码认证,生成 session 文件。
|
||||
只需运行一次,之后重启容器无需重新登录。
|
||||
|
||||
用法:
|
||||
docker exec -it <容器名> python3 /app/do_login.py
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
from pyrogram import Client
|
||||
|
||||
|
||||
def load_config(path: str = "config.yaml") -> dict:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
|
||||
|
||||
async def main():
|
||||
config_path = os.path.join(os.path.abspath("."), "config.yaml")
|
||||
if not os.path.exists(config_path):
|
||||
print(f"错误:找不到配置文件 {config_path}")
|
||||
sys.exit(1)
|
||||
|
||||
cfg = load_config(config_path)
|
||||
api_id = cfg.get("api_id")
|
||||
api_hash = cfg.get("api_hash")
|
||||
|
||||
if not api_id or not api_hash:
|
||||
print("错误:config.yaml 中缺少 api_id 或 api_hash")
|
||||
sys.exit(1)
|
||||
|
||||
session_dir = os.path.join(os.path.abspath("."), "sessions")
|
||||
os.makedirs(session_dir, exist_ok=True)
|
||||
session_file = os.path.join(session_dir, "media_downloader")
|
||||
|
||||
proxy_cfg = cfg.get("proxy")
|
||||
proxy = None
|
||||
if proxy_cfg:
|
||||
proxy = {
|
||||
"scheme": proxy_cfg.get("scheme", "socks5"),
|
||||
"hostname": proxy_cfg.get("hostname", "127.0.0.1"),
|
||||
"port": proxy_cfg.get("port", 1080),
|
||||
}
|
||||
|
||||
print("=== Telegram 首次登录 ===")
|
||||
print(f"Session 文件将保存到:{session_file}.session")
|
||||
print()
|
||||
|
||||
client = Client(
|
||||
session_file,
|
||||
api_id=api_id,
|
||||
api_hash=api_hash,
|
||||
proxy=proxy,
|
||||
)
|
||||
|
||||
async with client:
|
||||
me = await client.get_me()
|
||||
print(f"\n登录成功!账号:{me.first_name} (@{me.username})")
|
||||
print("Session 文件已保存,后续启动无需重新登录。")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user