#!/bin/bash # 显示下载任务状态 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" cd "$SCRIPT_DIR" STATE_FILE="download_state.json" # 检查状态文件是否存在 if [ ! -f "$STATE_FILE" ]; then echo "⚠️ 状态文件不存在,服务可能尚未初始化" exit 0 fi # 使用 Python 解析 JSON 并显示状态 python3 << 'EOF' import json import os STATE_FILE = "download_state.json" try: with open(STATE_FILE, 'r', encoding='utf-8') as f: tasks = json.load(f) if not tasks: print("📋 暂无下载任务") exit(0) # 统计状态 status_count = {} for task in tasks: status = task.get('status', 'unknown') status_count[status] = status_count.get(status, 0) + 1 # 显示统计 total = len(tasks) print(f"📊 总计: {total} 个任务") print() status_map = { 'pending': ('⏳ 等待中', 'yellow'), 'downloading': ('⬇️ 下载中', 'blue'), 'retrying': ('🔄 重试中', 'orange'), 'completed': ('✅ 已完成', 'green'), 'failed': ('❌ 失败', 'red'), } for status, (label, _) in status_map.items(): count = status_count.get(status, 0) if count > 0: print(f" {label}: {count}") print() print("📋 任务详情:") print("-" * 80) # 显示每个任务 for i, task in enumerate(tasks, 1): url = task.get('url', '') status = task.get('status', 'unknown') filename = task.get('filename', '') size = task.get('size', 0) error = task.get('error', '') retry = task.get('retry', 0) # 状态图标 status_icons = { 'pending': '⏳', 'downloading': '⬇️ ', 'retrying': '🔄', 'completed': '✅', 'failed': '❌', } icon = status_icons.get(status, '❓') # 格式化大小 def format_size(bytes_size): if bytes_size == 0: return '-' for unit in ['B', 'KB', 'MB', 'GB']: if bytes_size < 1024: return f"{bytes_size:.2f} {unit}" bytes_size /= 1024 return f"{bytes_size:.2f} TB" # URL 缩短显示 short_url = url if len(url) <= 60 else url[:57] + "..." print(f"{i:2d}. {icon} {status}") print(f" URL: {short_url}") if filename: print(f" 文件: {filename} ({format_size(size)})") if retry > 0: print(f" 重试: {retry} 次") if error: print(f" 错误: {error}") print() except FileNotFoundError: print("⚠️ 状态文件不存在") except json.JSONDecodeError: print("⚠️ 状态文件格式错误") except Exception as e: print(f"⚠️ 读取状态失败: {e}") EOF