#!/usr/bin/env python3
"""Pilih artikel sasaran FAQ: >=1.000 kata, belum punya FAQPage, diurutkan berdasarkan impresi GSC."""
import base64, html, json, os, re, time, urllib.request, urllib.error
from datetime import date, timedelta
from google.oauth2 import service_account
from googleapiclient.discovery import build

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
creds = service_account.Credentials.from_service_account_file(
    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)
e = date.today() - timedelta(days=2)
a, b = (e - timedelta(days=89)).isoformat(), e.isoformat()


def api(path):
    h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
    r = urllib.request.Request(S + path, headers=h)
    err = None
    for _ in range(4):
        try:
            with urllib.request.urlopen(r, timeout=240) as x:
                return json.loads(x.read())
        except Exception as ex:
            err = ex; time.sleep(5)
    print("  gagal:", str(err)[:80]); return []


imp = {}
for r in gsc.searchanalytics().query(siteUrl=S + "/", body={"startDate": a, "endDate": b, "dimensions": ["page"],
                                                            "rowLimit": 1000}).execute().get("rows", []):
    imp[r["keys"][0].rstrip("/")] = int(r["impressions"])

posts, page = [], 1
while True:
    d = api(f"/wp-json/wp/v2/posts?per_page=100&page={page}&context=edit&_fields=id,link,title,content,date")
    posts += d
    if len(d) < 100:
        break
    page += 1
print(f"artikel: {len(posts)}")

sasaran = []
for p in posts:
    raw = p["content"]["raw"] if isinstance(p["content"], dict) else ""
    kata = len(html.unescape(re.sub(r"<[^>]+>", " ", raw)).split())
    sudah_faq = "FAQPage" in raw or bool(re.search(r"<h[23][^>]*>\s*(FAQ|Pertanyaan yang sering)", raw, re.I))
    if kata >= 1000 and not sudah_faq:
        h2 = [html.unescape(re.sub("<[^>]+>", "", h)).strip() for h in re.findall(r"<h2[^>]*>(.*?)</h2>", raw, re.S)][:8]
        ringkas = re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", raw)))[:300]
        sasaran.append({"id": p["id"], "judul": html.unescape(p["title"]["rendered"]), "link": p["link"],
                        "kata": kata, "imp": imp.get(p["link"].rstrip("/"), 0), "h2": h2, "ringkas": ringkas})

sasaran.sort(key=lambda x: -x["imp"])
json.dump(sasaran, open("ss_sasaran_faq.json", "w"), indent=1, ensure_ascii=False)
print(f"artikel >=1000 kata & belum ber-FAQ: {len(sasaran)}")
print(f"  di antaranya punya impresi >0: {sum(1 for x in sasaran if x['imp']>0)}\n")
for x in sasaran[:12]:
    print(f"  {x['imp']:>4} imp | {x['kata']:>5} kata | #{x['id']} {x['judul'][:56]}")
    print(f"        H2: {' | '.join(h[:32] for h in x['h2'][:4])}")
