feat: add like and favorite functionality for feeds (#207)

* feat: add like and favorite functionality for feeds

- Implemented handleLikeFeed and handleFavoriteFeed methods in mcp_handlers.go to manage liking and favoriting feeds.
- Added LikeFavoriteArgs struct in mcp_server.go for handling parameters.
- Registered new MCP tools for liking and favoriting feeds in registerTools function.
- Introduced LikeFeed and FavoriteFeed methods in XiaohongshuService to interact with the respective actions.
- Created LikeFavoriteAction in a new file to encapsulate the logic for liking and favoriting feeds on the Xiaohongshu platform.

* "Fix-build-errors"

* refactor: streamline like and favorite actions in LikeFavoriteAction

- Introduced a generic method `performInteractAction` to handle both liking and favoriting feeds, reducing code duplication.
- Updated logging to reflect the action type being performed (like or favorite).
- Enhanced state verification after interaction to ensure accurate feedback on success or failure.
- Removed the `clickLastMatch` function, simplifying the interaction logic.

* "Add-unlike-and-unfavorite-functionality"

* "Refactor-performInteractAction-function"

* "Refactor-split-LikeFavoriteAction-into-separate-actions"

---------

Co-authored-by: chekayo <9827969+chekayo@user.noreply.gitee.com>
This commit is contained in:
haikow
2025-10-08 11:40:45 +08:00
committed by GitHub
parent d84bf2e9a2
commit 66aa36b48c
5 changed files with 436 additions and 12 deletions

View File

@@ -391,6 +391,78 @@ func (s *AppServer) handleUserProfile(ctx context.Context, args map[string]any)
}
}
// handleLikeFeed 处理点赞/取消点赞
func (s *AppServer) handleLikeFeed(ctx context.Context, args map[string]interface{}) *MCPToolResult {
feedID, ok := args["feed_id"].(string)
if !ok || feedID == "" {
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少feed_id参数"}}, IsError: true}
}
xsecToken, ok := args["xsec_token"].(string)
if !ok || xsecToken == "" {
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少xsec_token参数"}}, IsError: true}
}
unlike, _ := args["unlike"].(bool)
var res *ActionResult
var err error
if unlike {
res, err = s.xiaohongshuService.UnlikeFeed(ctx, feedID, xsecToken)
} else {
res, err = s.xiaohongshuService.LikeFeed(ctx, feedID, xsecToken)
}
if err != nil {
action := "点赞"
if unlike {
action = "取消点赞"
}
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: action + "失败: " + err.Error()}}, IsError: true}
}
action := "点赞"
if unlike {
action = "取消点赞"
}
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: fmt.Sprintf("%s成功 - Feed ID: %s", action, res.FeedID)}}}
}
// handleFavoriteFeed 处理收藏/取消收藏
func (s *AppServer) handleFavoriteFeed(ctx context.Context, args map[string]interface{}) *MCPToolResult {
feedID, ok := args["feed_id"].(string)
if !ok || feedID == "" {
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少feed_id参数"}}, IsError: true}
}
xsecToken, ok := args["xsec_token"].(string)
if !ok || xsecToken == "" {
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少xsec_token参数"}}, IsError: true}
}
unfavorite, _ := args["unfavorite"].(bool)
var res *ActionResult
var err error
if unfavorite {
res, err = s.xiaohongshuService.UnfavoriteFeed(ctx, feedID, xsecToken)
} else {
res, err = s.xiaohongshuService.FavoriteFeed(ctx, feedID, xsecToken)
}
if err != nil {
action := "收藏"
if unfavorite {
action = "取消收藏"
}
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: action + "失败: " + err.Error()}}, IsError: true}
}
action := "收藏"
if unfavorite {
action = "取消收藏"
}
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: fmt.Sprintf("%s成功 - Feed ID: %s", action, res.FeedID)}}}
}
// handlePostComment 处理发表评论到Feed
func (s *AppServer) handlePostComment(ctx context.Context, args map[string]interface{}) *MCPToolResult {
logrus.Info("MCP: 发表评论到Feed")

View File

@@ -50,6 +50,20 @@ type PostCommentArgs struct {
Content string `json:"content" jsonschema:"评论内容"`
}
// LikeFeedArgs 点赞参数
type LikeFeedArgs struct {
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID从Feed列表获取"`
XsecToken string `json:"xsec_token" jsonschema:"访问令牌从Feed列表的xsecToken字段获取"`
Unlike bool `json:"unlike,omitempty" jsonschema:"是否取消点赞true为取消点赞false或未设置则为点赞"`
}
// FavoriteFeedArgs 收藏参数
type FavoriteFeedArgs struct {
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID从Feed列表获取"`
XsecToken string `json:"xsec_token" jsonschema:"访问令牌从Feed列表的xsecToken字段获取"`
Unfavorite bool `json:"unfavorite,omitempty" jsonschema:"是否取消收藏true为取消收藏false或未设置则为收藏"`
}
// InitMCPServer 初始化 MCP Server
func InitMCPServer(appServer *AppServer) *mcp.Server {
// 创建 MCP Server
@@ -208,7 +222,41 @@ func registerTools(server *mcp.Server, appServer *AppServer) {
},
)
logrus.Infof("Registered %d MCP tools", 9)
// 工具 10: 点赞笔记
mcp.AddTool(server,
&mcp.Tool{
Name: "like_feed",
Description: "为指定笔记点赞或取消点赞(如已点赞将跳过点赞,如未点赞将跳过取消点赞)",
},
func(ctx context.Context, req *mcp.CallToolRequest, args LikeFeedArgs) (*mcp.CallToolResult, any, error) {
argsMap := map[string]interface{}{
"feed_id": args.FeedID,
"xsec_token": args.XsecToken,
"unlike": args.Unlike,
}
result := appServer.handleLikeFeed(ctx, argsMap)
return convertToMCPResult(result), nil, nil
},
)
// 工具 11: 收藏笔记
mcp.AddTool(server,
&mcp.Tool{
Name: "favorite_feed",
Description: "收藏指定笔记或取消收藏(如已收藏将跳过收藏,如未收藏将跳过取消收藏)",
},
func(ctx context.Context, req *mcp.CallToolRequest, args FavoriteFeedArgs) (*mcp.CallToolResult, any, error) {
argsMap := map[string]interface{}{
"feed_id": args.FeedID,
"xsec_token": args.XsecToken,
"unfavorite": args.Unfavorite,
}
result := appServer.handleFavoriteFeed(ctx, argsMap)
return convertToMCPResult(result), nil, nil
},
)
logrus.Infof("Registered %d MCP tools", 11)
}
// convertToMCPResult 将自定义的 MCPToolResult 转换为官方 SDK 的格式

View File

@@ -368,29 +368,79 @@ func (s *XiaohongshuService) UserProfile(ctx context.Context, userID, xsecToken
// PostCommentToFeed 发表评论到Feed
func (s *XiaohongshuService) PostCommentToFeed(ctx context.Context, feedID, xsecToken, content string) (*PostCommentResponse, error) {
// 使用非无头模式以便查看操作过程
b := newBrowser()
defer b.Close()
page := b.NewPage()
defer page.Close()
// 创建 Feed 评论 action
action := xiaohongshu.NewCommentFeedAction(page)
// 发表评论
err := action.PostComment(ctx, feedID, xsecToken, content)
if err != nil {
if err := action.PostComment(ctx, feedID, xsecToken, content); err != nil {
return nil, err
}
response := &PostCommentResponse{
FeedID: feedID,
Success: true,
Message: "评论发表成功",
}
return &PostCommentResponse{FeedID: feedID, Success: true, Message: "评论发表成功"}, nil
}
return response, nil
// LikeFeed 点赞笔记
func (s *XiaohongshuService) LikeFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
b := newBrowser()
defer b.Close()
page := b.NewPage()
defer page.Close()
action := xiaohongshu.NewLikeAction(page)
if err := action.Like(ctx, feedID, xsecToken); err != nil {
return nil, err
}
return &ActionResult{FeedID: feedID, Success: true, Message: "点赞成功或已点赞"}, nil
}
// UnlikeFeed 取消点赞笔记
func (s *XiaohongshuService) UnlikeFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
b := newBrowser()
defer b.Close()
page := b.NewPage()
defer page.Close()
action := xiaohongshu.NewLikeAction(page)
if err := action.Unlike(ctx, feedID, xsecToken); err != nil {
return nil, err
}
return &ActionResult{FeedID: feedID, Success: true, Message: "取消点赞成功或未点赞"}, nil
}
// FavoriteFeed 收藏笔记
func (s *XiaohongshuService) FavoriteFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
b := newBrowser()
defer b.Close()
page := b.NewPage()
defer page.Close()
action := xiaohongshu.NewFavoriteAction(page)
if err := action.Favorite(ctx, feedID, xsecToken); err != nil {
return nil, err
}
return &ActionResult{FeedID: feedID, Success: true, Message: "收藏成功或已收藏"}, nil
}
// UnfavoriteFeed 取消收藏笔记
func (s *XiaohongshuService) UnfavoriteFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
b := newBrowser()
defer b.Close()
page := b.NewPage()
defer page.Close()
action := xiaohongshu.NewFavoriteAction(page)
if err := action.Unfavorite(ctx, feedID, xsecToken); err != nil {
return nil, err
}
return &ActionResult{FeedID: feedID, Success: true, Message: "取消收藏成功或未收藏"}, nil
}
func newBrowser() *headless_browser.Browser {

View File

@@ -63,3 +63,10 @@ type UserProfileRequest struct {
UserID string `json:"user_id" binding:"required"`
XsecToken string `json:"xsec_token" binding:"required"`
}
// ActionResult 通用动作响应(点赞/收藏等)
type ActionResult struct {
FeedID string `json:"feed_id"`
Success bool `json:"success"`
Message string `json:"message"`
}

View File

@@ -0,0 +1,247 @@
package xiaohongshu
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/go-rod/rod"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// ActionResult 通用动作响应(点赞/收藏等)
type ActionResult struct {
FeedID string `json:"feed_id"`
Success bool `json:"success"`
Message string `json:"message"`
}
// 选择器常量
const (
SelectorLikeButton = ".interact-container .left .like-lottie"
SelectorCollectButton = ".interact-container .left .reds-icon.collect-icon"
)
// interactActionType 交互动作类型
type interactActionType string
const (
actionLike interactActionType = "点赞"
actionFavorite interactActionType = "收藏"
actionUnlike interactActionType = "取消点赞"
actionUnfavorite interactActionType = "取消收藏"
)
type interactAction struct {
page *rod.Page
}
func newInteractAction(page *rod.Page) *interactAction {
return &interactAction{page: page}
}
func (a *interactAction) preparePage(ctx context.Context, actionType interactActionType, feedID, xsecToken string) *rod.Page {
page := a.page.Context(ctx).Timeout(60 * time.Second)
url := makeFeedDetailURL(feedID, xsecToken)
logrus.Infof("Opening feed detail page for %s: %s", actionType, url)
page.MustNavigate(url)
page.MustWaitDOMStable()
time.Sleep(1 * time.Second)
return page
}
func (a *interactAction) performClick(page *rod.Page, selector string) {
element := page.MustElement(selector)
element.MustClick()
}
// LikeAction 负责处理点赞相关交互
type LikeAction struct {
*interactAction
}
func NewLikeAction(page *rod.Page) *LikeAction {
return &LikeAction{interactAction: newInteractAction(page)}
}
// Like 点赞指定笔记,如果已点赞则直接返回
func (a *LikeAction) Like(ctx context.Context, feedID, xsecToken string) error {
return a.perform(ctx, feedID, xsecToken, true)
}
// Unlike 取消点赞指定笔记,如果未点赞则直接返回
func (a *LikeAction) Unlike(ctx context.Context, feedID, xsecToken string) error {
return a.perform(ctx, feedID, xsecToken, false)
}
func (a *LikeAction) perform(ctx context.Context, feedID, xsecToken string, targetLiked bool) error {
actionType := actionLike
if !targetLiked {
actionType = actionUnlike
}
page := a.preparePage(ctx, actionType, feedID, xsecToken)
liked, _, err := a.getInteractState(page, feedID)
if err != nil {
logrus.Warnf("failed to read interact state: %v (continue to try clicking)", err)
return a.toggleLike(page, feedID, targetLiked, actionType)
}
if targetLiked && liked {
logrus.Infof("feed %s already liked, skip clicking", feedID)
return nil
}
if !targetLiked && !liked {
logrus.Infof("feed %s not liked yet, skip clicking", feedID)
return nil
}
return a.toggleLike(page, feedID, targetLiked, actionType)
}
func (a *LikeAction) toggleLike(page *rod.Page, feedID string, targetLiked bool, actionType interactActionType) error {
a.performClick(page, SelectorLikeButton)
time.Sleep(3 * time.Second)
liked, _, err := a.getInteractState(page, feedID)
if err != nil {
logrus.Warnf("验证%s状态失败: %v", actionType, err)
return nil
}
if liked == targetLiked {
logrus.Infof("feed %s %s成功", feedID, actionType)
return nil
}
logrus.Warnf("feed %s %s可能未成功状态未变化尝试再次点击", feedID, actionType)
a.performClick(page, SelectorLikeButton)
time.Sleep(2 * time.Second)
liked, _, err = a.getInteractState(page, feedID)
if err != nil {
logrus.Warnf("第二次验证%s状态失败: %v", actionType, err)
return nil
}
if liked == targetLiked {
logrus.Infof("feed %s 第二次点击%s成功", feedID, actionType)
return nil
}
return nil
}
// FavoriteAction 负责处理收藏相关交互
type FavoriteAction struct {
*interactAction
}
func NewFavoriteAction(page *rod.Page) *FavoriteAction {
return &FavoriteAction{interactAction: newInteractAction(page)}
}
// Favorite 收藏指定笔记,如果已收藏则直接返回
func (a *FavoriteAction) Favorite(ctx context.Context, feedID, xsecToken string) error {
return a.perform(ctx, feedID, xsecToken, true)
}
// Unfavorite 取消收藏指定笔记,如果未收藏则直接返回
func (a *FavoriteAction) Unfavorite(ctx context.Context, feedID, xsecToken string) error {
return a.perform(ctx, feedID, xsecToken, false)
}
func (a *FavoriteAction) perform(ctx context.Context, feedID, xsecToken string, targetCollected bool) error {
actionType := actionFavorite
if !targetCollected {
actionType = actionUnfavorite
}
page := a.preparePage(ctx, actionType, feedID, xsecToken)
_, collected, err := a.getInteractState(page, feedID)
if err != nil {
logrus.Warnf("failed to read interact state: %v (continue to try clicking)", err)
return a.toggleFavorite(page, feedID, targetCollected, actionType)
}
if targetCollected && collected {
logrus.Infof("feed %s already favorited, skip clicking", feedID)
return nil
}
if !targetCollected && !collected {
logrus.Infof("feed %s not favorited yet, skip clicking", feedID)
return nil
}
return a.toggleFavorite(page, feedID, targetCollected, actionType)
}
func (a *FavoriteAction) toggleFavorite(page *rod.Page, feedID string, targetCollected bool, actionType interactActionType) error {
a.performClick(page, SelectorCollectButton)
time.Sleep(3 * time.Second)
_, collected, err := a.getInteractState(page, feedID)
if err != nil {
logrus.Warnf("验证%s状态失败: %v", actionType, err)
return nil
}
if collected == targetCollected {
logrus.Infof("feed %s %s成功", feedID, actionType)
return nil
}
logrus.Warnf("feed %s %s可能未成功状态未变化尝试再次点击", feedID, actionType)
a.performClick(page, SelectorCollectButton)
time.Sleep(2 * time.Second)
_, collected, err = a.getInteractState(page, feedID)
if err != nil {
logrus.Warnf("第二次验证%s状态失败: %v", actionType, err)
return nil
}
if collected == targetCollected {
logrus.Infof("feed %s 第二次点击%s成功", feedID, actionType)
return nil
}
return nil
}
// getInteractState 从 __INITIAL_STATE__ 读取笔记的点赞/收藏状态
func (a *interactAction) getInteractState(page *rod.Page, feedID string) (liked bool, collected bool, err error) {
result := page.MustEval(`() => {
if (window.__INITIAL_STATE__) {
return JSON.stringify(window.__INITIAL_STATE__);
}
return "";
}`).String()
if result == "" {
return false, false, fmt.Errorf("__INITIAL_STATE__ not found")
}
var state struct {
Note struct {
NoteDetailMap map[string]struct {
Note struct {
InteractInfo struct {
Liked bool `json:"liked"`
Collected bool `json:"collected"`
} `json:"interactInfo"`
} `json:"note"`
} `json:"noteDetailMap"`
} `json:"note"`
}
if err := json.Unmarshal([]byte(result), &state); err != nil {
return false, false, errors.Wrap(err, "unmarshal __INITIAL_STATE__ failed")
}
detail, ok := state.Note.NoteDetailMap[feedID]
if !ok {
return false, false, fmt.Errorf("feed %s not in noteDetailMap", feedID)
}
return detail.Note.InteractInfo.Liked, detail.Note.InteractInfo.Collected, nil
}