refactor: 统一CLI输出格式为标准JSON
This commit is contained in:
@@ -176,7 +176,10 @@ def main():
|
||||
result = extract_up(extractor, args.id, args.output)
|
||||
elif args.type == 'series':
|
||||
if not args.mid:
|
||||
print("错误: series类型需要提供 --mid 参数")
|
||||
print(json.dumps({
|
||||
"success": False,
|
||||
"error": "series类型需要提供 --mid 参数"
|
||||
}, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
result = extract_series(extractor, args.mid, args.id, args.output)
|
||||
elif args.type == 'readlist':
|
||||
@@ -184,37 +187,33 @@ def main():
|
||||
elif args.type == 'favlist':
|
||||
result = extract_favlist(extractor, args.id, args.output)
|
||||
else:
|
||||
print(f"错误: 不支持的类型 {args.type}")
|
||||
print(json.dumps({
|
||||
"success": False,
|
||||
"error": f"不支持的类型 {args.type}"
|
||||
}, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
# 包装为标准输出格式
|
||||
# 生成文件名
|
||||
platform = "bilibili"
|
||||
filename = f"{platform}_{args.id}.meta.json"
|
||||
filepath = args.output or filename
|
||||
|
||||
# 统一输出格式
|
||||
output = {
|
||||
"success": True,
|
||||
"data": result,
|
||||
"metadata": {
|
||||
"type": args.type,
|
||||
"id": args.id,
|
||||
"fetched_at": "2024-01-01T00:00:00"
|
||||
}
|
||||
"filename": filename,
|
||||
"filepath": str(Path(filepath).resolve()) if filepath else None,
|
||||
"message": f"{args.type}元数据提取成功"
|
||||
}
|
||||
|
||||
# 输出结果
|
||||
if not args.output:
|
||||
if args.pretty:
|
||||
print(json.dumps(output, ensure_ascii=False, indent=2))
|
||||
else:
|
||||
print(json.dumps(output, ensure_ascii=False))
|
||||
|
||||
if args.output:
|
||||
print(f"结果已保存到: {args.output}")
|
||||
print(json.dumps(output, ensure_ascii=False, indent=2))
|
||||
|
||||
except Exception as e:
|
||||
error_output = {
|
||||
print(json.dumps({
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"error_type": type(e).__name__
|
||||
}
|
||||
print(json.dumps(error_output, ensure_ascii=False, indent=2))
|
||||
"detail": type(e).__name__
|
||||
}, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user