#!/usr/bin/env python3
"""Cari artikel LIVE di studiosoft.id yang berkaitan dengan properti/manufaktur/retail/keuangan."""
import base64, json, urllib.request, urllib.error

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()

def api(path):
    h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
    r = urllib.request.Request(S + path, headers=h, method="GET")
    try:
        with urllib.request.urlopen(r, timeout=120) as x:
            return x.status, json.loads(x.read())
    except urllib.error.HTTPError as e:
        return e.code, e.read().decode()[:200]
    except Exception as e:
        return 0, str(e)

KEYS = ("properti", "manufaktur", "retail", "keuangan", "pabrik", "industri", "apartemen", "kavling", "perumahan")
found = 0
for p in range(1, 6):
    st, d = api(f"/wp-json/wp/v2/posts?per_page=100&page={p}&_fields=id,slug,link,status,date,title")
    if not isinstance(d, list):
        print("stop halaman", p, st, str(d)[:120]); break
    for post in d:
        blob = (post["slug"] + " " + (post.get("title") or {}).get("rendered", "")).lower()
        if any(k in blob for k in KEYS):
            print(f"#{post['id']} {post['date'][:10]} {post['status']} {post['link']}")
            found += 1
    if len(d) < 100:
        print("(habis di halaman", p, ")", len(d), "post)")
        break
print("total kandidat:", found)
