Просмотр исходного кода

内容: 添加评论检测脚本和启动脚本

Sisyphus Agent 2 недель назад
Родитель
Сommit
363fcaed6f
2 измененных файлов с 124 добавлено и 0 удалено
  1. 2 0
      运营文案/xhs_unreplied_comments.bat
  2. 122 0
      运营文案/xhs_unreplied_comments.py

+ 2 - 0
运营文案/xhs_unreplied_comments.bat

@@ -0,0 +1,2 @@
+@echo off
+C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe "%~dp0xhs_unreplied_comments.py"

+ 122 - 0
运营文案/xhs_unreplied_comments.py

@@ -0,0 +1,122 @@
+# -*- coding: utf-8 -*-
+"""小红书评论检测脚本 - 检测有评论的笔记,推送到飞书群"""
+import io
+import json
+import os
+import re
+import sys
+import time
+
+sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
+
+SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
+XHS_DIR = os.path.dirname(SCRIPT_DIR)
+OPERATION_DIR = os.path.dirname(XHS_DIR)
+CONFIG_PATH = os.path.join(OPERATION_DIR, "xhs_daily_config.json")
+
+XHS_SKILLS_PATH = r"C:\code\XiaohongshuSkills\scripts"
+sys.path.insert(0, XHS_SKILLS_PATH)
+sys.path.insert(0, OPERATION_DIR)
+
+from cdp_publish import XiaohongshuPublisher
+from xhs_feishu import send_daily_report
+
+
+def _load_config():
+    with open(CONFIG_PATH, "r", encoding="utf-8") as f:
+        return json.load(f)
+
+
+def extract_comment_counts(publisher, limit=10):
+    """从笔记管理页提取每篇笔记的评论数"""
+    publisher._navigate("https://creator.xiaohongshu.com/new/note-manager")
+    time.sleep(6)
+
+    text = publisher._evaluate("document.body.innerText") or ""
+    lines = [ln.strip() for ln in text.split("\n")]
+
+    notes = []
+    i = 0
+    while i < len(lines) and len(notes) < limit:
+        ln = lines[i]
+        # 匹配日期格式
+        if re.match(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$", ln) and i + 5 < len(lines):
+            title = lines[i - 1].strip() if i >= 1 else ""
+            # 后面5个数字:阅读/点赞/收藏/评论/分享
+            nums = []
+            j = i + 1
+            while j < len(lines) and len(nums) < 5 and lines[j].isdigit():
+                nums.append(int(lines[j]))
+                j += 1
+            if len(nums) >= 5 and title:
+                notes.append({
+                    "title": title,
+                    "publish_date": ln[:10],
+                    "read_count": nums[0],
+                    "like_count": nums[1],
+                    "collect_count": nums[2],
+                    "comment_count": nums[3],
+                    "share_count": nums[4],
+                })
+            i = j if j > i + 1 else i + 1
+        else:
+            i += 1
+
+    return notes
+
+
+def check_comments():
+    """检查评论并推送到飞书群"""
+    cfg = _load_config()
+
+    publisher = XiaohongshuPublisher()
+    publisher.connect(reuse_existing_tab=True)
+
+    try:
+        if not publisher.check_login():
+            raise RuntimeError("未登录小红书")
+
+        notes = extract_comment_counts(publisher, limit=10)
+        # 筛选有评论的笔记
+        noted_with_comments = [n for n in notes if n["comment_count"] > 0]
+
+        if not noted_with_comments:
+            print("没有发现新评论")
+            return
+
+        # 构建推送消息
+        date = time.strftime("%Y-%m-%d")
+        overview = {
+            "fans_count": 0,
+            "follow_count": 0,
+            "interact_count": 0,
+            "period_7d": {},
+            "period_30d": {},
+        }
+        # 只推送评论笔记列表
+        notes_for_card = noted_with_comments
+
+        print(f"发现 {len(noted_with_comments)} 篇有评论的笔记")
+        for n in noted_with_comments:
+            print(f"  - {n['title']}: {n['comment_count']} 条评论")
+
+        # 发送飞书消息
+        try:
+            send_daily_report(CONFIG_PATH, date, overview, notes_for_card)
+            print("评论通知已推送")
+        except Exception as e:
+            print(f"推送失败: {e}")
+
+    except Exception as e:
+        print(f"检查评论失败: {e}")
+        import traceback
+        traceback.print_exc()
+    finally:
+        try:
+            publisher.disconnect()
+        except Exception:
+            pass
+
+
+if __name__ == "__main__":
+    check_comments()