#!/usr/bin/env python3
"""Verifikasi bebas (bukan lewat skrip generator) artikel TSC #10246 «Bekas Jerawat Cekung»."""
import base64, importlib.util, json, re, time, 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"}
PID = 10246

r = urllib.request.Request(f"https://{DOM}/wp-json/wp/v2/posts/{PID}?context=edit",
                           headers={"Authorization": f"Basic {AUTH}", **UA})
d = json.loads(urllib.request.urlopen(r, timeout=120).read())
html = d["content"]["raw"]
teks = re.sub(r"<[^>]+>", " ", html)

print(f"id      : {d['id']}")
print(f"judul   : {d['title']['raw']}")
print(f"slug    : {d['slug']}")
print(f"status  : {d['status']}")
print(f"date    : {d['date']}  (date_gmt {d['date_gmt']})")
print(f"kata    : {len(teks.split())}")
print(f"H2/H3   : {html.count('<h2')}/{html.count('<h3')}")
print(f"WA      : {'ada' if 'wa.me/6285117444417' in html else 'TIDAK ADA'}")
print(f"strong  : {html.count('<strong>')}")
tg = re.findall(r'href="(https://www\.theskincodeclinic\.com/[^"]+)"', html)
print(f"tautan internal ({len(tg)}): {tg}")
print(f"featured_media: {d.get('featured_media')}")
print("meta    :", json.dumps({k: v for k, v in (d.get("meta") or {}).items() if "yoast" in k}, ensure_ascii=False)[:300])

print("\n--- cek pola terlarang ---")
pola = {"em dash": "—", "kutip melengkung": r"[“”‘’]", "CJK": r"[\u3000-\u9fff\uac00-\ud7af]",
        "tanda seru": "!", "bukan sekadar": r"bukan sekadar", "tidak hanya..tetapi": r"tidak hanya.{0,40}tetapi"}
for nama, p in pola.items():
    n = re.findall(p, teks)
    print(f"  {nama}: {len(n)}")

# cek og:image + gambar unggulan live
time.sleep(3)
u = f"https://{DOM}/?p={PID}&cb={int(time.time())}"
try:
    t = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=90).read().decode("utf-8", "replace")
    og = re.search(r'<meta property="og:image" content="([^"]+)"', t)
    print(f"\nog:image: {og.group(1) if og else '(tidak ada)'}")
except Exception as e:
    print(f"\nhalaman pratinjau: {str(e)[:100]} (normal untuk status future)")

# URL media live?
mm = urllib.request.Request(f"https://{DOM}/wp-json/wp/v2/media/10247?_fields=source_url",
                            headers={"Authorization": f"Basic {AUTH}", **UA})
src = json.loads(urllib.request.urlopen(mm, timeout=90).read())["source_url"]
import subprocess
code = subprocess.run(["curl", "-4", "-s", "-o", "/dev/null", "-w", "%{http_code}", src],
                      capture_output=True, text=True).stdout
print(f"media 10247: HTTP {code} · {src}")
