#!/usr/bin/env python3
"""Ambil bagian akhir artikel Medium + simpan utuh ke berkas."""
import asyncio, re

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")
KELUAR = "/home/mantapman/.hermes/profiles/atun/workspace/drafts/artikel_threads_medium.txt"


async def main():
    from playwright.async_api import async_playwright
    async with async_playwright() as pw:
        br = await pw.chromium.launch()
        ctx = await br.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
                                   locale="en-US")
        hal = await ctx.new_page()
        await hal.goto(URL, wait_until="domcontentloaded", timeout=90000)
        await hal.wait_for_timeout(6000)
        judul = await hal.title()
        isi = await hal.evaluate("() => (document.querySelector('article') || document.body).innerText")
        await br.close()

    isi = re.sub(r"\n{3,}", "\n\n", isi)
    baris = [b.strip() for b in isi.split("\n") if b.strip()]
    with open(KELUAR, "w") as f:
        f.write(judul + "\n\n" + "\n".join(baris) + "\n")
    print(f"total baris tersimpan: {len(baris)} → {KELUAR}\n")
    print("=== bagian akhir artikel ===")
    for b in baris[95:]:
        print("   " + b[:220])


asyncio.run(main())
