91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
"""
|
|
小红书发布功能测试脚本
|
|
快速测试发布功能是否正常工作
|
|
"""
|
|
import asyncio
|
|
import json
|
|
import os
|
|
from xhs_publish import XHSPublishService
|
|
|
|
|
|
async def test_publish():
|
|
"""测试发布功能"""
|
|
|
|
# 1. 从 cookies.json 读取 Cookie
|
|
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
|
|
except Exception as e:
|
|
print(f"❌ 读取 cookies.json 失败: {e}")
|
|
return
|
|
|
|
# 2. 准备测试数据
|
|
title = "【测试】小红书发布功能测试"
|
|
content = """这是一条测试笔记 📝
|
|
|
|
今天测试一下自动发布功能是否正常~
|
|
|
|
如果你看到这条笔记,说明发布成功了!
|
|
|
|
#测试 #自动化"""
|
|
|
|
# 3. 准备测试图片(可选)
|
|
images = []
|
|
test_image_dir = "temp_uploads"
|
|
if os.path.exists(test_image_dir):
|
|
for file in os.listdir(test_image_dir):
|
|
if file.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
|
|
img_path = os.path.abspath(os.path.join(test_image_dir, file))
|
|
images.append(img_path)
|
|
if len(images) >= 3: # 最多3张测试图片
|
|
break
|
|
|
|
if images:
|
|
print(f"✅ 找到 {len(images)} 张测试图片")
|
|
else:
|
|
print("⚠️ 未找到测试图片,将只发布文字")
|
|
|
|
# 4. 准备标签
|
|
tags = ["测试", "自动化发布"]
|
|
|
|
# 5. 创建发布服务
|
|
print("\n开始发布测试笔记...")
|
|
publisher = XHSPublishService(cookies)
|
|
|
|
# 6. 执行发布
|
|
result = await publisher.publish(
|
|
title=title,
|
|
content=content,
|
|
images=images if images else None,
|
|
tags=tags
|
|
)
|
|
|
|
# 7. 显示结果
|
|
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__":
|
|
print("="*50)
|
|
print("小红书发布功能测试")
|
|
print("="*50)
|
|
print()
|
|
|
|
# 运行测试
|
|
asyncio.run(test_publish())
|