import json
from playwright.sync_api import sync_playwright

def login_and_read():
    with sync_playwright() as p:
        # We need a persistent context to save login state, or at least run it headful first to login
        browser = p.chromium.launch_persistent_context(
            user_data_dir="./snapchat_session",
            headless=False, # Must be False for the user to login
            viewport={"width": 1280, "height": 720}
        )
        page = browser.pages[0] if browser.pages else browser.new_page()
        
        print("Navigating to Snapchat Web...")
        page.goto("https://web.snapchat.com/")
        
        print("Please log in. Waiting for the chat interface to load...")
        try:
            # The chat list usually has a specific ARIA label or class. Let's wait for something that indicates login success.
            # We wait for the "search" input or avatar which means we are inside.
            page.wait_for_selector('div[data-testid="conversations-list"]', timeout=300000) # 5 minutes to login
            print("Successfully logged in!")
            
            print("Dumping HTML for analysis...")
            html = page.content()
            with open("snap_web.html", "w") as f:
                f.write(html)
            print("HTML saved to snap_web.html")
            
        except Exception as e:
            print(f"Error or timeout: {e}")
        
        browser.close()

if __name__ == "__main__":
    login_and_read()
