#!/usr/bin/env python3
"""Cek jadwal tayang yang sudah ada + contoh struktur artikel terakhir (WA button, FAQ schema, gambar)."""
import base64, html, json, re, time, urllib.request
from datetime import datetime, timezone, timedelta

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
WIB = timezone(timedelta(hours=7))
print("sekarang WIB:", datetime.now(WIB).strftime("%Y-%m-%d %H:%M"))


def wp(path):
    r = urllib.request.Request(S + path, headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=150) as x:
                return json.loads(x.read()), dict(x.headers)
        except Exception:
            time.sleep(4)
    return [], {}


print("\n=== jadwal tayang (future) yang sudah ada ===")
d, h = wp("/wp-json/wp/v2/posts?per_page=30&status=future&orderby=date&order=asc&_fields=id,date,title,slug")
for p in d if isinstance(d, list) else []:
    tgl = datetime.fromisoformat(p["date"]).replace(tzinfo=timezone.utc).astimezone(WIB)
    print(f"  #{p['id']:>5} {tgl.strftime('%d %b %H:%M')} WIB  {html.unescape(p['title']['rendered'])[:60]}")
print(f"  total: {h.get('X-WP-Total', '?')}")

print("\n=== 3 artikel terakhir terbit (contoh struktur) ===")
d, _ = wp("/wp-json/wp/v2/posts?per_page=3&status=publish&_fields=id,date,title,link,categories,tags,featured_media")
for p in d if isinstance(d, list) else []:
    tgl = datetime.fromisoformat(p["date"]).replace(tzinfo=timezone.utc).astimezone(WIB)
    print(f"  #{p['id']} {tgl.strftime('%d %b %H:%M')} | media {p['featured_media']} | {p['link'].replace(S,'')[:60]}")
    print(f"     {html.unescape(p['title']['rendered'])[:80]}")

id_contoh = d[0]["id"] if isinstance(d, list) and d else None
if id_contoh:
    p, _ = wp(f"/wp-json/wp/v2/posts/{id_contoh}?context=edit&_fields=content,title")
    raw = p["content"]["raw"]
    print(f"\n=== isi #{id_contoh}: {len(raw)} karakter ===")
    print("  --- blok WA (cari tombol) ---")
    m = re.search(r".{0,120}wa\.me.{0,320}", raw, re.S)
    print("  ", (m.group(0)[:420] if m else "tidak ditemukan"))
    print("  --- blok FAQ / schema ---")
    for m in re.finditer(r"(FAQPage|application/ld\+json|<h2[^>]*>.*?[Ff][Aa][Qq].*?</h2>)", raw):
        print("   pola:", m.group(0)[:80])
    print("  --- struktur heading ---")
    for hd in re.findall(r"<h[23][^>]*>(.*?)</h[23]>", raw)[:14]:
        print("   ", html.unescape(re.sub(r"<[^>]+>", "", hd))[:70])
