#!/usr/bin/env python3
"""Cek situs TSLaw + cari artikel publish bertema pajak/usaha untuk tautan internal."""
import base64, json, re, time, urllib.request, urllib.error

DOM = "tslaw.id"
AUTH = base64.b64encode(b"bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y").decode()
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}


def rest(path, method="GET", data=None):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    body = None
    if data is not None:
        body = json.dumps(data).encode()
        h["Content-Type"] = "application/json"
    r = urllib.request.Request(f"https://{DOM}{path}", data=body, headers=h, method=method)
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=120) as x:
                return x.status, json.loads(x.read())
        except urllib.error.HTTPError as e:
            return e.code, e.read().decode()[:200]
        except Exception:
            time.sleep(3)
    return 0, "gagal"


for u in ("https://www.tslaw.id/", "https://www.tslaw.id/layanan/", "https://www.tslaw.id/hubungi/",
          "https://tslaw.id/wp-json/"):
    try:
        t0 = time.time()
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60) as x:
            print(f"{x.status} {u}  ({time.time()-t0:.2f}s)")
    except urllib.error.HTTPError as e:
        print(f"{e.code} {u}")
    except Exception as e:
        print(f"ERR {u} {str(e)[:60]}")

print("\n=== post publish bertema pajak / usaha / sengketa")
semua, off = [], 0
while off < 400:
    st, p = rest(f"/wp-json/wp/v2/posts?status=publish&per_page=100&offset={off}&_fields=id,slug,title,link")
    if not isinstance(p, list) or not p:
        break
    semua += p
    off += 100
print(f"total publish: {len(semua)}")
kunci = re.compile(r"pajak|usaha|pt |cv|perusahaan|kontrak|sengketa|mediasi|arbitrase|keuangan|akuntan|legalitas|nib", re.I)
for p in semua:
    t = p["title"]["rendered"]
    if kunci.search(t):
        print(f"  #{p['id']} {p['slug']}")
        print(f"       {t[:90]}")
json.dump(semua, open("/home/mantapman/.hermes/profiles/atun/workspace/tslaw_listikel/_publish18.json", "w"),
          ensure_ascii=False)
