import sys
from playwright.sync_api import sync_playwright

if hasattr(sys.stdout, 'reconfigure'):
    sys.stdout.reconfigure(encoding='utf-8')

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

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp("http://127.0.0.1:9224")
    context = browser.contexts[0]
    page = context.pages[0] if context.pages else context.new_page()

    page.goto(GROUP_URL, wait_until="domcontentloaded")
    page.wait_for_timeout(3000)

    # Lấy các khối text trong feed
    texts = page.locator("div[role='feed'] div[dir='auto']").all_inner_texts()
    print(f"Total dir=auto texts: {len(texts)}")
    
    unique_texts = []
    for t in texts:
        t_clean = t.strip()
        if len(t_clean) > 25 and t_clean not in unique_texts:
            unique_texts.append(t_clean)

    for idx, ut in enumerate(unique_texts[:10]):
        print(f"\n[{idx+1}] {ut[:300]}")
