#!/usr/bin/env python3
"""Fetch app.curogram.com index, extract every <script src>, download each
bundle, and grep for the LoginPage enum definition + its member values.
Read-only, no creds."""
import urllib.request, re
ORIGIN = "https://app.curogram.com/"
def get(u):
    req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
    return urllib.request.urlopen(req, timeout=15).read().decode("utf-8", "replace")
idx = get(ORIGIN)
srcs = re.findall(r'<script[^>]+src=["\']([^"\']+)["\']', idx)
# also modulepreload
srcs += re.findall(r'<link[^>]+rel=["\']modulepreload["\'][^>]+href=["\']([^"\']+)["\']', idx)
print("scripts_in_index", len(srcs), srcs[:10])
# also pull lazy chunk filenames referenced by main bundle
all_urls = set()
for s in srcs:
    u = s if s.startswith("http") else ORIGIN + s.lstrip("/")
    all_urls.add(u)
# download each main script; collect referenced .js filenames to fetch lazy chunks
lazy = set()
texts = {}
for u in list(all_urls):
    try:
        t = get(u); texts[u] = t
    except Exception as e:
        print("skip", u.split("/")[-1], e); continue
    for fn in re.findall(r'["\']([\w.-]+\.js)["\']', t):
        lazy.add(ORIGIN + fn)
print("lazy_referenced", len(lazy))
for u in list(lazy):
    if u in texts: continue
    try:
        texts[u] = get(u)
    except Exception:
        pass
enum_re = re.compile(r'.{0,40}LoginPage.{0,260}')
found = 0
for u, t in texts.items():
    if "LoginPage" not in t: continue
    for m in enum_re.finditer(t):
        s = re.sub(r"\s+", " ", m.group(0))
        # skip the mutation query strings (they contain 'login(' / 'mutation')
        if "mutation" in s or "login(" in s or "sendChangePassword" in s or "setPassword" in s:
            continue
        print("ENUM", u.split("/")[-1], s[:300])
        found += 1
        if found > 12: break
    if found > 12: break
print("total_bundles", len(texts), "enum_hits", found)
