#!/usr/bin/env python3
"""Cari celah: kueri TSC yang punya tayangan tapi belum ada halaman yang benar-benar menyasar."""
import base64, importlib.util, json, re, time, urllib.error, urllib.request
from datetime import date, timedelta

from google.oauth2 import service_account
from googleapiclient.discovery import build

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
AUTH = base64.b64encode(f"{CFG['user']}:{CFG['pass']}".encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}
BASIS = ("dokter", "kulit", "klinik", "kecantikan", "jerawat", "kelamin", "perawatan", "facial",
         "estetika", "sidoarjo", "krian", "terdekat", "biaya", "harga", "konsultasi", "spesialis")
STOP = set("dan atau yang untuk di ke dari pada dengan adalah ini itu the skin code clinic".split())


def rest(path):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    r = urllib.request.Request(f"https://{CFG['domain']}{path}", headers=h)
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=120) as x:
                return json.loads(x.read())
        except Exception:
            time.sleep(4)
    return {}


# semua judul + slug halaman
pages = []
for jenis in ("posts", "pages"):
    page = 1
    while True:
        d = rest(f"/wp-json/wp/v2/{jenis}?per_page=100&page={page}&status=publish&_fields=id,slug,title,link")
        if not isinstance(d, list) or not d:
            break
        for x in d:
            judul = re.sub(r"<[^>]+>", "", str(x["title"]["rendered"]))
            pages.append((x["slug"] + " " + judul).lower())
        if len(d) < 100:
            break
        page += 1
print(f"halaman diperiksa: {len(pages)}")

creds = service_account.Credentials.from_service_account_file(
    __import__("os").path.expanduser("~/.hermes/gsc_service_account.json"),
    scopes=["https://www.googleapis.com/auth/webmasters.readonly"])
gsc = build("webmasters", "v3", credentials=creds, cache_discovery=False)
AKHIR = date.today() - timedelta(days=2)
q = gsc.searchanalytics().query(siteUrl="https://www.theskincodeclinic.com/", body={
    "startDate": (AKHIR - timedelta(days=89)).isoformat(), "endDate": AKHIR.isoformat(),
    "dimensions": ["query"], "rowLimit": 2000}).execute().get("rows", [])

celah = []
for r in q:
    k = r["keys"][0].lower().strip()
    if r["impressions"] < 10:
        continue
    kata = [w for w in re.findall(r"[a-z]+", k) if w not in STOP and len(w) > 3]
    if not kata:
        continue
    # halaman dianggap menyasar bila memuat minimal 60% kata kunci
    cocok = max((sum(1 for w in kata if w in p) / len(kata) for p in pages), default=0)
    if cocok < 0.6:
        celah.append({"kueri": r["keys"][0], "tayang": int(r["impressions"]), "klik": int(r["clicks"]),
                      "posisi": round(r["position"], 1), "kecocokan": round(cocok, 2)})

celah.sort(key=lambda x: -x["tayang"])
print(f"\nkueri tanpa halaman khusus: {len(celah)}\n")
for c in celah[:25]:
    print(f"  {c['tayang']:>5} tayang | {c['klik']:>2} klik | pos {c['posisi']:>5} | kecocokan {c['kecocokan']} | «{c['kueri']}»")
json.dump(celah, open(f"{WS}/drafts/tsc_celah_keyword.json", "w"), ensure_ascii=False, indent=1)
