#!/usr/bin/env python3
"""Diagnosa 13 URL LEIT yang 'unknown to Google': sitemap submission + akses Googlebot."""
import json, os, urllib.request
from google.oauth2 import service_account
from googleapiclient.discovery import build

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)
SITE = "https://www.leitsurabaya.com/"

print("=== sitemap yang disubmit di GSC ===")
try:
    for s in gsc.sitemaps().list(siteUrl=SITE).execute().get("sitemap", []):
        print(" ", s.get("path"), "| lastSubmitted:", str(s.get("lastSubmitted"))[:19],
              "| lastDownloaded:", str(s.get("lastDownloaded"))[:19], "| errors:", s.get("errors"), "| warnings:", s.get("warnings"))
except Exception as e:
    print("  ERROR:", str(e)[:200])

print("\n=== tes akses sebagai Googlebot ===")
UA_G = {"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"}
UA_N = {"User-Agent": "Mozilla/5.0"}
for u in ["/kursus-bahasa-inggris-surabaya-barat/", "/les-bahasa-inggris-terdekat-surabaya/",
          "/bahasa-inggris-untuk-anak-metode-seru/", "/kursus-bahasa-inggris-paling-terjangkau-surabaya/"]:
    row = [u]
    for label, hd in (("googlebot", UA_G), ("browser", UA_N)):
        try:
            r = urllib.request.Request("https://www.leitsurabaya.com" + u, headers=hd)
            resp = urllib.request.urlopen(r, timeout=60)
            body = resp.read().decode("utf-8", "replace")
            noindex = "noindex" in body.lower()[:4000]
            row.append(f"{label}: {resp.status} {len(body)}b noindex={noindex}")
        except Exception as e:
            row.append(f"{label}: ERR {str(e)[:60]}")
    print("  " + " | ".join(row))
