Files
nanocode/TradingAgents/test_publish_debug.py
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

88 lines
2.9 KiB
Python
Raw Permalink 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.
#!/usr/bin/env python3
"""调试 publish 流程,找出 600383 发布失败的原因。"""
import sys
import json
import os
sys.path.insert(0, '/home/ubuntu/.nanobot/workspace/skills/baoyu-post-to-wechat')
# 1. 读取 600383 的分析结果
with open('/home/ubuntu/nanocode/TradingAgents/logs/tasks/600383_20260610161057.json', 'r') as f:
task = json.load(f)
state = task["result"]
print("✅ 读取 600383 分析结果成功")
# 2. 测试 Markdown 组装
from tradingagents.publish.wechat_publisher import _build_markdown, publish_analysis_result
md = _build_markdown(state, "600383", "2026-06-10")
print(f"✅ Markdown 组装成功,长度: {len(md)} 字符")
# 3. 测试保存到磁盘
results_dir = os.path.join(os.path.expanduser("~"), "TradingAgents", "logs")
publish_dir = os.path.join(results_dir, "600383", "published")
os.makedirs(publish_dir, exist_ok=True)
md_path = os.path.join(publish_dir, "analysis_2026-06-10.md")
with open(md_path, "w", encoding="utf-8") as f:
f.write(md)
print(f"✅ Markdown 已保存到: {md_path}")
# 4. 测试 WeChatPublisher 初始化
try:
from baoyu_optimized import WeChatPublisher
publisher = WeChatPublisher()
print(f"✅ WeChatPublisher 初始化成功")
print(f" 配置: {publisher.config}")
except Exception as e:
print(f"❌ WeChatPublisher 初始化失败: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
# 5. 测试获取 access_token
try:
token = publisher.get_access_token()
print(f"✅ 获取 access_token 成功: {token[:20]}...")
except Exception as e:
print(f"❌ 获取 access_token 失败: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
# 6. 测试 blog_sync 导入(这是发布流程的一部分)
print("\n🔍 测试 blog_sync 导入...")
try:
from blog_sync import sync_markdown_to_blog
print("✅ blog_sync 导入成功")
except Exception as e:
print(f"❌ blog_sync 导入失败: {e}")
import traceback
traceback.print_exc()
# 7. 检查封面图片是否存在
cover = publisher.config.get('default_cover_image')
print(f"\n🔍 检查封面图片: {cover}")
if cover and os.path.exists(cover):
print("✅ 封面图片存在")
else:
print(f"⚠️ 封面图片不存在或路径为空")
# 8. 测试解析 frontmatter(publish_markdown_with_images 的前几步)
print("\n🔍 测试 Markdown 解析...")
try:
with open(md_path, 'r', encoding='utf-8') as f:
content = f.read()
frontmatter, body = publisher.markdown_parser.parse_frontmatter(content)
print(f"✅ Frontmatter 解析成功: {frontmatter}")
images = publisher.extract_images_from_markdown(body)
print(f"✅ 提取到 {len(images)} 张图片")
except Exception as e:
print(f"❌ 解析失败: {e}")
import traceback
traceback.print_exc()
print("\n--- 调试完成,未实际调用微信 API ---")
print("如需测试完整发布流程,请手动调用 publisher.publish_markdown_with_images()")