This commit is contained in:
2026-01-18 05:38:14 +08:00
parent e510c568b4
commit 7e92e12c79
5 changed files with 438 additions and 132 deletions

View File

@@ -113,7 +113,7 @@ def preload_all_account_ids() -> tuple[int, int]:
Returns:
tuple: (success_count, fail_count)
"""
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
import sys
success_count = 0
fail_count = 0
@@ -127,42 +127,71 @@ def preload_all_account_ids() -> tuple[int, int]:
log.success(f"所有 Team account_id 已缓存 ({len(teams_with_token)} 个)")
return len(teams_with_token), 0
total = len(teams_with_token)
log.info(f"预加载 {len(teams_need_fetch)} 个 Team 的 account_id...", icon="sync")
need_save = False
failed_teams = []
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
TextColumn("{task.fields[status]}"),
) as progress:
task = progress.add_task("加载中", total=len(teams_with_token), status="")
# 检测是否为 TTY 环境
is_tty = sys.stdout.isatty()
for team in teams_with_token:
progress.update(task, description=f"[cyan]{team['name']}", status="")
if is_tty:
# TTY 环境: 使用 rich 进度条
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
TextColumn("{task.fields[status]}"),
) as progress:
task = progress.add_task("加载中", total=total, status="")
for team in teams_with_token:
progress.update(task, description=f"[cyan]{team['name']}", status="")
if team.get("account_id"):
success_count += 1
progress.update(task, advance=1, status="[green]✓ 已缓存")
continue
account_id = fetch_account_id(team, silent=True)
if account_id:
success_count += 1
progress.update(task, advance=1, status="[green]✓")
if team.get("format") == "new":
need_save = True
else:
fail_count += 1
failed_teams.append(team['name'])
progress.update(task, advance=1, status="[red]✗")
else:
# 非 TTY 环境 (systemd/journalctl): 使用普通日志输出
for idx, team in enumerate(teams_with_token, 1):
team_name = team['name']
if team.get("account_id"):
success_count += 1
progress.update(task, advance=1, status="[green]✓ 已缓存")
log.info(f"预加载 [{idx}/{total}] {team_name}: ✓ 已缓存")
continue
account_id = fetch_account_id(team, silent=True)
if account_id:
success_count += 1
progress.update(task, advance=1, status="[green]✓")
log.info(f"预加载 [{idx}/{total}] {team_name}: ✓ {account_id}")
if team.get("format") == "new":
need_save = True
else:
fail_count += 1
failed_teams.append(team['name'])
progress.update(task, advance=1, status="[red]✗")
failed_teams.append(team_name)
log.warning(f"预加载 [{idx}/{total}] {team_name}: ✗ 失败")
# 输出失败的 team
for name in failed_teams:
log.warning(f"Team {name}: 获取 account_id 失败")
# 输出失败的 team (仅 TTY 环境,非 TTY 已在循环中输出)
if is_tty:
for name in failed_teams:
log.warning(f"Team {name}: 获取 account_id 失败")
# 持久化到 team.json
if need_save: