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

81 lines
2.2 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.

"""
测试网络图片下载功能
"""
import asyncio
import json
from xhs_publish import XHSPublishService
async def test_network_images():
"""测试网络图片功能"""
print("="*50)
print("网络图片下载功能测试")
print("="*50)
print()
# 1. 准备测试 Cookie从 cookies.json 读取)
try:
with open('cookies.json', 'r', encoding='utf-8') as f:
cookies = json.load(f)
print(f"✅ 成功读取 {len(cookies)} 个 Cookie")
except FileNotFoundError:
print("❌ cookies.json 文件不存在")
print("请先运行登录获取 Cookie:")
print(" python xhs_cli.py login <手机号> <验证码>")
return
# 2. 准备测试数据
title = "【测试】网络图片发布测试"
content = """测试使用网络 URL 图片发布笔记 📸
本次测试使用了:
✅ 网络 URL 图片picsum.photos
✅ 自动下载功能
✅ 临时文件管理
如果你看到这条笔记,说明网络图片功能正常!"""
# 3. 使用网络图片 URL
images = [
"https://picsum.photos/800/600?random=test1",
"https://picsum.photos/800/600?random=test2",
"https://picsum.photos/800/600?random=test3"
]
print(f"\n测试图片 URL:")
for i, url in enumerate(images, 1):
print(f" {i}. {url}")
tags = ["测试", "网络图片", "自动发布"]
# 4. 创建发布服务
print("\n开始测试发布...")
publisher = XHSPublishService(cookies)
# 5. 执行发布
result = await publisher.publish(
title=title,
content=content,
images=images,
tags=tags,
cleanup=True # 自动清理临时文件
)
# 6. 显示结果
print("\n" + "="*50)
print("测试结果:")
print(json.dumps(result, ensure_ascii=False, indent=2))
print("="*50)
if result.get('success'):
print("\n✅ 测试成功!网络图片功能正常")
if 'url' in result:
print(f"📎 笔记链接: {result['url']}")
else:
print(f"\n❌ 测试失败: {result.get('error')}")
if __name__ == "__main__":
asyncio.run(test_network_images())