import sys, time, json
from pathlib import Path
sys.path.insert(0, '/app')
from pinterest.client import PinterestClient

data_dir = Path('/app/data/pinterest')
with PinterestClient(data_dir) as client:
    client.is_authenticated()
    client.page.goto('https://www.pinterest.com/rmacsparran/_created/', timeout=30000)
    time.sleep(6)
    client.page.screenshot(path=str(data_dir / 'debug_created_boards.png'))
    elems = client.page.query_selector_all('[data-test-id], button, [role="button"]')
    seen = set()
    results = []
    for e in elems:
        try:
            txt = (e.inner_text() or '').strip()[:60]
            aria = e.get_attribute('aria-label') or ''
            testid = e.get_attribute('data-test-id') or ''
            href = e.get_attribute('href') or ''
            key = txt + '|' + aria + '|' + testid
            if key not in seen and (txt or aria or testid):
                seen.add(key)
                results.append({'txt': txt, 'aria': aria, 'testid': testid, 'href': href[:80]})
        except:
            pass
    print(json.dumps(results))
