#!/usr/bin/env python3
"""Lanjutan riset: niche web/SEO/AI + intip postingan terakhir tiap akun."""
import subprocess, re, xml.etree.ElementTree as ET, glob

UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124 Safari/537.36"


def curl(url, timeout=40):
    p = subprocess.run(["curl", "-4", "-sL", "--compressed", "--max-time", str(timeout), "-A", UA, url],
                       capture_output=True, text=True, errors="replace")
    return p.stdout or ""


def rss_items(xml, maks=5):
    out = []
    try:
        root = ET.fromstring(xml)
    except Exception:
        return out
    for it in root.iter("item"):
        t = re.sub(r"\s+", " ", (it.findtext("title") or "")).strip()
        s = it.findtext("source") or ""
        out.append((t, s))
        if len(out) >= maks:
            break
    return out


print("=" * 70)
print("C. WEB/SEO/AI/BISNIS (StudioSoft + uky)")
print("=" * 70)
for q in ["SEO website", "UMKM digital", "AI untuk bisnis", "website UMKM", "digital marketing Indonesia"]:
    url = f"https://news.google.com/rss/search?q={q.replace(' ', '+')}+when:3d&hl=id&gl=ID&ceid=ID:id"
    print(f"\n  [{q}]")
    for t, s in rss_items(curl(url)):
        print(f"     - {t[:115]}  ({s})")

print()
print("=" * 70)
print("D. POSTINGAN TERAKHIR TERLIHAT DI PROFIL (window publik)")
print("=" * 70)
for f in sorted(glob.glob("/tmp/th_*.md")) + sorted(glob.glob("/tmp/r_*.md")):
    nama = f.split("th_")[-1].split("r_")[-1].replace(".md", "")
    txt = open(f, errors="replace").read()
    posts = re.findall(r"\[(\d+[smhd])\]\(https://www\.threads\.com/@[^/]+/post/([A-Za-z0-9_-]+)\)\n\n(.+?)\n", txt)
    print(f"\n  --- @{nama}: {len(posts)} post terlihat")
    for rel, pid, isi in posts[:6]:
        isi = re.sub(r"\s+", " ", isi)[:95]
        print(f"      {rel:>4s} | {pid:14s} | {isi}")
