#!/usr/bin/env python3
"""Uji akses GSC studiosoft.id (read-only, ringkas)."""
import json, os
from datetime import date, timedelta
from google.oauth2.credentials import Credentials as OAuthCreds
from googleapiclient.discovery import build

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
t = json.load(open(os.path.join(WS, "seo-oauth", "google_token_atun.json")))
c = OAuthCreds(**{k: t[k] for k in ("token", "refresh_token", "token_uri", "client_id", "client_secret")},
               scopes=t.get("scopes"))
gsc = build("webmasters", "v3", credentials=c, cache_discovery=False)
END = date.today() - timedelta(days=2)
START = END - timedelta(days=89)
r = gsc.searchanalytics().query(siteUrl="sc-domain:studiosoft.id", body={
    "startDate": START.isoformat(), "endDate": END.isoformat(),
    "dimensions": ["query"], "rowLimit": 5}).execute()
rows = r.get("rows", [])
print(f"OK GSC: {len(rows)} baris contoh, rentang {START}..{END}")
for x in rows:
    print(f"  {x['keys'][0][:60]} | klik {x['clicks']:.0f} | imp {x['impressions']:.0f} | pos {x['position']:.1f}")
