#!/usr/bin/env python3
"""Verifikasi HTTP 200 + ambil isi 4 artikel pilihan."""
import re, sys, json
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from openssl_fetch import fetch


def dechunk(body: bytes) -> bytes:
    out = b""; i = 0
    while True:
        j = body.find(b"\r\n", i)
        if j < 0:
            break
        try:
            n = int(body[i:j].split(b";")[0].strip(), 16)
        except Exception:
            break
        if n == 0:
            break
        out += body[j + 2:j + 2 + n]
        i = j + 2 + n + 2
    return out


URLS = {
 "uky": "https://studiosoft.id/6-skill-wajib-untuk-belajar-digital-marketing-bagi-pemula-di-tahun-2026/",
 "tslaw": "https://www.tslaw.id/cara-daftar-hak-cipta/",
 "tsc": "https://www.theskincodeclinic.com/keluhan-kulit-kelamin-yang-ditangani-dokter-spesialis/",
 "das": "https://dasindonesia.com/6-tips-cara-mudah-merawat-mobil-kesayangan-anda/",
}
for k, u in URLS.items():
    try:
        st, head, body = fetch(u)
    except Exception as e:
        print(k, "FETCH ERR", e); continue
    b = dechunk(body)
    if "chunked" not in head.lower() and b == b"":
        b = body
    html = b.decode("utf-8", "replace")
    m = re.search(r"<title>(.*?)</title>", html, re.S | re.I)
    title = re.sub(r"\s+", " ", m.group(1)).strip() if m else "?"
    # ambil paragraf
    paras = re.findall(r"<p[^>]*>(.*?)</p>", html, re.S | re.I)
    txt = []
    for p in paras:
        t = re.sub(r"<[^>]+>", "", p)
        t = re.sub(r"&#\d+;|&\w+;", " ", t)
        t = re.sub(r"\s+", " ", t).strip()
        if len(t) > 60:
            txt.append(t)
    print(f"\n########## {k}  status={st} bytes={len(b)} ##########")
    print("TITLE:", title[:130])
    for t in txt[:12]:
        print("  -", t[:300])
