Files
Agent-Reach/agent_reach/channels/xiaohongshu.py
T

85 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""XiaoHongShu -- check if mcporter + xiaohongshu MCP is available."""
import platform
import shutil
import subprocess
from .base import Channel
def _is_arm64() -> bool:
"""Detect ARM64 architecture (e.g. Apple Silicon)."""
machine = platform.machine().lower()
return machine in ("arm64", "aarch64")
def _docker_run_hint() -> str:
"""Return the docker run command, with --platform flag for ARM64."""
if _is_arm64():
return (
" docker run -d --name xiaohongshu-mcp -p 18060:18060 "
"--platform linux/amd64 xpzouying/xiaohongshu-mcp\n"
" # ARM64 also: build from source: "
"https://github.com/xpzouying/xiaohongshu-mcp"
)
return (
" docker run -d --name xiaohongshu-mcp -p 18060:18060 "
"xpzouying/xiaohongshu-mcp"
)
class XiaoHongShuChannel(Channel):
name = "xiaohongshu"
description = "小红书笔记"
backends = ["xiaohongshu-mcp"]
tier = 2
def can_handle(self, url: str) -> bool:
from urllib.parse import urlparse
d = urlparse(url).netloc.lower()
return "xiaohongshu.com" in d or "xhslink.com" in d
def check(self, config=None):
mcporter = shutil.which("mcporter")
if not mcporter:
return "off", (
"需要 mcporter + xiaohongshu-mcp。安装步骤:\n"
" 1. npm install -g mcporter\n"
" 2. " + _docker_run_hint().strip() + "\n"
" 3. mcporter config add xiaohongshu http://localhost:18060/mcp\n"
" 详见 https://github.com/xpzouying/xiaohongshu-mcp"
)
try:
r = subprocess.run(
[mcporter, "config", "get", "xiaohongshu", "--json"],
capture_output=True,
encoding="utf-8",
errors="replace",
timeout=5,
)
if r.returncode != 0 or "xiaohongshu" not in r.stdout.lower():
return "off", (
"mcporter 已装但小红书 MCP 未配置。运行:\n"
+ _docker_run_hint() + "\n"
" mcporter config add xiaohongshu http://localhost:18060/mcp"
)
except Exception:
return "off", "mcporter 连接异常"
try:
r = subprocess.run(
[mcporter, "list", "xiaohongshu", "--json"],
capture_output=True,
encoding="utf-8",
errors="replace",
timeout=10,
)
out = r.stdout.lower()
if r.returncode == 0 and '"status": "ok"' in out:
return "ok", "MCP 已连接(阅读、搜索、发帖、评论、点赞)"
return "warn", "MCP 已配置,但连接异常;请检查 xiaohongshu-mcp 服务状态"
except subprocess.TimeoutExpired:
return "warn", "MCP 已配置,但健康检查超时;请检查 xiaohongshu-mcp 服务状态"
except Exception:
return "warn", "MCP 已配置,但连接异常;请检查 xiaohongshu-mcp 服务状态"