#!/usr/bin/env python3
"""Ambil isi artikel Medium yang dikirim Bos (dan salinan cadangan lewat r.jina.ai)."""
import html, re, urllib.request, urllib.error

URL = "https://dkspeaks.medium.com/how-to-grow-on-threads-the-real-strategy-that-took-me-from-0-to-2-000-followers-in-45-days-8b150f4c9eee"
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
      "Accept-Language": "en-US,en;q=0.9"}


def ambil(u, timeout=90):
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=timeout)
        return r.status, r.read().decode("utf-8", "replace")
    except urllib.error.HTTPError as e:
        return e.code, ""
    except Exception as e:
        return 0, str(e)[:100]


st, h = ambil(URL)
print(f"langsung: status {st} | {len(h):,} byte")
if st != 200 or len(h) < 5000:
    st2, h = ambil("https://r.jina.ai/" + URL)
    print(f"cadangan (r.jina.ai): status {st2} | {len(h):,} byte")

if h:
    bersih = re.sub(r"<script.*?</script>|<style.*?</style>", " ", h, flags=re.S | re.I)
    teks = html.unescape(re.sub(r"<[^>]+>", "\n", bersih))
    baris = [b.strip() for b in teks.split("\n") if b.strip()]
    # buang baris navigasi Medium
    inti = [b for b in baris if len(b) > 25 and not re.search(r"(Sign in|Follow|Written by|Medium|Open in app|Recommended from)", b, re.I)]
    print(f"\n=== {len(inti)} baris inti ===\n")
    for b in inti[:120]:
        print("   " + b[:200])
    open("/home/mantapman/.hermes/profiles/atun/workspace/drafts/artikel_threads_medium.txt", "w").write("\n".join(inti))
    print("\n→ drafts/artikel_threads_medium.txt")
