# -*- coding: utf-8 -*-
import json, re, io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = r"D:\work buddy\2026-11-05-14-15-52"
rep = json.load(open(BASE + r"\_density_report.json", encoding="utf-8"))
s = open(BASE + r"\index_main.html", encoding="utf-8").read()
seg = s[s.index("const PROGRAMS=["):s.index("const CATD=[")]
parts = seg.split('{id:"')[1:]
info = {}
for p in parts:
    def g(k):
        m = re.search(k + r':"((?:[^"\\]|\\.)*)"', p)
        return m.group(1) if m else ""
    info[p[:p.index('"')]] = {k: g(k) for k in ("s", "n", "en", "dept", "college", "disc", "lvl", "deg", "url")}
byid = {r["id"]: r for r in rep["rows"]}
need = sys.argv[1] if len(sys.argv) > 1 else "jhu"
for pid, it in sorted(info.items()):
    r = byid.get(pid)
    if not r or r["grade"] == "A":
        continue
    if not pid.startswith(need + "-"):
        continue
    print("%-24s %d %s | %-8s | %-14s | %s" % (pid, r["score"], r["grade"], it["disc"], it["deg"][:14], it["url"][:95]))
