#!/usr/bin/env python3
"""Explore Toutiao user profile"""
import asyncio, json
from playwright.async_api import async_playwright

URL = "https://web.toutiao.com/c/user/token/MS4wLjABAAAAkyiH4s89PlPc8mLslzJIyp86oKuXs_r3CZ7TGIWVQKg/?source=tuwen_detail&entrance_gid=7595117695461343779&log_from=c7cc34d3c729f_1784096195358"

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        await page.goto(URL, timeout=30000, wait_until="networkidle")
        await page.wait_for_timeout(3000)
        
        title = await page.title()
        print("TITLE:", title)
        
        # Try to find user name
        el = await page.query_selector(".user-name, .nickname, .username, h1")
        if el:
            print("USER:", await el.inner_text())
        
        # Get all article links
        links = await page.eval_on_selector_all(
            "a[href*='article'], a[href*='item'], .article-item a, .feed-card a",
            "els => els.map(e => ({href: e.href, text: e.textContent.trim().slice(0,80)})).filter(x => x.text.length > 5)"
        )
        print(f"LINKS: {len(links)}")
        for link in links[:10]:
            print(f"  {link['text'][:60]} | {link['href'][:80]}")
        
        body = await page.evaluate("document.body.innerText.substring(0, 3000)")
        print("\nBODY:", body[:1500])
        
        await browser.close()

asyncio.run(main())
