#!/usr/bin/env python3
"""Cari foto Pexels untuk topik monitor / website / wordpress — untuk mengganti thumbnail bertulisan."""
import json, requests, time
from pexels_image import PEXELS_KEYS, PEXELS_API

QUERIES = ["computer monitor", "desktop monitor workspace", "website design laptop",
           "wordpress website", "web developer monitor", "laptop screen website",
           "monitor screen code", "website development desk"]

hasil = {}
for q in QUERIES:
    dapat = False
    for key in PEXELS_KEYS:
        try:
            r = requests.get(PEXELS_API, params={"query": q, "per_page": 6, "orientation": "landscape"},
                             headers={"Authorization": key}, timeout=30)
            if r.status_code == 200:
                photos = r.json().get("photos", [])
                hasil[q] = [{"id": p["id"], "alt": (p.get("alt") or "")[:70],
                             "fotografer": p.get("photographer"),
                             "url": p["src"].get("large2x") or p["src"].get("large"),
                             "w": p.get("width"), "h": p.get("height")} for p in photos]
                print(f"  OK   {q:<32} {len(photos)} foto")
                dapat = True
                break
            print(f"  {r.status_code}  {q:<32} (key ...{key[-6:]})")
            time.sleep(1)
        except Exception as e:
            print(f"  ERR  {q:<32} {str(e)[:50]}")
    if not dapat:
        print(f"  GAGAL {q}")

json.dump(hasil, open("drafts/pexels_monitor_website.json", "w"), indent=1, ensure_ascii=False)
print("\n=== rincian ===")
for q, fotos in hasil.items():
    print(f"\n{q}")
    for f in fotos[:6]:
        print(f"   #{f['id']:<10} {f['w']}x{f['h']}  {f['alt'][:62]}")
