Call the Token Plan video generation API, submit a text-to-video task, wait for completion, and download the video.
Video description: $ARGUMENTS
## Steps
1. Extract the prompt (video description), model (default: happyhorse-1.1-t2v), resolution (default: 720P), ratio (default: 16:9), and duration (default: 5) from the description. If the description specifies a model, use that model.
2. Install dependencies: pip install requests
3. Use the Bash tool to run the following Python script. The script reads TOKEN_PLAN_API_KEY first, then ANTHROPIC_AUTH_TOKEN. Do not display the key or write it to files or logs.
```python
import json
import os
import sys
import time
from pathlib import Path
import requests
prompt = " ".join(sys.argv[1:])
key = os.environ.get("TOKEN_PLAN_API_KEY") or os.environ.get("ANTHROPIC_AUTH_TOKEN")
if not key:
raise SystemExit("Token Plan API Key not found.")
base_url = "https://token-plan.maas.qwencloudapi.com"
headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
response = requests.post(
f"{base_url}/api/v1/services/aigc/video-generation/video-synthesis",
headers={**headers, "X-DashScope-Async": "enable"},
json={
"model": "happyhorse-1.1-t2v",
"input": {"prompt": prompt},
"parameters": {"resolution": "720P", "ratio": "16:9", "duration": 5},
},
timeout=60,
)
if not response.ok:
raise SystemExit(f"{response.status_code}: {response.text}")
task_id = response.json()["output"]["task_id"]
print(f"Task submitted: {task_id}")
while True:
time.sleep(10)
result = requests.get(f"{base_url}/api/v1/tasks/{task_id}", headers=headers, timeout=60)
if not result.ok:
raise SystemExit(f"{result.status_code}: {result.text}")
data = result.json()
status = data["output"]["task_status"]
if status == "SUCCEEDED":
video_url = data["output"]["video_url"]
output = f"generated_{time.strftime('%Y%m%d_%H%M%S')}.mp4"
video = requests.get(video_url, timeout=120)
video.raise_for_status()
Path(output).write_bytes(video.content)
print(output)
break
if status in {"FAILED", "CANCELED", "UNKNOWN"}:
raise SystemExit(json.dumps(data, ensure_ascii=False, indent=2))
print(f"Task status: {status}")
```
4. Output the path of the downloaded video file.