|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +from pathlib import Path |
| 4 | +import sys |
| 5 | + |
| 6 | +from loguru import logger |
| 7 | + |
| 8 | +from .config import Settings |
| 9 | +from .locking import state_lock, AlreadyRunning |
| 10 | +from .registry import inventory |
| 11 | + |
| 12 | + |
| 13 | +def parser(): |
| 14 | + root = argparse.ArgumentParser(description="Telegram 登录与单次签到;定时由青龙控制。") |
| 15 | + root.add_argument("--config", type=Path, default=Path(__file__).resolve().parents[1] / "config.toml") |
| 16 | + sub = root.add_subparsers(dest="command") |
| 17 | + sub.add_parser("list-sites", help="列出内置签到器") |
| 18 | + run = sub.add_parser("run", help="签到一次后退出(默认)") |
| 19 | + run.add_argument("--sites", help="本次站点,逗号分隔;也支持 templ_a<bot_username>") |
| 20 | + run.add_argument("--account", help="只运行一个账号") |
| 21 | + status = sub.add_parser("status", help="只检查现有会话,不发送签到消息") |
| 22 | + status.add_argument("--account") |
| 23 | + login = sub.add_parser("login", help="使用 Telethon 交互式首次登录") |
| 24 | + login.add_argument("--account", required=True) |
| 25 | + login.add_argument("--replace", action="store_true", help="明确创建一个新会话并备份原会话") |
| 26 | + migration = sub.add_parser("migrate-embykeeper", help="在本机读取原配置和会话;先停用旧任务") |
| 27 | + migration.add_argument("--source", type=Path, required=True) |
| 28 | + return root |
| 29 | + |
| 30 | + |
| 31 | +async def execute(args, settings, accounts): |
| 32 | + from .sessions import connected, login, check_unique_sessions |
| 33 | + if args.command == "login": |
| 34 | + await login(settings, accounts[0], args.replace) |
| 35 | + return 0 |
| 36 | + if args.command == "status": |
| 37 | + check_unique_sessions(settings, accounts) |
| 38 | + for account in accounts: |
| 39 | + async with connected(settings, account): |
| 40 | + logger.info("{}: 现有 Telegram 会话有效。", account.name) |
| 41 | + return 0 |
| 42 | + from .runner import run |
| 43 | + override = [s.strip() for s in args.sites.split(",") if s.strip()] if getattr(args, "sites", None) else None |
| 44 | + return await run(settings, accounts, override) |
| 45 | + |
| 46 | + |
| 47 | +def main(argv=None): |
| 48 | + logger.remove() |
| 49 | + logger.add(sys.stdout, format="{time:YYYY-MM-DD HH:mm:ss} | {level: <7} | {message}", colorize=False) |
| 50 | + args = parser().parse_args(argv) |
| 51 | + try: |
| 52 | + if args.command == "list-sites": |
| 53 | + for item in inventory(): |
| 54 | + print(item["id"]) |
| 55 | + raise SystemExit(0) |
| 56 | + if args.command == "migrate-embykeeper": |
| 57 | + from .migrate import migrate |
| 58 | + count = migrate(args.source, args.config) |
| 59 | + logger.info("已迁移 {} 个账号的配置和会话;没有发起新登录。", count) |
| 60 | + raise SystemExit(0) |
| 61 | + settings = Settings.load(args.config) |
| 62 | + accounts = [a for a in settings.accounts if a.enabled and |
| 63 | + (not getattr(args, "account", None) or a.name == args.account)] |
| 64 | + if not accounts: |
| 65 | + raise ValueError("没有匹配的启用账号。") |
| 66 | + with state_lock(settings.state_dir): |
| 67 | + code = asyncio.run(execute(args, settings, accounts)) |
| 68 | + raise SystemExit(code) |
| 69 | + except AlreadyRunning as exc: |
| 70 | + logger.warning("{}", exc) |
| 71 | + raise SystemExit(75) |
| 72 | + except KeyboardInterrupt: |
| 73 | + logger.warning("任务被中断。") |
| 74 | + raise SystemExit(130) |
| 75 | + except Exception as exc: |
| 76 | + # No traceback locals, session strings, phone numbers or credentials in normal logs. |
| 77 | + logger.error("{}: {}", type(exc).__name__, exc) |
| 78 | + raise SystemExit(1) |
0 commit comments