#!/usr/bin/env python3
"""DAS: diagnosa halaman yang kehilangan impresi — status, robots meta, canonical, dan status indeks GSC."""
import os, re, time, urllib.request, urllib.error
from google.oauth2 import service_account
from googleapiclient.discovery import build

KEY = os.path.expanduser("~/.hermes/gsc_service_account.json")
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
gsc = build("searchconsole", "v1", credentials=service_account.Credentials.from_service_account_file(
    KEY, scopes=["https://www.googleapis.com/auth/webmasters.readonly"]), cache_discovery=False)

HAL = ["/keluhan-yang-sering-terjadi-pada-mobil-mitsubishi-expander/",
       "/10-bengkel-mobil-resmi-terbaik-di-surabaya-rekomendasi/",
       "/bengkel-spesialis-nissan-surabaya/",
       "/7-tips-memilih-oli-mobil-terbaik/",
       "/hari-minggu-tetap-buka/",
       "/check-engine-menyala-tiba-tiba-7-langkah-wajib-dilakukan/",
       "/bengkel-spesialis-matic-surabaya/",
       "/bengkel-mobil-bmw-mercy-surabaya/"]

print("=== A. status teknis halaman (live) ===")
for u in HAL:
    url = "https://dasindonesia.com" + u
    try:
        r = urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=60)
        h = r.read().decode("utf-8", "replace")
        robots = re.search(r'<meta name="robots" content="([^"]*)"', h)
        canon = re.search(r'<link rel="canonical" href="([^"]*)"', h)
        t = re.search(r"<title>(.*?)</title>", h, re.S)
        print(f"   {r.status} | {u[:46]:<48} robots={robots.group(1) if robots else '-'} | "
              f"canonical={'beda!' if canon and canon.group(1).rstrip('/') != url.rstrip('/') else 'ok'}")
        print(f"        judul: {t.group(1).strip()[:70] if t else '-'}")
    except urllib.error.HTTPError as e:
        print(f"   {e.code} | {u[:46]:<48} ← masalah akses")
    except Exception as e:
        print(f"   ERR | {u[:46]} ({str(e)[:40]})")

print("\n=== B. status indeks GSC (URL Inspection) ===")
for u in HAL[:6]:
    try:
        res = gsc.urlInspection().index().inspect(body={
            "inspectionUrl": "https://dasindonesia.com" + u, "siteUrl": "https://dasindonesia.com/"}).execute()
        st = res.get("inspectionResult", {}).get("indexStatusResult", {})
        print(f"   {u[:44]:<46} verdict={st.get('verdict')} | coverage={st.get('coverageState')[:44]} | "
              f"robots={st.get('robotsTxtState')} | terakhir dirayapi: {str(st.get('lastCrawlTime'))[:10]}")
    except Exception as e:
        print(f"   {u[:44]}: gagal {str(e)[:70]}")
    time.sleep(0.5)

print("\n=== C. robots.txt & sitemap. DAS ===")
for u in ("/robots.txt", "/sitemap_index.xml"):
    try:
        r = urllib.request.urlopen(urllib.request.Request("https://dasindonesia.com" + u, headers=UA), timeout=45)
        isi = r.read().decode("utf-8", "replace")
        print(f"   {r.status} | {u} | {len(isi)} char")
        if u.endswith("robots.txt"):
            for b in isi.splitlines()[:14]:
                print("        ", b)
        else:
            for m in re.findall(r"<loc>([^<]+)</loc>", isi)[:8]:
                print("        ", m)
    except Exception as e:
        print(f"   {u}: gagal {str(e)[:50]}")
