#!/usr/bin/env python3
"""Cari semua bentuk nomor telepon/Contact Person di konten TSC (untuk diseragamkan ke 085117444417)."""
import base64, importlib.util, json, re, sys, time, urllib.error, urllib.request
from collections import Counter

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"}

POLA = [
    ("nomor 08xx", re.compile(r"\b0\d{2}[\s\-]?\d{3,4}[\s\-]?\d{3,5}\b")),
    ("nomor +62", re.compile(r"\+62[\s\-]?\d{2,3}[\s\-]?\d{3,4}[\s\-]?\d{3,5}")),
    ("placeholder X", re.compile(r"0\d{2}[\s\-]?[Xx]{2,}[\s\-]?[Xx]{2,}")),
    ("Contact Person", re.compile(r"(?:contact|kontak)\s*person[^\n<]{0,40}", re.I)),
    ("CP:", re.compile(r"\bCP\s*[:：][^\n<]{0,40}", re.I)),
]


def rest(path):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    r = urllib.request.Request(f"https://{DOM}{path}", headers=h)
    for i in range(4):
        try:
            with urllib.request.urlopen(r, timeout=90) as x:
                return json.loads(x.read())
        except urllib.error.HTTPError as e:
            if e.code in (429, 404):
                time.sleep(4)
                continue
            return {"err": e.code}
        except Exception:
            time.sleep(4)
    return {}


temuan = {}
for jenis in ("posts", "pages"):
    page = 1
    while True:
        d = rest(f"/wp-json/wp/v2/{jenis}?per_page=100&page={page}&status=publish&context=edit&_fields=id,link,content")
        if not isinstance(d, list) or not d:
            break
        for x in d:
            teks = re.sub(r"<[^>]+>", " ", x["content"]["raw"])
            teks = teks.replace("&nbsp;", " ")
            for nama, pola in POLA:
                for m2 in pola.finditer(teks):
                    potong = re.sub(r"\s+", " ", teks[max(0, m2.start() - 60):m2.end() + 40]).strip()
                    temuan.setdefault(m2.group(0).strip(), {"jenis": nama, "contoh": potong,
                                                            "halaman": []})
                    if x["link"] not in temuan[m2.group(0).strip()]["halaman"]:
                        temuan[m2.group(0).strip()]["halaman"].append(x["link"])
        if len(d) < 100:
            break
        page += 1
        time.sleep(0.5)

print(f"bentuk nomor/kontak yang ditemukan: {len(temuan)}\n")
for nomor, info in sorted(temuan.items(), key=lambda y: -len(y[1]["halaman"])):
    print(f"■ «{nomor}» ({info['jenis']}) · {len(info['halaman'])} halaman")
    print(f"   contoh: …{info['contoh'][:150]}…")
    for u in info["halaman"][:2]:
        print(f"   {u[:88]}")

json.dump(temuan, open(f"{WS}/drafts/tsc_nomor_kontak.json", "w"), ensure_ascii=False, indent=1)
print("\n→ drafts/tsc_nomor_kontak.json")
