#!/usr/bin/env python3
"""Minta WP Rocket membuat ulang CSS terpakai untuk halaman beranda (#409) setelah isi diubah."""
import base64, importlib.util, json, time, urllib.error, urllib.request

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg", f"{WS}/cfg_tsc.py")
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
CFG = m.CFG
DOM = CFG["domain"]
AUTH = base64.b64encode(f"{CFG['user']}:{CFG['pass']}".encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}

for pid in (409,):
    url = f"https://{DOM}/wp-json/wp-rocket/v1/cpcss/post/{pid}"
    r = urllib.request.Request(url, data=b"", headers={"Authorization": f"Basic {AUTH}", **UA}, method="POST")
    try:
        with urllib.request.urlopen(r, timeout=180) as x:
            print(f"  #{pid} → HTTP {x.status} {x.read()[:120]}")
    except urllib.error.HTTPError as e:
        print(f"  #{pid} → HTTP {e.code} {e.read()[:200]}")
    except Exception as e:
        print(f"  #{pid} → gagal {str(e)[:80]}")
    time.sleep(2)

# cek ulang halaman depan
time.sleep(10)
t = urllib.request.urlopen(urllib.request.Request(f"https://{DOM}/?ver={int(time.time())}", headers=UA),
                           timeout=90).read().decode("utf-8", "replace")
sisa = t.count("klinikdnisidoarjo.com")
print(f"\nrujukan domain lama di HTML beranda sekarang: {sisa}")
if sisa:
    print("   (masih ada; CSS terpakai atau cache halaman belum diperbarui)")
