#!/usr/bin/env python3
"""Find the BPK JDIH detail URL for a regulation by scraping their search page."""
import re
import sys
import urllib.parse
import urllib.request

UA = ("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
      "Chrome/124.0.0.0 Safari/537.36")

q = sys.argv[1]
url = "https://peraturan.bpk.go.id/Search?keywords=" + urllib.parse.quote(q)
req = urllib.request.Request(url, headers={"User-Agent": UA})
html = urllib.request.urlopen(req, timeout=60).read().decode("utf-8", "ignore")
hits = re.findall(r"/Details/(\d+)/([a-z0-9\-]+)", html)
seen = []
for rid, slug in hits:
    pair = (rid, slug)
    if pair not in seen:
        seen.append(pair)
title = re.search(r"(?is)<title>(.*?)</title>", html)
print("PAGE TITLE:", title.group(1).strip() if title else "?")
for rid, slug in seen[:40]:
    print(f"https://peraturan.bpk.go.id/Details/{rid}/{slug}")
