#!/usr/bin/env python3
"""Uji API Google yang relevan untuk SEO: mana yang bisa dipakai sekarang tanpa permohonan."""
import json, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0"}
SITUS = "https://studiosoft.id/"


def coba(nama, url):
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=60) as x:
            body = x.read().decode()[:400]
            print(f"  ✅ {nama:<34} HTTP {x.status}")
            print(f"       {body[:220]}")
    except urllib.error.HTTPError as e:
        print(f"  ❌ {nama:<34} HTTP {e.code} | {e.read().decode()[:160]}")
    except Exception as e:
        print(f"  ⚠️  {nama:<34} {type(e).__name__}: {str(e)[:110]}")


print("=== 1. PageSpeed Insights API (Core Web Vitals lab) ===")
coba("PSI tanpa API key",
     f"https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={SITUS}&strategy=mobile")

print("\n=== 2. Chrome UX Report / CrUX API (data lapangan) ===")
coba("CrUX tanpa key (origin)",
     f"https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=")

print("\n=== 3. Google Business Profile (perlu approval) ===")
coba("Business Profile Performance API",
     "https://businessprofileperformance.googleapis.com/v1/locations/0")

print("\n=== 4. YouTube Data API (video SEO) ===")
coba("YouTube Data API tanpa key",
     "https://www.googleapis.com/youtube/v3/search?part=snippet&q=seo&maxResults=1")
