137 lines
3.6 KiB
Python
137 lines
3.6 KiB
Python
|
|
"""
|
|||
|
|
快速发布脚本
|
|||
|
|
简化命令行调用,避免 JSON 转义问题
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import json
|
|||
|
|
import asyncio
|
|||
|
|
from xhs_publish import XHSPublishService
|
|||
|
|
|
|||
|
|
|
|||
|
|
def load_cookies_from_file(filepath='cookies.json'):
|
|||
|
|
"""从文件加载 Cookie"""
|
|||
|
|
try:
|
|||
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|||
|
|
return json.load(f)
|
|||
|
|
except FileNotFoundError:
|
|||
|
|
print(f"❌ Cookie 文件不存在: {filepath}")
|
|||
|
|
return None
|
|||
|
|
except json.JSONDecodeError as e:
|
|||
|
|
print(f"❌ Cookie 文件格式错误: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
|
|||
|
|
async def quick_publish(
|
|||
|
|
title: str,
|
|||
|
|
content: str,
|
|||
|
|
images: list = None,
|
|||
|
|
tags: list = None,
|
|||
|
|
cookies_file: str = 'cookies.json'
|
|||
|
|
):
|
|||
|
|
"""
|
|||
|
|
快速发布
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
title: 标题
|
|||
|
|
content: 内容
|
|||
|
|
images: 图片列表(支持本地路径和网络 URL)
|
|||
|
|
tags: 标签列表
|
|||
|
|
cookies_file: Cookie 文件路径
|
|||
|
|
"""
|
|||
|
|
# 加载 Cookie
|
|||
|
|
cookies = load_cookies_from_file(cookies_file)
|
|||
|
|
if not cookies:
|
|||
|
|
return {
|
|||
|
|
"success": False,
|
|||
|
|
"error": "无法加载 Cookie"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 创建发布服务
|
|||
|
|
publisher = XHSPublishService(cookies)
|
|||
|
|
|
|||
|
|
# 执行发布
|
|||
|
|
result = await publisher.publish(
|
|||
|
|
title=title,
|
|||
|
|
content=content,
|
|||
|
|
images=images,
|
|||
|
|
tags=tags
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""
|
|||
|
|
命令行入口
|
|||
|
|
|
|||
|
|
使用方式:
|
|||
|
|
python quick_publish.py "标题" "内容" "图片1,图片2,图片3" "标签1,标签2"
|
|||
|
|
python quick_publish.py "标题" "内容" "" "标签1,标签2" # 不使用图片
|
|||
|
|
"""
|
|||
|
|
if len(sys.argv) < 3:
|
|||
|
|
print("使用方式:")
|
|||
|
|
print(' python quick_publish.py "标题" "内容" ["图片1,图片2"] ["标签1,标签2"]')
|
|||
|
|
print()
|
|||
|
|
print("示例:")
|
|||
|
|
print(' python quick_publish.py "测试笔记" "这是内容" "https://picsum.photos/800/600,D:/test.jpg" "测试,自动化"')
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
# 解析参数
|
|||
|
|
title = sys.argv[1]
|
|||
|
|
content = sys.argv[2]
|
|||
|
|
|
|||
|
|
# 解析图片(逗号分隔)
|
|||
|
|
images = []
|
|||
|
|
if len(sys.argv) > 3 and sys.argv[3].strip():
|
|||
|
|
images = [img.strip() for img in sys.argv[3].split(',') if img.strip()]
|
|||
|
|
|
|||
|
|
# 解析标签(逗号分隔)
|
|||
|
|
tags = []
|
|||
|
|
if len(sys.argv) > 4 and sys.argv[4].strip():
|
|||
|
|
tags = [tag.strip() for tag in sys.argv[4].split(',') if tag.strip()]
|
|||
|
|
|
|||
|
|
# Cookie 文件路径(可选)
|
|||
|
|
cookies_file = sys.argv[5] if len(sys.argv) > 5 else 'cookies.json'
|
|||
|
|
|
|||
|
|
print("="*50)
|
|||
|
|
print("快速发布小红书笔记")
|
|||
|
|
print("="*50)
|
|||
|
|
print(f"标题: {title}")
|
|||
|
|
print(f"内容: {content[:100]}{'...' if len(content) > 100 else ''}")
|
|||
|
|
print(f"图片: {len(images)} 张")
|
|||
|
|
if images:
|
|||
|
|
for i, img in enumerate(images, 1):
|
|||
|
|
print(f" {i}. {img}")
|
|||
|
|
print(f"标签: {tags}")
|
|||
|
|
print(f"Cookie: {cookies_file}")
|
|||
|
|
print("="*50)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 执行发布
|
|||
|
|
result = asyncio.run(quick_publish(
|
|||
|
|
title=title,
|
|||
|
|
content=content,
|
|||
|
|
images=images if images else None,
|
|||
|
|
tags=tags if tags else None,
|
|||
|
|
cookies_file=cookies_file
|
|||
|
|
))
|
|||
|
|
|
|||
|
|
# 输出结果
|
|||
|
|
print()
|
|||
|
|
print("="*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')}")
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|