Skip to content

Commit 7153831

Browse files
committed
feat: add get_acs_result.py script to download job results from API and gitignore output.zip
1 parent 64c43ce commit 7153831

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ evalate_threads/evaluate.py
2121
evalate_threads/quick_test.py
2222
evalate_threads/test_adk_intergration.py
2323
evaluation_results.json
24+
output.zip

scripts/get_acs_result.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import argparse
2+
import json
3+
import os
4+
import sys
5+
6+
import requests
7+
8+
9+
def download_file(url, output_path):
10+
"""下载文件并显示进度"""
11+
response = requests.get(url, stream=True)
12+
response.raise_for_status()
13+
14+
total_size = int(response.headers.get('content-length', 0))
15+
downloaded_size = 0
16+
17+
with open(output_path, 'wb') as f:
18+
for chunk in response.iter_content(chunk_size=8192):
19+
if chunk:
20+
f.write(chunk)
21+
downloaded_size += len(chunk)
22+
if total_size > 0:
23+
progress = (downloaded_size / total_size) * 100
24+
print(f"\rDownload progress: {progress:.1f}%", end='', flush=True)
25+
26+
print() # 换行
27+
28+
29+
def main():
30+
parser = argparse.ArgumentParser(description='Download job results from API')
31+
parser.add_argument('job_id', help='Job ID to fetch results for')
32+
parser.add_argument(
33+
'-o',
34+
'--output',
35+
default='output.zip',
36+
help='Output file path (default: output.zip)',
37+
)
38+
39+
args = parser.parse_args()
40+
41+
# 设置常量
42+
access_key = '7c4d4edd67284c2e9c62d8b9350baaa4'
43+
api_url = f"https://openapi.test.dp.tech/openapi/v1/sandbox/job/{args.job_id}?accessKey={access_key}"
44+
45+
# 删除已存在的输出文件
46+
if os.path.exists(args.output):
47+
os.remove(args.output)
48+
49+
# 调用API获取job信息
50+
print(f"Fetching job information for ID: {args.job_id}")
51+
52+
try:
53+
response = requests.get(api_url)
54+
response.raise_for_status()
55+
data = response.json()
56+
except requests.exceptions.RequestException as e:
57+
print(f"Error: Failed to fetch data from API: {e}")
58+
sys.exit(1)
59+
except json.JSONDecodeError as e:
60+
print(f"Error: Failed to parse JSON response: {e}")
61+
sys.exit(1)
62+
63+
print(f"\n{json.dumps(data, indent=2)}\n")
64+
65+
# 解析JSON获取resultUrl
66+
if data.get('code') == 0:
67+
result_url = data.get('data', {}).get('resultUrl', '')
68+
else:
69+
result_url = ''
70+
print(f"API returned error code: {data.get('code')}")
71+
72+
# 获取log文件的token(可选操作)
73+
token_url = 'https://openapi.test.dp.tech/openapi/v1/sandbox/job/file/token?accessKey=7c4d4edd67284c2e9c62d8b9350baaa4'
74+
token_data = {'filePath': 'log', 'jobId': args.job_id}
75+
76+
try:
77+
token_response = requests.post(token_url, json=token_data)
78+
token_response.raise_for_status()
79+
token_data = token_response.json()
80+
except requests.exceptions.RequestException:
81+
pass # 忽略token获取错误,因为这不是主要功能
82+
83+
print(f"\n{json.dumps(token_data, indent=2)}\n")
84+
85+
log_token = token_data.get('data', {}).get('token', '')
86+
log_path = token_data.get('data', {}).get('path', '')
87+
log_host = token_data.get('data', {}).get('host', '')
88+
89+
print(f"\nlog_path: {log_host}/api/download/{log_path}?token={log_token}\n")
90+
91+
# 下载结果文件
92+
if result_url and result_url != 'null':
93+
print(f"\nFound resultUrl: {result_url}")
94+
print(f"\nDownloading to: {args.output}")
95+
96+
try:
97+
download_file(result_url, args.output)
98+
99+
if os.path.exists(args.output) and os.path.getsize(args.output) > 0:
100+
file_size = os.path.getsize(args.output)
101+
print(f"Download completed successfully! File size: {file_size} bytes")
102+
else:
103+
print("Error: Download failed - file is empty or doesn't exist")
104+
sys.exit(1)
105+
106+
except requests.exceptions.RequestException as e:
107+
print(f"Error: Download failed: {e}")
108+
sys.exit(1)
109+
else:
110+
print('No resultUrl found or resultUrl is empty')
111+
sys.exit(1)
112+
113+
114+
if __name__ == '__main__':
115+
main()

0 commit comments

Comments
 (0)