import requests
import json
API_KEY = "<YOUR_API_KEY>"
API_URL = "https://api.olostep.com/v1"
# 步骤1:创建上传URL,目的为"batch"
create_response = requests.post(
f"{API_URL}/files",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json={"filename": "batch-items.json", "purpose": "batch"}
)
upload_data = create_response.json()
file_id = upload_data["id"]
upload_url = upload_data["upload_url"]
# 步骤2:准备批处理JSON数据(适用于/v1/batches端点的有效格式)
batch_data = {
"items": [
{"custom_id": "item-1", "url": "https://www.google.com/search?q=stripe&gl=us&hl=en"},
{"custom_id": "item-2", "url": "https://www.google.com/search?q=paddle&gl=us&hl=en"},
{"custom_id": "item-3", "url": "https://www.google.com/search?q=payment+gateway&gl=us&hl=en"}
],
"parser": {"id": "@olostep/google-search"},
"country": "US"
}
upload_response = requests.put(
upload_url,
data=json.dumps(batch_data),
headers={"Content-Type": "application/json"}
)
upload_response.raise_for_status()
# 步骤3:完成上传
complete_response = requests.post(
f"{API_URL}/files/{file_id}/complete",
headers={"Authorization": f"Bearer {API_KEY}"}
)
file_info = complete_response.json()
print(f"批处理文件上传成功:{file_info['id']}")
print(f"文件大小:{file_info['bytes']} 字节")
print(f"目的:{file_info['purpose']}")