#!/usr/bin/env python3
"""Tunggu REST StudioSoft hidup lagi, maksimal 45 menit. Berhenti begitu 200."""
import base64, json, time, urllib.request, urllib.error, sys

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
H = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0", "Accept": "application/json"}
URL = f"{S}/wp-json/wp/v2/posts?status=future&per_page=1&_fields=id,date,slug"
BATAS = time.time() + 45 * 60
n = 0
while time.time() < BATAS:
    n += 1
    t0 = time.time()
    try:
        r = urllib.request.Request(URL, headers=H)
        d = json.loads(urllib.request.urlopen(r, timeout=30).read())
        print(f"OK percobaan {n} setelah {time.time()-t0:.0f}s: {d}", flush=True)
        sys.exit(0)
    except Exception as e:
        print(f"gagal {n}: {type(e).__name__} {str(e)[:60]} ({time.time()-t0:.0f}s)", flush=True)
    time.sleep(60)
print("HABIS: REST tetap tidak merespons dalam 45 menit", flush=True)
sys.exit(2)
