#!/usr/bin/env python3 """ 小红书热点趋势工作流 基于热门话题创作并发布原创内容 """ import os import sys import json import re import requests from datetime import datetime sys.path.insert(0, os.path.dirname(__file__)) from xiaohongshu_multi_image import XiaohongshuMultiImagePublisher class XHSTrendingWorkflow: """小红书热点工作流""" def __init__(self): self.api_key = os.environ.get('MOONSHOT_API_KEY') self.api_base = os.environ.get('MOONSHOT_API_BASE', 'https://api.moonshot.cn/v1') def search_trending(self, keyword="AI科技", limit=5): """ 搜索小红书热门笔记 注意:这里使用xhs CLI命令,需要在shell中执行 """ import subprocess result = subprocess.run( ['xhs', 'search', keyword], capture_output=True, text=True ) # 解析结果...(简化版) return [] def read_note(self, note_id): """读取笔记详情""" import subprocess result = subprocess.run( ['xhs', 'read', note_id], capture_output=True, text=True ) return result.stdout def generate_content(self, topic): """ 基于话题AI创作原创内容 """ messages = [ {"role": "system", "content": "你是科技内容创作专家,擅长撰写小红书上受欢迎的科技趋势分析文章。"}, {"role": "user", "content": f"""基于"{topic}"这个话题,创作一篇全新的小红书科技笔记。 创作要求: 1. 标题吸引人,20-30字,不用任何markdown符号 2. 正文600-800字,结构清晰 3. 使用-作为列表符号,不要用**加粗 4. 自然融入emoji 5. 包含独到的技术趋势洞察 6. 纯原创,不要复制任何现有文章 7. 结尾带5个话题标签,格式为#标签 输出格式: 标题:[你的标题] 正文:[你的正文] 标签:[标签1] [标签2] [标签3] [标签4] [标签5]"""} ] response = requests.post( f"{self.api_base}/chat/completions", headers={ "Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json" }, json={ "model": "kimi-k2.5", "messages": messages, "temperature": 1, "max_tokens": 1500 }, timeout=60 ) return response.json()['choices'][0]['message']['content'] def publish(self, content_text): """ 发布到小红书 """ # 解析内容 title_match = re.search(r'标题:(.+)', content_text) title = title_match.group(1) if title_match else "科技趋势分析" # 清理内容 content = content_text content = re.sub(r'标题:.+\n', '', content) content = re.sub(r'正文:', '', content) content = re.sub(r'标签:', '\n\n', content) content = re.sub(r'\*\*(.+?)\*\*', r'\1', content) content = re.sub(r'##?\s*', '', content) # 发布 publisher = XiaohongshuMultiImagePublisher() tags = ["科技趋势", "人工智能", "AI产业", "商业洞察", "科技创新"] return publisher.publish_to_xiaohongshu(title, content, tags, "tech") def run(self, topic="AI产业趋势"): """执行完整工作流""" print(f"🚀 开始创作: {topic}") # 1. AI创作内容 print("📝 AI创作原创内容...") content = self.generate_content(topic) # 2. 发布到小红书 print("📱 发布到小红书...") success = self.publish(content) if success: print("✅ 发布成功!") else: print("❌ 发布失败") return success if __name__ == "__main__": workflow = XHSTrendingWorkflow() # 可以从命令行参数获取话题 topic = sys.argv[1] if len(sys.argv) > 1 else "AI产业趋势" workflow.run(topic)