#!/usr/bin/env python3
"""Find prompt_id(s) in ComfyUI history by SaveVideo filename_prefix substring."""
import json, sys, urllib.request

prefix = sys.argv[1]
h = json.load(urllib.request.urlopen("http://127.0.0.1:8188/history?max_items=200", timeout=15))
for pid, e in h.items():
    for nid, n in e.get("prompt", [None, None, {}])[2].items():
        if "Save" in n.get("class_type", ""):
            fp = str(n.get("inputs", {}).get("filename_prefix", ""))
            if prefix in fp:
                msgs = e.get("status", {}).get("messages", [])
                ts = [m[1] for m in msgs if m and m[0] in ("execution_start", "execution_success")]
                print(json.dumps({"prompt_id": pid, "prefix": fp, "status": e.get("status", {}).get("status_str"),
                                  "start_end": ts}))
