#!/usr/bin/env python3
"""DAS: apakah halaman lain memunculkan canonical/robots dari Yoast? (cek 4 halaman + status cache)"""
import re, time, urllib.request

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
UJI = ["/das-bengkel-spesialis-innova/", "/bengkel-spesialis-lexus-di-surabaya/",
       "/8-layanan-lengkap-das-indonesia-untuk-innova-fortuner-di-surabaya/", "/"]

for u in UJI:
    for nocache in ("", "?nocache=1"):
        try:
            r = urllib.request.urlopen(urllib.request.Request(f"https://dasindonesia.com{u}{nocache}", headers=UA), timeout=90)
            h = r.read().decode("utf-8", "replace")
            can = re.findall(r'<link rel="canonical" href="([^"]+)"', h)
            rob = re.findall(r'<meta name="robots" content="([^"]+)"', h)
            jml = len(re.findall(r"yoast", h, re.I))
            print(f"{u[:50]:<52} {nocache:<10} status {r.status} | canonical {can} | robots {rob} | sebutan yoast {jml}x")
            print(f"      cf-cache: {r.headers.get('cf-cache-status')} | cache-control: {str(r.headers.get('cache-control'))[:40]}")
        except Exception as e:
            print(f"{u[:50]:<52} {nocache:<10} GAGAL {str(e)[:50]}")

print("\n=== HTML mentah: bagian <head> halaman uji Innova ===")
h = urllib.request.urlopen(urllib.request.Request("https://dasindonesia.com/8-layanan-lengkap-das-indonesia-untuk-innova-fortuner-di-surabaya/?v=9", headers=UA), timeout=90).read().decode("utf-8", "replace")
head = h[:h.lower().find("</head>")]
for baris in re.findall(r"<(?:meta|link|title)[^>]*>", head):
    if re.search(r"robots|canonical|og:url|twitter:url", baris, re.I):
        print("   " + baris[:150])
print(f"   (panjang head: {len(head)} karakter)")
