Files
ai_wht_wechat/backend/test_cookie_formats.py
2025-12-19 22:36:48 +08:00

144 lines
4.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.

"""
测试两种 Cookie 格式支持
"""
import asyncio
import json
from xhs_publish import XHSPublishService
# 格式1: Playwright 完整格式(从文件读取)
playwright_cookies = [
{
"name": "a1",
"value": "19b11d16e24t3h3xmlvojbrw1cr55xwamiacluw3c50000231766",
"domain": ".xiaohongshu.com",
"path": "/",
"expires": 1797066496,
"httpOnly": False,
"secure": False,
"sameSite": "Lax"
},
{
"name": "web_session",
"value": "030037ae088f0acf2c81329d432e4a12fcb0ca",
"domain": ".xiaohongshu.com",
"path": "/",
"expires": 1797066497.112584,
"httpOnly": True,
"secure": True,
"sameSite": "Lax"
}
]
# 格式2: 键值对格式(从数据库读取)
keyvalue_cookies = {
"a1": "19b11d16e24t3h3xmlvojbrw1cr55xwamiacluw3c50000231766",
"abRequestId": "b273b4d0-3ef7-5b8f-bba4-2d19e63ad883",
"acw_tc": "0a4ae09717655304937202738e4b75c08d6eb78f2c8d30d7dc5a465429e1e6",
"gid": "yjDyyfyKiD6DyjDyyfyKd37EJ49qxqC61hlV0qSDFEySFS2822CE01888JqyWKK8Djdi8d2j",
"loadts": "1765530496548",
"sec_poison_id": "a589e333-c364-477c-9d14-53af8a1e7f1c",
"unread": "{%22ub%22:%22648455690000000014025d90%22%2C%22ue%22:%2264b34737000000002f0262f9%22%2C%22uc%22:22}",
"webBuild": "5.0.6",
"webId": "fdf2dccee4bec7534aff5581310c0e26",
"web_session": "030037ae088f0acf2c81329d432e4a12fcb0ca",
"websectiga": "984412fef754c018e472127b8effd174be8a5d51061c991aadd200c69a2801d6",
"xsecappid": "xhs-pc-web"
}
async def test_playwright_format():
"""测试 Playwright 格式"""
print("="*60)
print("测试 1: Playwright 格式(完整格式)")
print("="*60)
try:
publisher = XHSPublishService(playwright_cookies)
print("✅ 初始化成功")
print(f" 转换后的 Cookie 数量: {len(publisher.cookies)}")
return True
except Exception as e:
print(f"❌ 初始化失败: {e}")
return False
async def test_keyvalue_format():
"""测试键值对格式"""
print("\n" + "="*60)
print("测试 2: 键值对格式(数据库格式)")
print("="*60)
try:
publisher = XHSPublishService(keyvalue_cookies)
print("✅ 初始化成功")
print(f" 转换后的 Cookie 数量: {len(publisher.cookies)}")
# 显示转换后的一个示例
print("\n转换示例(第一个 Cookie:")
print(json.dumps(publisher.cookies[0], ensure_ascii=False, indent=2))
return True
except Exception as e:
print(f"❌ 初始化失败: {e}")
return False
async def test_from_file():
"""从文件读取测试"""
print("\n" + "="*60)
print("测试 3: 从 cookies.json 文件读取")
print("="*60)
try:
with open('cookies.json', 'r', encoding='utf-8') as f:
cookies = json.load(f)
publisher = XHSPublishService(cookies)
print("✅ 初始化成功")
print(f" Cookie 数量: {len(publisher.cookies)}")
return True
except FileNotFoundError:
print("⚠️ cookies.json 文件不存在,跳过此测试")
return None
except Exception as e:
print(f"❌ 初始化失败: {e}")
return False
async def main():
print("\n🧪 Cookie 格式兼容性测试\n")
# 测试1: Playwright格式
result1 = await test_playwright_format()
# 测试2: 键值对格式
result2 = await test_keyvalue_format()
# 测试3: 从文件读取
result3 = await test_from_file()
# 总结
print("\n" + "="*60)
print("测试总结")
print("="*60)
print(f"Playwright 格式: {'✅ 通过' if result1 else '❌ 失败'}")
print(f"键值对格式: {'✅ 通过' if result2 else '❌ 失败'}")
if result3 is not None:
print(f"文件读取: {'✅ 通过' if result3 else '❌ 失败'}")
else:
print(f"文件读取: ⚠️ 跳过")
if result1 and result2:
print("\n🎉 所有格式测试通过!")
print("\n💡 使用说明:")
print(" - 从 Python 脚本保存的 cookies.json → Playwright 格式")
print(" - 从数据库读取的 Cookie → 键值对格式")
print(" - 两种格式都可以正常使用!")
else:
print("\n⚠️ 部分测试失败")
if __name__ == "__main__":
asyncio.run(main())