import sys, xml.etree.ElementTree as ET
if len(sys.argv) != 2: print("usage: check-xml.py feed-or-item.xml"); sys.exit(2)
try:
    root = ET.parse(sys.argv[1]).getroot()
except OSError as e:
    print(f"cannot open {sys.argv[1]}: {e.strerror}"); sys.exit(2)
except ET.ParseError as e:
    print("not well-formed:", e); sys.exit(1)
ns = "{https://podcastindex.org/namespace/1.0}transcript"
tags = root.findall(".//item/" + ns)
print("transcript tags found:", len(tags))
bad = 0
for t in tags:
    print(t.get("type"), t.get("url"))
    for a in ("url", "type"):
        if not t.get(a): print(f"PROBLEM: a transcript tag has no {a} (required)"); bad += 1
if not tags:
    print("hint: the tag must be inside an <item>, not in <channel>, and the namespace must be exactly https://podcastindex.org/namespace/1.0 (https, not http)")
sys.exit(0 if tags and not bad else 1)
