#!/usr/bin/env python3
"""Cari nomor WhatsApp di berkas workspace dan/atau seluruh halaman situs TSC.

Pakai: python3 tsc_lacak_nomor2.py file          → hanya berkas workspace
       python3 tsc_lacak_nomor2.py web [nomor]   → hanya halaman situs
"""
import os, re, sys, time, urllib.request

NOMOR = ["6281335114324", "6285117444417", "6285194936363", "6281935000624"]
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
DOM = "theskincodeclinic.com"
UA = {"User-Agent": "Mozilla/5.0 (compatible; AtunBot/1.0)"}
MODE = sys.argv[1] if len(sys.argv) > 1 else "file"

if MODE == "file":
    print("=== nomor di berkas workspace ===")
    hit = 0
    for akar, dirs, files in os.walk(WS):
        if any(x in akar for x in ("/.git", "/__pycache__", "/Agentic-SEO-Skill", "/sessions")):
            continue
        for f in files:
            if not f.endswith((".py", ".md", ".json", ".txt", ".html", ".csv")):
                continue
            p = os.path.join(akar, f)
            try:
                if os.path.getsize(p) > 4_000_000:
                    continue
                isi = open(p, errors="replace").read()
            except Exception:
                continue
            for n in NOMOR:
                if n in isi:
                    baris = [i + 1 for i, l in enumerate(isi.splitlines()) if n in l]
                    print(f"   {n} → {p.replace(WS, 'workspace')} (baris {baris[:5]})")
                    hit += 1
    print(f"   total temuan: {hit}")
    sys.exit(0)

# MODE web
print("=== nomor di halaman situs (dari sitemap) ===")
try:
    sm = urllib.request.urlopen(urllib.request.Request(f"https://www.{DOM}/sitemap_index.xml", headers=UA),
                               timeout=60).read().decode("utf-8", "replace")
    sub = re.findall(r"<loc>([^<]+)</loc>", sm)
except Exception as e:
    print("   sitemap gagal:", str(e)[:80]); sub = []
urls = []
for s in sub:
    try:
        x = urllib.request.urlopen(urllib.request.Request(s, headers=UA), timeout=60).read().decode("utf-8", "replace")
        urls += re.findall(r"<loc>([^<]+)</loc>", x)
    except Exception:
        continue
urls = sorted(set(u for u in urls if u.startswith("http")))
print(f"   {len(urls)} URL diperiksa")

ketemu, gagal = [], 0
for i, u in enumerate(urls, 1):
    try:
        t = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=30).read().decode("utf-8", "replace")
    except Exception:
        gagal += 1
        continue
    kena = sorted({n for n in NOMOR if n in t})
    if kena:
        ketemu.append((u, kena))
    if i % 20 == 0:
        print(f"   ...{i}/{len(urls)} (temuan {len(ketemu)}, gagal {gagal})", flush=True)

print(f"\n   halaman memuat nomor: {len(ketemu)} | gagal dibuka: {gagal}")
for u, kena in ketemu:
    print(f"   {', '.join(kena):<34} {u}")
