Files
ai_wht_wechat/backend/test_get_cookies.py
2026-01-23 16:27:47 +08:00

103 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
测试AdsPower查询Cookie功能
"""
import asyncio
import sys
from pathlib import Path
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
from fingerprint_browser import get_fingerprint_manager
from loguru import logger
async def test_get_cookies():
"""测试获取Cookie"""
logger.info("="*50)
logger.info("开始测试AdsPower Cookie查询功能")
logger.info("="*50)
manager = get_fingerprint_manager()
# 检查AdsPower状态
if not await manager.check_adspower_status():
logger.error("❌ AdsPower未运行请先启动AdsPower")
return
logger.success("✅ AdsPower运行正常")
# 获取所有配置
logger.info("\n查询所有浏览器配置...")
profiles = await manager.get_browser_profiles()
if not profiles:
logger.error("❌ 没有找到任何浏览器配置")
return
logger.success(f"✅ 找到 {len(profiles)} 个配置")
# 显示配置列表
print("\n可用的配置列表:")
for i, profile in enumerate(profiles[:5], 1): # 只显示前5个
profile_id = profile.get('user_id') or profile.get('id')
profile_name = profile.get('name', 'unknown')
print(f"{i}. ID: {profile_id}, 名称: {profile_name}")
# 选择看起来像是小红书账号的配置
test_profile = None
for profile in profiles:
name = profile.get('name', '')
if 'XHS' in name or '小红书' in name or '1570' in name:
test_profile = profile
break
if not test_profile:
# 如果没有找到,就用第一个
test_profile = profiles[0]
profile_id = test_profile.get('user_id') or test_profile.get('id')
profile_name = test_profile.get('name', 'unknown')
logger.info("\n" + "="*50)
logger.info(f"测试配置: {profile_name} (ID: {profile_id})")
logger.info("="*50)
# 查询Cookie
logger.info("\n开始查询Cookie...")
cookies = await manager.get_profile_cookies(profile_id)
if cookies:
logger.success(f"\n✅ 成功获取Cookie!")
logger.info(f"Cookie数量: {len(cookies)}")
# 显示前3个Cookie的详细信息
print("\n前3个Cookie详情:")
for i, cookie in enumerate(cookies[:3], 1):
print(f"\n{i}. {cookie.get('name', 'unknown')}")
print(f" Domain: {cookie.get('domain', 'N/A')}")
print(f" Path: {cookie.get('path', 'N/A')}")
print(f" Value: {cookie.get('value', '')[:20]}..." if len(cookie.get('value', '')) > 20 else f" Value: {cookie.get('value', '')}")
print(f" HttpOnly: {cookie.get('httpOnly', False)}")
print(f" Secure: {cookie.get('secure', False)}")
print(f" SameSite: {cookie.get('sameSite', 'N/A')}")
# 统计Cookie域名分布
domains = {}
for cookie in cookies:
domain = cookie.get('domain', 'unknown')
domains[domain] = domains.get(domain, 0) + 1
print("\nCookie域名分布:")
for domain, count in sorted(domains.items(), key=lambda x: x[1], reverse=True)[:5]:
print(f" {domain}: {count}")
else:
logger.error("❌ 获取Cookie失败")
logger.info("\n" + "="*50)
logger.info("测试完成")
logger.info("="*50)
if __name__ == "__main__":
asyncio.run(test_get_cookies())