Skip to content

Commit d37119a

Browse files
authored
Merge pull request #214 from AnguseZhang/chore/update-gitignore
feat: add ACS result download script with dotenv integration and JSON encoding fix
2 parents 64c43ce + e7bdb0b commit d37119a

2 files changed

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

0 commit comments

Comments
 (0)