#!/usr/bin/env python3
"""Cari sumber soal Threads: DuckDuckGo (peramban) + sumber industri yang sudah dikenal."""
import asyncio, json, re

TANYA = ["threads algorithm how to grow 2026", "threads engagement strategy brand tips"]
LANGSUNG = [
    ("Hootsuite", "https://blog.hootsuite.com/threads-algorithm/"),
    ("Sprout Social", "https://sproutsocial.com/insights/threads-algorithm/"),
    ("Buffer", "https://buffer.com/resources/threads-growth/"),
    ("Later", "https://later.com/blog/threads-algorithm/"),
]


async def main():
    from playwright.async_api import async_playwright
    hasil = {"pencarian": {}, "halaman": {}}
    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()

        for q in TANYA:
            for mesin, u in (("ddg", "https://html.duckduckgo.com/html/?q=" + q.replace(" ", "+")),
                             ("google", "https://www.google.com/search?hl=en&num=15&q=" + q.replace(" ", "+"))):
                try:
                    await hal.goto(u, wait_until="domcontentloaded", timeout=70000)
                    await hal.wait_for_timeout(4000)
                    tautan = await hal.eval_on_selector_all("a", "els => els.map(e => e.href).filter(h => h && h.startsWith('http'))")
                    bersih = []
                    for t in tautan:
                        if any(x in t for x in ("duckduckgo.com", "google.com", "bing.com", "youtube.com",
                                                "facebook.com", "threads.com", "instagram.com", "play.google")):
                            continue
                        if t not in bersih:
                            bersih.append(t)
                    hasil["pencarian"][f"{mesin}: {q}"] = bersih[:12]
                    print(f"\n=== {mesin}: {q} ({len(bersih)} tautan) ===")
                    for t in bersih[:12]:
                        print("   " + t[:110])
                except Exception as e:
                    print(f"\n=== {mesin}: {q} → gagal {str(e)[:60]}")

        print("\n\n=== sumber industri (langsung) ===")
        for nama, u in LANGSUNG:
            try:
                await hal.goto(u, wait_until="domcontentloaded", timeout=70000)
                await hal.wait_for_timeout(4000)
                judul = await hal.title()
                isi = await hal.evaluate("() => (document.querySelector('article') || document.querySelector('main') || document.body).innerText")
                isi = re.sub(r"\n{3,}", "\n\n", isi)
                baris = [b.strip() for b in isi.split("\n") if len(b.strip()) > 40]
                hasil["halaman"][nama] = {"url": u, "judul": judul[:130], "baris": baris[:80]}
                print(f"\n--- {nama}: {judul[:90]}")
                for b in baris[:14]:
                    print("   " + b[:200])
            except Exception as e:
                print(f"\n--- {nama}: gagal {str(e)[:70]}")
        await br.close()
    json.dump(hasil, open("/home/mantapman/.hermes/profiles/atun/workspace/drafts/riset_threads_sumber.json", "w"), indent=1, ensure_ascii=False)


asyncio.run(main())
