from playwright.sync_api import sync_playwright

GROUP_URL = "https://www.facebook.com/groups/1928282657363211/"

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context(storage_state="storage_state.json")
    page = context.new_page()
    page.goto(GROUP_URL, wait_until="networkidle")
    page.wait_for_timeout(3000)

    with open("debug_articles2.txt", "w", encoding="utf-8") as f:
        for round_no in range(7):
            articles = page.locator('div[role="article"]')
            count = articles.count()
            f.write(f"\n########## KÖR {round_no} — talált elemek: {count} ##########\n")
            print(f"KÖR {round_no}: {count} elem")

            for i in range(count):
                try:
                    text = articles.nth(i).inner_text(timeout=2000)
                except Exception as e:
                    text = f"[HIBA: {e}]"
                tail = text[-150:].replace("\n", " | ")
                f.write(f"--- #{i} (tail: ...{tail}) ---\n{text}\n\n")

            if round_no < 6:
                page.mouse.wheel(0, 3000)
                page.wait_for_timeout(1500)

    browser.close()
    print("Kész, lásd debug_articles2.txt")
