#!/usr/bin/env python3
"""Ambil teks resmi UU 22/2009 (jdih.kemenkeu.go.id) dan cetak pasal yang akan dikutip."""
import re, urllib.request

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
URL = "https://jdih.kemenkeu.go.id/api/download/fulltext/2009/22TAHUN2009UU.htm"
with urllib.request.urlopen(urllib.request.Request(URL, headers=UA), timeout=90) as x:
    h = x.read().decode("utf-8", "replace")
t = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", h, flags=re.S | re.I)
t = re.sub(r"<[^>]+>", " ", t)
t = re.sub(r"&nbsp;?", " ", t)
t = re.sub(r"\s+", " ", t)
print(f"panjang teks: {len(t)}")
for p in ["Pasal 229", "Pasal 230", "Pasal 231", "Pasal 232", "Pasal 234", "Pasal 235", "Pasal 236",
          "Pasal 239", "Pasal 240", "Pasal 241", "Pasal 310", "Pasal 311", "Pasal 312"]:
    i = t.find(p + " ")
    if i < 0:
        print(f"\n{p}: TIDAK KETEMU")
        continue
    print(f"\n>>> {p}: {t[i:i+700]}")
