from playwright.sync_api import sync_playwright
import sys
pid=sys.argv[1]
with sync_playwright() as p:
    b=p.chromium.launch(headless=True,args=["--no-sandbox","--disable-dev-shm-usage"])
    ctx=b.new_context(storage_state="/tmp/das_state.json",user_agent="Mozilla/5.0")
    pg=ctx.new_page()
    pg.goto(f"https://dasindonesia.com/wp-admin/post.php?post={pid}&action=edit",timeout=60000,wait_until="domcontentloaded")
    pg.wait_for_timeout(9000)
    b2=pg.locator("button[aria-label*='Yoast']")
    if b2.count()>0: b2.first.click(); pg.wait_for_timeout(2500)
    try: pg.locator("text=SEO analysis").last.click(); pg.wait_for_timeout(2500)
    except: pass
    # cari elemen yang berisi "Problems" / "Analysis results"
    for anchor in ["Analysis results","Problems","Good results"]:
        try:
            el=pg.locator(f"text={anchor}").last
            if el.count()>0:
                # naik ke container terdekat
                container=el.locator("xpath=ancestor::div[contains(@class,'yoast')][1]")
                if container.count()==0:
                    container=el.locator("xpath=ancestor::*[4]")
                t=container.inner_text(timeout=4000)
                print(f"===== {anchor} =====")
                print(t[:1200])
                break
        except Exception as e:
            print("anchor skip",anchor,str(e)[:60])
    b.close()
