import re, os, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
base = os.path.dirname(os.path.abspath(__file__))
html = open(os.path.join(base, 'index_main.html'), encoding='utf-8').read()
m = re.search(r'const PROGRAMS\s*=\s*\[(.*?)\];', html, re.S)
body = m.group(1)
blocks = dict(re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|$)', body))

def g(rest, key):
    mm = re.search(key+r':"([^"]*)"', rest)
    return mm.group(1) if mm else ''

priority = ["gatech-cs-ms","gatech-ece-ms","gatech-qcf-ms","gatech-analytics-ms",
            "gatech-stat-ms","gatech-me-ms","gatech-bme-ms","gatech-mgmt-ms"]
for tid in priority:
    rest = blocks.get(tid)
    if not rest: print(tid, "NOT FOUND"); continue
    url = g(rest,'url')
    det = os.path.join(base, url.replace('/', os.sep))
    links = []
    if os.path.exists(det):
        dh = open(det, encoding='utf-8', errors='ignore').read()
        links = [L for L in re.findall(r'https?://[^"\'<> ]+', dh)
                 if not L.startswith('file://') and 'google' not in L and 'fonts' not in L and 'cdn' not in L]
    print(f"{tid} | {g(rest,'n')}")
    for L in links[:4]:
        print(f"   {L}")
    print()
