71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Test cookie setting in browser"""
|
|
|
|
import sys
|
|
sys.path.insert(0, '/home/starmen1265/work/kb-skills')
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
from kb_workflow.pipeline.config import get_settings
|
|
|
|
# Get cookie
|
|
settings = get_settings()
|
|
cookie_str = settings.zhihu_cookie.get_secret_value()
|
|
|
|
print(f"Cookie string length: {len(cookie_str)}")
|
|
print(f"First 100 chars: {cookie_str[:100]}")
|
|
print()
|
|
|
|
# Parse cookies
|
|
cookies = []
|
|
for item in cookie_str.split(';'):
|
|
item = item.strip()
|
|
if '=' in item:
|
|
name, value = item.split('=', 1)
|
|
cookies.append({
|
|
'name': name.strip(),
|
|
'value': value.strip(),
|
|
'domain': '.zhihu.com',
|
|
'path': '/',
|
|
'secure': True,
|
|
'httpOnly': False,
|
|
'sameSite': 'Lax'
|
|
})
|
|
|
|
print(f"Parsed {len(cookies)} cookies")
|
|
for i, cookie in enumerate(cookies[:5]):
|
|
print(f" {i+1}. {cookie['name']}: {cookie['value'][:30]}...")
|
|
print()
|
|
|
|
# Test with browser
|
|
with sync_playwright() as p:
|
|
browser = p.chromium.launch(headless=True)
|
|
context = browser.new_context(
|
|
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
)
|
|
|
|
# Add cookies
|
|
context.add_cookies(cookies)
|
|
print("✅ Cookies added to context")
|
|
|
|
# Create page and test
|
|
page = context.new_page()
|
|
|
|
print("Navigating to zhihu.com...")
|
|
page.goto("https://www.zhihu.com", timeout=30000)
|
|
print(f"Title: {page.title()}")
|
|
print(f"URL: {page.url}")
|
|
|
|
# Check if we're logged in by visiting the API
|
|
print("\nTesting API access...")
|
|
response = page.request.get("https://www.zhihu.com/api/v4/me")
|
|
print(f"API status: {response.status}")
|
|
if response.status == 200:
|
|
data = response.json()
|
|
print(f"Username: {data.get('name', 'N/A')}")
|
|
print("✅ Cookie is valid!")
|
|
else:
|
|
print(f"❌ API returned status {response.status}")
|
|
print(f"Response: {response.text()[:200]}")
|
|
|
|
browser.close()
|