import base64, json, os, re, sys
import urllib.request, urllib.error

sys.path.insert(0, '/home/mantapman/.hermes/profiles/atun/workspace')
from cfg_tslaw import CFG
import html as htmlmod

AUTH = base64.b64encode(f"{CFG['user']}:{CFG['pass']}".encode()).decode()
BASE = f"https://{CFG['domain']}/wp-json/wp/v2"
HDR = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}

def get(path):
    req = urllib.request.Request(BASE + path, headers=HDR)
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.load(r)

OUT = '/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261003'
os.makedirs(OUT, exist_ok=True)

for pid in (3208, 3233):
    p = get(f"/posts/{pid}?context=edit")
    with open(f"{OUT}/post_{pid}_BEFORE.json", 'w') as f:
        json.dump(p, f, ensure_ascii=False, indent=1)
    raw = p['content']['raw']
    with open(f"{OUT}/post_{pid}_BEFORE.html", 'w') as f:
        f.write(raw)
    m = p.get('meta', {})
    txt = re.sub(r'<[^>]+>', ' ', raw)
    txt = htmlmod.unescape(txt)
    wc = len(txt.split())
    print("=" * 70)
    print(f"ID {pid} | status={p['status']} | kata(REST raw)={wc}")
    print("WP title    :", p['title']['raw'])
    print("link        :", p['link'])
    print("focus kw    :", m.get('_yoast_wpseo_focuskw'))
    print("SEO title   :", m.get('_yoast_wpseo_title'))
    print("meta desc   :", m.get('_yoast_wpseo_metadesc'))
    print("meta keys   :", sorted(m.keys()))
    print("wa.me       :", sorted(set(re.findall(r'wa\.me/\d+', raw))))
    print("FAQPage     :", 'FAQPage' in raw)
    print("found FAQ h2:", [h for h in re.findall(r'<h2[^>]*>(.*?)</h2>', raw) if 'faq' in h.lower()])
    print("pillar link :", 'law-firm-surabaya' in raw)
    print("H2 list     :")
    for h in re.findall(r'<h2[^>]*>(.*?)</h2>', raw):
        print("   -", re.sub(r'<[^>]+>', '', h))
    print("H3 list     :")
    for h in re.findall(r'<h3[^>]*>(.*?)</h3>', raw):
        print("   -", re.sub(r'<[^>]+>', '', h)[:90])
