# -*- coding: utf-8 -*-
import requests, sys, io, re, pathlib, hashlib
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52")
CACHE = BASE / "cache"; CACHE.mkdir(exist_ok=True)
H = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36",
     "Accept-Language": "en-US,en;q=0.9"}

def fetch(tag, url):
    p = CACHE / (tag + ".html")
    if p.exists() and p.stat().st_size > 1000:
        return p.read_text(encoding="utf-8", errors="ignore"), "cache"
    r = requests.get(url, headers=H, timeout=25, allow_redirects=True)
    p.write_text(r.text, encoding="utf-8", errors="ignore")
    return r.text, str(r.status_code)

def text(h):
    h = re.sub(r"<script.*?</script>|<style.*?</style>|<nav.*?</nav>", " ", h, flags=re.S)
    h = re.sub(r"<[^>]+>", " ", h)
    return re.sub(r"\s+", " ", h)

# 1. NYU bulletin arts-science programs index -> find CS slug
idx, st = fetch("r346-nyu-gsas-programs", "https://bulletins.nyu.edu/graduate/arts-science/programs/")
print("index", st, len(idx))
for m in re.finditer(r'href="([^"]*programs/[^"]*)"[^>]*>(.*?)</a>', idx, re.S):
    href, name = m.group(1), re.sub(r"<[^>]+>", "", m.group(2)).strip()
    if any(k in name.lower() for k in ["computer", "data", "information", "math"]):
        print(" ", name, "->", href)
