Merge remote-tracking branch 'upstream/main' into feature/comment-feed-logic
# Conflicts: # mcp_handlers.go # mcp_server.go # service.go # xiaohongshu/like_favorite.go
This commit is contained in:
@@ -129,6 +129,15 @@
|
|||||||
"contributions": [
|
"contributions": [
|
||||||
"code"
|
"code"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"login": "haikow",
|
||||||
|
"name": "haikow",
|
||||||
|
"avatar_url": "https://avatars.githubusercontent.com/u/22428382?v=4",
|
||||||
|
"profile": "https://github.com/haikow",
|
||||||
|
"contributions": [
|
||||||
|
"code"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"contributorsPerLine": 7,
|
"contributorsPerLine": 7,
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -1,7 +1,7 @@
|
|||||||
# xiaohongshu-mcp
|
# xiaohongshu-mcp
|
||||||
|
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||||
[](#contributors-)
|
[](#contributors-)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||||
|
|
||||||
MCP for 小红书/xiaohongshu.com。
|
MCP for 小红书/xiaohongshu.com。
|
||||||
@@ -736,12 +736,9 @@ Cline 是一个强大的 AI 编程助手,支持 MCP 协议集成。
|
|||||||
|
|
||||||
<!-- 两列排布:飞书二群 | 微信群 -->
|
<!-- 两列排布:飞书二群 | 微信群 -->
|
||||||
|
|
||||||
| 【飞书二群】:扫码进入 | 【微信群 6 群】:扫码进入 |
|
| 【飞书二群】:扫码进入 | 【微信群 8 群】:扫码进入 |
|
||||||
| ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
|
| ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| <img src="https://github.com/user-attachments/assets/ca1f5d6e-b1bf-4c15-9975-ff75f339ec9b" alt="qrcode_2qun" width="300"> | <img src="https://github.com/user-attachments/assets/9055afb7-20e5-4e2c-9988-08aaa6956f1f" alt="WechatIMG119" width="300"> |
|
| <img src="https://github.com/user-attachments/assets/ca1f5d6e-b1bf-4c15-9975-ff75f339ec9b" alt="qrcode_2qun" width="300"> | <img src="https://github.com/user-attachments/assets/69b8169f-a3f7-42bc-b197-86c907b69ea0" alt="WechatIMG119" width="300"> |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 🙏 致谢贡献者 ✨
|
## 🙏 致谢贡献者 ✨
|
||||||
@@ -769,6 +766,7 @@ Cline 是一个强大的 AI 编程助手,支持 MCP 协议集成。
|
|||||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/varz1"><img src="https://avatars.githubusercontent.com/u/60377372?v=4?s=100" width="100px;" alt="varz1"/><br /><sub><b>varz1</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=varz1" title="Code">💻</a></td>
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/varz1"><img src="https://avatars.githubusercontent.com/u/60377372?v=4?s=100" width="100px;" alt="varz1"/><br /><sub><b>varz1</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=varz1" title="Code">💻</a></td>
|
||||||
<td align="center" valign="top" width="14.28%"><a href="https://google.meloguan.site"><img src="https://avatars.githubusercontent.com/u/62586556?v=4?s=100" width="100px;" alt="Melo Y Guan"/><br /><sub><b>Melo Y Guan</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=Meloyg" title="Code">💻</a></td>
|
<td align="center" valign="top" width="14.28%"><a href="https://google.meloguan.site"><img src="https://avatars.githubusercontent.com/u/62586556?v=4?s=100" width="100px;" alt="Melo Y Guan"/><br /><sub><b>Melo Y Guan</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=Meloyg" title="Code">💻</a></td>
|
||||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/lmxdawn"><img src="https://avatars.githubusercontent.com/u/21293193?v=4?s=100" width="100px;" alt="lmxdawn"/><br /><sub><b>lmxdawn</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=lmxdawn" title="Code">💻</a></td>
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/lmxdawn"><img src="https://avatars.githubusercontent.com/u/21293193?v=4?s=100" width="100px;" alt="lmxdawn"/><br /><sub><b>lmxdawn</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=lmxdawn" title="Code">💻</a></td>
|
||||||
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/haikow"><img src="https://avatars.githubusercontent.com/u/22428382?v=4?s=100" width="100px;" alt="haikow"/><br /><sub><b>haikow</b></sub></a><br /><a href="https://github.com/xpzouying/xiaohongshu-mcp/commits?author=haikow" title="Code">💻</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -391,38 +391,76 @@ func (s *AppServer) handleUserProfile(ctx context.Context, args map[string]any)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleLikeFeed 处理点赞
|
// handleLikeFeed 处理点赞/取消点赞
|
||||||
func (s *AppServer) handleLikeFeed(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
func (s *AppServer) handleLikeFeed(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||||||
feedID, ok := args["feed_id"].(string)
|
feedID, ok := args["feed_id"].(string)
|
||||||
if !ok || feedID == "" {
|
if !ok || feedID == "" {
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "点赞失败: 缺少feed_id参数"}}, IsError: true}
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少feed_id参数"}}, IsError: true}
|
||||||
}
|
}
|
||||||
xsecToken, ok := args["xsec_token"].(string)
|
xsecToken, ok := args["xsec_token"].(string)
|
||||||
if !ok || xsecToken == "" {
|
if !ok || xsecToken == "" {
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "点赞失败: 缺少xsec_token参数"}}, IsError: true}
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少xsec_token参数"}}, IsError: true}
|
||||||
}
|
}
|
||||||
res, err := s.xiaohongshuService.LikeFeed(ctx, feedID, xsecToken)
|
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 {
|
if err != nil {
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "点赞失败: " + err.Error()}}, IsError: true}
|
action := "点赞"
|
||||||
|
if unlike {
|
||||||
|
action = "取消点赞"
|
||||||
|
}
|
||||||
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: action + "失败: " + err.Error()}}, IsError: true}
|
||||||
}
|
}
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: fmt.Sprintf("点赞成功 - Feed ID: %s", res.FeedID)}}}
|
|
||||||
|
action := "点赞"
|
||||||
|
if unlike {
|
||||||
|
action = "取消点赞"
|
||||||
|
}
|
||||||
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: fmt.Sprintf("%s成功 - Feed ID: %s", action, res.FeedID)}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleFavoriteFeed 处理收藏
|
// handleFavoriteFeed 处理收藏/取消收藏
|
||||||
func (s *AppServer) handleFavoriteFeed(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
func (s *AppServer) handleFavoriteFeed(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||||||
feedID, ok := args["feed_id"].(string)
|
feedID, ok := args["feed_id"].(string)
|
||||||
if !ok || feedID == "" {
|
if !ok || feedID == "" {
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "收藏失败: 缺少feed_id参数"}}, IsError: true}
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少feed_id参数"}}, IsError: true}
|
||||||
}
|
}
|
||||||
xsecToken, ok := args["xsec_token"].(string)
|
xsecToken, ok := args["xsec_token"].(string)
|
||||||
if !ok || xsecToken == "" {
|
if !ok || xsecToken == "" {
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "收藏失败: 缺少xsec_token参数"}}, IsError: true}
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "操作失败: 缺少xsec_token参数"}}, IsError: true}
|
||||||
}
|
}
|
||||||
res, err := s.xiaohongshuService.FavoriteFeed(ctx, feedID, xsecToken)
|
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 {
|
if err != nil {
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "收藏失败: " + err.Error()}}, IsError: true}
|
action := "收藏"
|
||||||
|
if unfavorite {
|
||||||
|
action = "取消收藏"
|
||||||
|
}
|
||||||
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: action + "失败: " + err.Error()}}, IsError: true}
|
||||||
}
|
}
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: fmt.Sprintf("收藏成功 - Feed ID: %s", res.FeedID)}}}
|
|
||||||
|
action := "收藏"
|
||||||
|
if unfavorite {
|
||||||
|
action = "取消收藏"
|
||||||
|
}
|
||||||
|
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: fmt.Sprintf("%s成功 - Feed ID: %s", action, res.FeedID)}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlePostComment 处理发表评论到Feed
|
// handlePostComment 处理发表评论到Feed
|
||||||
@@ -486,39 +524,3 @@ func (s *AppServer) handlePostComment(ctx context.Context, args map[string]inter
|
|||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleReplyComment 处理回复评论
|
|
||||||
func (s *AppServer) handleReplyComment(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
|
||||||
logrus.Info("MCP: 回复评论")
|
|
||||||
|
|
||||||
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}
|
|
||||||
}
|
|
||||||
|
|
||||||
commentID, _ := args["comment_id"].(string)
|
|
||||||
userID, _ := args["user_id"].(string)
|
|
||||||
if commentID == "" && userID == "" {
|
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "回复评论失败: 缺少comment_id或user_id参数"}}, IsError: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
content, ok := args["content"].(string)
|
|
||||||
if !ok || content == "" {
|
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "回复评论失败: 缺少content参数"}}, IsError: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("MCP: 回复评论 - Feed ID: %s, Comment ID: %s, User ID: %s, 内容长度: %d", feedID, commentID, userID, len(content))
|
|
||||||
|
|
||||||
result, err := s.xiaohongshuService.ReplyCommentToFeed(ctx, feedID, xsecToken, commentID, userID, content)
|
|
||||||
if err != nil {
|
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: "回复评论失败: " + err.Error()}}, IsError: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
responseText := fmt.Sprintf("评论回复成功 - Feed ID: %s, Comment ID: %s, User ID: %s", result.FeedID, result.TargetCommentID, result.TargetUserID)
|
|
||||||
return &MCPToolResult{Content: []MCPContent{{Type: "text", Text: responseText}}}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -50,19 +50,18 @@ type PostCommentArgs struct {
|
|||||||
Content string `json:"content" jsonschema:"评论内容"`
|
Content string `json:"content" jsonschema:"评论内容"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReplyCommentArgs 回复评论的参数
|
// LikeFeedArgs 点赞参数
|
||||||
type ReplyCommentArgs struct {
|
type LikeFeedArgs struct {
|
||||||
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID,从Feed列表获取"`
|
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID,从Feed列表获取"`
|
||||||
XsecToken string `json:"xsec_token" jsonschema:"访问令牌,从Feed列表的xsecToken字段获取"`
|
XsecToken string `json:"xsec_token" jsonschema:"访问令牌,从Feed列表的xsecToken字段获取"`
|
||||||
CommentID string `json:"comment_id,omitempty" jsonschema:"目标评论ID,从评论列表获取"`
|
Unlike bool `json:"unlike,omitempty" jsonschema:"是否取消点赞,true为取消点赞,false或未设置则为点赞"`
|
||||||
UserID string `json:"user_id,omitempty" jsonschema:"目标评论作者ID,从评论列表获取"`
|
|
||||||
Content string `json:"content" jsonschema:"回复内容"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LikeFavoriteArgs 点赞/收藏参数
|
// FavoriteFeedArgs 收藏参数
|
||||||
type LikeFavoriteArgs struct {
|
type FavoriteFeedArgs struct {
|
||||||
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID,从Feed列表获取"`
|
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID,从Feed列表获取"`
|
||||||
XsecToken string `json:"xsec_token" jsonschema:"访问令牌,从Feed列表的xsecToken字段获取"`
|
XsecToken string `json:"xsec_token" jsonschema:"访问令牌,从Feed列表的xsecToken字段获取"`
|
||||||
|
Unfavorite bool `json:"unfavorite,omitempty" jsonschema:"是否取消收藏,true为取消收藏,false或未设置则为收藏"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitMCPServer 初始化 MCP Server
|
// InitMCPServer 初始化 MCP Server
|
||||||
@@ -205,33 +204,7 @@ func registerTools(server *mcp.Server, appServer *AppServer) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// 工具 9: 回复评论
|
// 工具 9: 发布视频(仅本地文件)
|
||||||
mcp.AddTool(server,
|
|
||||||
&mcp.Tool{
|
|
||||||
Name: "reply_comment_in_feed",
|
|
||||||
Description: "回复小红书笔记下的指定评论",
|
|
||||||
},
|
|
||||||
func(ctx context.Context, req *mcp.CallToolRequest, args ReplyCommentArgs) (*mcp.CallToolResult, any, error) {
|
|
||||||
if args.CommentID == "" && args.UserID == "" {
|
|
||||||
return &mcp.CallToolResult{
|
|
||||||
IsError: true,
|
|
||||||
Content: []mcp.Content{&mcp.TextContent{Text: "缺少 comment_id 或 user_id"}},
|
|
||||||
}, nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
argsMap := map[string]interface{}{
|
|
||||||
"feed_id": args.FeedID,
|
|
||||||
"xsec_token": args.XsecToken,
|
|
||||||
"comment_id": args.CommentID,
|
|
||||||
"user_id": args.UserID,
|
|
||||||
"content": args.Content,
|
|
||||||
}
|
|
||||||
result := appServer.handleReplyComment(ctx, argsMap)
|
|
||||||
return convertToMCPResult(result), nil, nil
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// 工具 10: 发布视频(仅本地文件)
|
|
||||||
mcp.AddTool(server,
|
mcp.AddTool(server,
|
||||||
&mcp.Tool{
|
&mcp.Tool{
|
||||||
Name: "publish_with_video",
|
Name: "publish_with_video",
|
||||||
@@ -249,39 +222,41 @@ func registerTools(server *mcp.Server, appServer *AppServer) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// 工具 11: 点赞笔记
|
// 工具 10: 点赞笔记
|
||||||
mcp.AddTool(server,
|
mcp.AddTool(server,
|
||||||
&mcp.Tool{
|
&mcp.Tool{
|
||||||
Name: "like_feed",
|
Name: "like_feed",
|
||||||
Description: "为指定笔记点赞(如已点赞将跳过)",
|
Description: "为指定笔记点赞或取消点赞(如已点赞将跳过点赞,如未点赞将跳过取消点赞)",
|
||||||
},
|
},
|
||||||
func(ctx context.Context, req *mcp.CallToolRequest, args LikeFavoriteArgs) (*mcp.CallToolResult, any, error) {
|
func(ctx context.Context, req *mcp.CallToolRequest, args LikeFeedArgs) (*mcp.CallToolResult, any, error) {
|
||||||
argsMap := map[string]interface{}{
|
argsMap := map[string]interface{}{
|
||||||
"feed_id": args.FeedID,
|
"feed_id": args.FeedID,
|
||||||
"xsec_token": args.XsecToken,
|
"xsec_token": args.XsecToken,
|
||||||
|
"unlike": args.Unlike,
|
||||||
}
|
}
|
||||||
result := appServer.handleLikeFeed(ctx, argsMap)
|
result := appServer.handleLikeFeed(ctx, argsMap)
|
||||||
return convertToMCPResult(result), nil, nil
|
return convertToMCPResult(result), nil, nil
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// 工具 12: 收藏笔记
|
// 工具 11: 收藏笔记
|
||||||
mcp.AddTool(server,
|
mcp.AddTool(server,
|
||||||
&mcp.Tool{
|
&mcp.Tool{
|
||||||
Name: "favorite_feed",
|
Name: "favorite_feed",
|
||||||
Description: "收藏指定笔记(如已收藏将跳过)",
|
Description: "收藏指定笔记或取消收藏(如已收藏将跳过收藏,如未收藏将跳过取消收藏)",
|
||||||
},
|
},
|
||||||
func(ctx context.Context, req *mcp.CallToolRequest, args LikeFavoriteArgs) (*mcp.CallToolResult, any, error) {
|
func(ctx context.Context, req *mcp.CallToolRequest, args FavoriteFeedArgs) (*mcp.CallToolResult, any, error) {
|
||||||
argsMap := map[string]interface{}{
|
argsMap := map[string]interface{}{
|
||||||
"feed_id": args.FeedID,
|
"feed_id": args.FeedID,
|
||||||
"xsec_token": args.XsecToken,
|
"xsec_token": args.XsecToken,
|
||||||
|
"unfavorite": args.Unfavorite,
|
||||||
}
|
}
|
||||||
result := appServer.handleFavoriteFeed(ctx, argsMap)
|
result := appServer.handleFavoriteFeed(ctx, argsMap)
|
||||||
return convertToMCPResult(result), nil, nil
|
return convertToMCPResult(result), nil, nil
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
logrus.Infof("Registered %d MCP tools", 12)
|
logrus.Infof("Registered %d MCP tools", 11)
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertToMCPResult 将自定义的 MCPToolResult 转换为官方 SDK 的格式
|
// convertToMCPResult 将自定义的 MCPToolResult 转换为官方 SDK 的格式
|
||||||
|
|||||||
64
service.go
64
service.go
@@ -20,13 +20,6 @@ import (
|
|||||||
// XiaohongshuService 小红书业务服务
|
// XiaohongshuService 小红书业务服务
|
||||||
type XiaohongshuService struct{}
|
type XiaohongshuService struct{}
|
||||||
|
|
||||||
// ActionResult 通用动作响应(点赞/收藏等)
|
|
||||||
type ActionResult struct {
|
|
||||||
FeedID string `json:"feed_id"`
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewXiaohongshuService 创建小红书服务实例
|
// NewXiaohongshuService 创建小红书服务实例
|
||||||
func NewXiaohongshuService() *XiaohongshuService {
|
func NewXiaohongshuService() *XiaohongshuService {
|
||||||
return &XiaohongshuService{}
|
return &XiaohongshuService{}
|
||||||
@@ -390,29 +383,6 @@ func (s *XiaohongshuService) PostCommentToFeed(ctx context.Context, feedID, xsec
|
|||||||
return &PostCommentResponse{FeedID: feedID, Success: true, Message: "评论发表成功"}, nil
|
return &PostCommentResponse{FeedID: feedID, Success: true, Message: "评论发表成功"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReplyCommentToFeed 回复指定评论
|
|
||||||
func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xsecToken, commentID, userID, content string) (*ReplyCommentResponse, error) {
|
|
||||||
b := newBrowser()
|
|
||||||
defer b.Close()
|
|
||||||
|
|
||||||
page := b.NewPage()
|
|
||||||
defer page.Close()
|
|
||||||
|
|
||||||
action := xiaohongshu.NewCommentFeedAction(page)
|
|
||||||
|
|
||||||
if err := action.ReplyToComment(ctx, feedID, xsecToken, commentID, userID, content); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ReplyCommentResponse{
|
|
||||||
FeedID: feedID,
|
|
||||||
TargetCommentID: commentID,
|
|
||||||
TargetUserID: userID,
|
|
||||||
Success: true,
|
|
||||||
Message: "评论回复成功",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LikeFeed 点赞笔记
|
// LikeFeed 点赞笔记
|
||||||
func (s *XiaohongshuService) LikeFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
|
func (s *XiaohongshuService) LikeFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
|
||||||
b := newBrowser()
|
b := newBrowser()
|
||||||
@@ -421,13 +391,28 @@ func (s *XiaohongshuService) LikeFeed(ctx context.Context, feedID, xsecToken str
|
|||||||
page := b.NewPage()
|
page := b.NewPage()
|
||||||
defer page.Close()
|
defer page.Close()
|
||||||
|
|
||||||
action := xiaohongshu.NewLikeFavoriteAction(page)
|
action := xiaohongshu.NewLikeAction(page)
|
||||||
if err := action.Like(ctx, feedID, xsecToken); err != nil {
|
if err := action.Like(ctx, feedID, xsecToken); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &ActionResult{FeedID: feedID, Success: true, Message: "点赞成功或已点赞"}, nil
|
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 收藏笔记
|
// FavoriteFeed 收藏笔记
|
||||||
func (s *XiaohongshuService) FavoriteFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
|
func (s *XiaohongshuService) FavoriteFeed(ctx context.Context, feedID, xsecToken string) (*ActionResult, error) {
|
||||||
b := newBrowser()
|
b := newBrowser()
|
||||||
@@ -436,13 +421,28 @@ func (s *XiaohongshuService) FavoriteFeed(ctx context.Context, feedID, xsecToken
|
|||||||
page := b.NewPage()
|
page := b.NewPage()
|
||||||
defer page.Close()
|
defer page.Close()
|
||||||
|
|
||||||
action := xiaohongshu.NewLikeFavoriteAction(page)
|
action := xiaohongshu.NewFavoriteAction(page)
|
||||||
if err := action.Favorite(ctx, feedID, xsecToken); err != nil {
|
if err := action.Favorite(ctx, feedID, xsecToken); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &ActionResult{FeedID: feedID, Success: true, Message: "收藏成功或已收藏"}, nil
|
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 {
|
func newBrowser() *headless_browser.Browser {
|
||||||
return browser.NewBrowser(configs.IsHeadless(), browser.WithBinPath(configs.GetBinPath()))
|
return browser.NewBrowser(configs.IsHeadless(), browser.WithBinPath(configs.GetBinPath()))
|
||||||
}
|
}
|
||||||
|
|||||||
7
types.go
7
types.go
@@ -81,3 +81,10 @@ type UserProfileRequest struct {
|
|||||||
UserID string `json:"user_id" binding:"required"`
|
UserID string `json:"user_id" binding:"required"`
|
||||||
XsecToken string `json:"xsec_token" 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"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,175 +4,215 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
"github.com/go-rod/rod/lib/proto"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LikeFavoriteAction 点赞/收藏 动作
|
// ActionResult 通用动作响应(点赞/收藏等)
|
||||||
// 提供在笔记详情页执行点赞和收藏的能力,并在可能的情况下避免重复点击
|
type ActionResult struct {
|
||||||
// 通过读取 window.__INITIAL_STATE__ 判断当前状态
|
FeedID string `json:"feed_id"`
|
||||||
// 并尽量采用多种选择器/文案做回退,避免因页面样式变更导致失败
|
Success bool `json:"success"`
|
||||||
// 注意:该实现依赖页面 DOM,可能随页面升级而变化
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
type LikeFavoriteAction struct {
|
// 选择器常量
|
||||||
|
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
|
page *rod.Page
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLikeFavoriteAction(page *rod.Page) *LikeFavoriteAction {
|
func newInteractAction(page *rod.Page) *interactAction {
|
||||||
return &LikeFavoriteAction{page: page}
|
return &interactAction{page: page}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Like 点赞指定笔记,如果已点赞则直接返回
|
func (a *interactAction) preparePage(ctx context.Context, actionType interactActionType, feedID, xsecToken string) *rod.Page {
|
||||||
func (a *LikeFavoriteAction) Like(ctx context.Context, feedID, xsecToken string) error {
|
|
||||||
page := a.page.Context(ctx).Timeout(60 * time.Second)
|
page := a.page.Context(ctx).Timeout(60 * time.Second)
|
||||||
url := makeFeedDetailURL(feedID, xsecToken)
|
url := makeFeedDetailURL(feedID, xsecToken)
|
||||||
logrus.Infof("Opening feed detail page for like: %s", url)
|
logrus.Infof("Opening feed detail page for %s: %s", actionType, url)
|
||||||
|
|
||||||
page.MustNavigate(url)
|
page.MustNavigate(url)
|
||||||
page.MustWaitDOMStable()
|
page.MustWaitDOMStable()
|
||||||
time.Sleep(1 * time.Second)
|
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)
|
liked, _, err := a.getInteractState(page, feedID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnf("failed to read interact state: %v (continue to try clicking)", err)
|
logrus.Warnf("failed to read interact state: %v (continue to try clicking)", err)
|
||||||
} else if liked {
|
return a.toggleLike(page, feedID, targetLiked, actionType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if targetLiked && liked {
|
||||||
logrus.Infof("feed %s already liked, skip clicking", feedID)
|
logrus.Infof("feed %s already liked, skip clicking", feedID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if !targetLiked && !liked {
|
||||||
// 依次尝试多种选择器或按文案匹配
|
logrus.Infof("feed %s not liked yet, skip clicking", feedID)
|
||||||
selectors := []string{
|
|
||||||
"span.like-lottie", // 页面提供的喜欢图标容器 (根据您提供的HTML)
|
|
||||||
".like-lottie", // 页面提供的喜欢图标容器
|
|
||||||
"button.like", // 常见按钮类名
|
|
||||||
"div.interaction-bar .like", // 交互区域 like
|
|
||||||
"div.footer .like", // 底部工具栏
|
|
||||||
".side-action .like", // 侧边操作栏
|
|
||||||
".like-wrapper", // 包裹元素
|
|
||||||
".interactions .like", // 通用交互区
|
|
||||||
}
|
|
||||||
// 同时尝试 SVG use 的 like 图标
|
|
||||||
selectors = append(selectors,
|
|
||||||
"svg.like-icon", "use[href='#like']", "use[xlink\\:href='#like']",
|
|
||||||
)
|
|
||||||
textCandidates := []string{"点赞", "赞", "喜欢"}
|
|
||||||
if err := clickFirstMatch(page, selectors, textCandidates); err != nil {
|
|
||||||
return errors.Wrap(err, "点击点赞按钮失败")
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(3 * time.Second) // 增加等待时间,确保状态更新
|
|
||||||
|
|
||||||
// 验证点赞是否成功
|
|
||||||
newLiked, _, err := a.getInteractState(page, feedID)
|
|
||||||
if err == nil && newLiked {
|
|
||||||
logrus.Infof("feed %s 点赞成功", feedID)
|
|
||||||
return nil
|
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 {
|
if err != nil {
|
||||||
logrus.Warnf("验证点赞状态失败: %v", err)
|
logrus.Warnf("验证%s状态失败: %v", actionType, err)
|
||||||
} else {
|
return nil
|
||||||
logrus.Warnf("feed %s 点赞可能未成功,状态未变化,尝试再次点击", feedID)
|
}
|
||||||
// 如果第一次点击失败,尝试再次点击
|
if liked == targetLiked {
|
||||||
if err := clickFirstMatch(page, selectors, textCandidates); err != nil {
|
logrus.Infof("feed %s %s成功", feedID, actionType)
|
||||||
logrus.Warnf("第二次点击点赞按钮也失败: %v", err)
|
return nil
|
||||||
} else {
|
}
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
newLiked2, _, err2 := a.getInteractState(page, feedID)
|
logrus.Warnf("feed %s %s可能未成功,状态未变化,尝试再次点击", feedID, actionType)
|
||||||
if err2 == nil && newLiked2 {
|
a.performClick(page, SelectorLikeButton)
|
||||||
logrus.Infof("feed %s 第二次点击点赞成功", feedID)
|
time.Sleep(2 * time.Second)
|
||||||
return nil
|
|
||||||
} else if err2 == nil && !newLiked2 {
|
liked, _, err = a.getInteractState(page, feedID)
|
||||||
logrus.Warnf("feed %s 第二次点击后取消了点赞,这是正常行为", feedID)
|
if err != nil {
|
||||||
return nil
|
logrus.Warnf("第二次验证%s状态失败: %v", actionType, err)
|
||||||
}
|
return nil
|
||||||
}
|
}
|
||||||
|
if liked == targetLiked {
|
||||||
|
logrus.Infof("feed %s 第二次点击%s成功", feedID, actionType)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Favorite 收藏指定笔记,如果已收藏则直接返回
|
// FavoriteAction 负责处理收藏相关交互
|
||||||
func (a *LikeFavoriteAction) Favorite(ctx context.Context, feedID, xsecToken string) error {
|
type FavoriteAction struct {
|
||||||
page := a.page.Context(ctx).Timeout(60 * time.Second)
|
*interactAction
|
||||||
url := makeFeedDetailURL(feedID, xsecToken)
|
}
|
||||||
logrus.Infof("Opening feed detail page for favorite: %s", url)
|
|
||||||
|
|
||||||
page.MustNavigate(url)
|
func NewFavoriteAction(page *rod.Page) *FavoriteAction {
|
||||||
page.MustWaitDOMStable()
|
return &FavoriteAction{interactAction: newInteractAction(page)}
|
||||||
time.Sleep(1 * time.Second)
|
}
|
||||||
|
|
||||||
|
// 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)
|
_, collected, err := a.getInteractState(page, feedID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnf("failed to read interact state: %v (continue to try clicking)", err)
|
logrus.Warnf("failed to read interact state: %v (continue to try clicking)", err)
|
||||||
} else if collected {
|
return a.toggleFavorite(page, feedID, targetCollected, actionType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if targetCollected && collected {
|
||||||
logrus.Infof("feed %s already favorited, skip clicking", feedID)
|
logrus.Infof("feed %s already favorited, skip clicking", feedID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if !targetCollected && !collected {
|
||||||
selectors := []string{
|
logrus.Infof("feed %s not favorited yet, skip clicking", feedID)
|
||||||
"#note-page-collect-board-guide", // 直接通过ID点击收藏按钮容器
|
|
||||||
".collect-wrapper", // 收藏按钮的包裹容器
|
|
||||||
".collect-wrapper svg", // 容器内的SVG
|
|
||||||
".collect-wrapper .reds-icon.collect-icon", // 容器内的收藏图标
|
|
||||||
".collect-wrapper use", // 容器内的use元素
|
|
||||||
"use[xlink:href='#collect']", // 直接点击SVG内部的use元素
|
|
||||||
"use[href='#collect']", // 备用use选择器
|
|
||||||
"svg.reds-icon.collect-icon use", // SVG内部的use元素
|
|
||||||
"svg.reds-icon.collect-icon", // SVG容器(可能需要点击父容器)
|
|
||||||
".reds-icon.collect-icon use", // 类组合的use元素
|
|
||||||
".reds-icon.collect-icon", // 类组合的容器
|
|
||||||
"svg.collect-icon use", // 通用SVG收藏图标内部的use
|
|
||||||
"svg.collect-icon", // 通用SVG收藏图标
|
|
||||||
".collect-icon", // 通用收藏图标类
|
|
||||||
"button.collect", // 常见按钮类名(收藏/收藏夹)
|
|
||||||
"button.favorite",
|
|
||||||
"div.interaction-bar .collect",
|
|
||||||
"div.footer .collect",
|
|
||||||
".side-action .collect",
|
|
||||||
".interactions .collect",
|
|
||||||
}
|
|
||||||
textCandidates := []string{"收藏", "收藏夹", "喜欢"}
|
|
||||||
if err := clickFirstMatch(page, selectors, textCandidates); err != nil {
|
|
||||||
return errors.Wrap(err, "点击收藏按钮失败")
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(3 * time.Second) // 增加等待时间,确保状态更新
|
|
||||||
|
|
||||||
// 验证收藏是否成功
|
|
||||||
_, newCollected, err := a.getInteractState(page, feedID)
|
|
||||||
if err == nil && newCollected {
|
|
||||||
logrus.Infof("feed %s 收藏成功", feedID)
|
|
||||||
return nil
|
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 {
|
if err != nil {
|
||||||
logrus.Warnf("验证收藏状态失败: %v", err)
|
logrus.Warnf("验证%s状态失败: %v", actionType, err)
|
||||||
} else {
|
return nil
|
||||||
logrus.Warnf("feed %s 收藏可能未成功,状态未变化,尝试再次点击", feedID)
|
}
|
||||||
// 如果第一次点击失败,尝试再次点击
|
if collected == targetCollected {
|
||||||
if err := clickFirstMatch(page, selectors, textCandidates); err != nil {
|
logrus.Infof("feed %s %s成功", feedID, actionType)
|
||||||
logrus.Warnf("第二次点击收藏按钮也失败: %v", err)
|
return nil
|
||||||
} else {
|
}
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
_, newCollected2, err2 := a.getInteractState(page, feedID)
|
logrus.Warnf("feed %s %s可能未成功,状态未变化,尝试再次点击", feedID, actionType)
|
||||||
if err2 == nil && newCollected2 {
|
a.performClick(page, SelectorCollectButton)
|
||||||
logrus.Infof("feed %s 第二次点击收藏成功", feedID)
|
time.Sleep(2 * time.Second)
|
||||||
return nil
|
|
||||||
}
|
_, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getInteractState 从 __INITIAL_STATE__ 读取笔记的点赞/收藏状态
|
// getInteractState 从 __INITIAL_STATE__ 读取笔记的点赞/收藏状态
|
||||||
func (a *LikeFavoriteAction) getInteractState(page *rod.Page, feedID string) (liked bool, collected bool, err error) {
|
func (a *interactAction) getInteractState(page *rod.Page, feedID string) (liked bool, collected bool, err error) {
|
||||||
result := page.MustEval(`() => {
|
result := page.MustEval(`() => {
|
||||||
if (window.__INITIAL_STATE__) {
|
if (window.__INITIAL_STATE__) {
|
||||||
return JSON.stringify(window.__INITIAL_STATE__);
|
return JSON.stringify(window.__INITIAL_STATE__);
|
||||||
@@ -205,128 +245,3 @@ func (a *LikeFavoriteAction) getInteractState(page *rod.Page, feedID string) (li
|
|||||||
}
|
}
|
||||||
return detail.Note.InteractInfo.Liked, detail.Note.InteractInfo.Collected, nil
|
return detail.Note.InteractInfo.Liked, detail.Note.InteractInfo.Collected, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// clickFirstMatch 依次尝试选择器点击;若失败,尝试按按钮/链接文本模糊匹配
|
|
||||||
func clickFirstMatch(page *rod.Page, selectors []string, textCandidates []string) error {
|
|
||||||
// 1) 尝试按选择器查找多个元素并点击(优先点击最后一个,即笔记的点赞按钮)
|
|
||||||
for _, sel := range selectors {
|
|
||||||
if els, err := page.Elements(sel); err == nil && len(els) > 0 {
|
|
||||||
// 从最后一个元素开始尝试(笔记的点赞按钮通常在评论区之前)
|
|
||||||
for i := len(els) - 1; i >= 0; i-- {
|
|
||||||
if tryClickChain(els[i]) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 单个元素回退
|
|
||||||
if el, err := page.Element(sel); err == nil && el != nil {
|
|
||||||
if tryClickChain(el) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 2) 文案匹配:在按钮/链接/容器中查找包含文案的元素
|
|
||||||
for _, txt := range textCandidates {
|
|
||||||
if els, err := page.Elements("button, a, div, span, svg, use"); err == nil && len(els) > 0 {
|
|
||||||
// 从最后一个元素开始尝试匹配文本
|
|
||||||
for i := len(els) - 1; i >= 0; i-- {
|
|
||||||
text, _ := els[i].Text()
|
|
||||||
if strings.Contains(strings.ToLower(text), strings.ToLower(txt)) {
|
|
||||||
if tryClickChain(els[i]) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 单个元素回退
|
|
||||||
if el, err := page.ElementR("button, a, div, span, svg, use", fmt.Sprintf("(?i)%s", regexpEscape(txt))); err == nil && el != nil {
|
|
||||||
if tryClickChain(el) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errors.New("no clickable element matched for selectors/text")
|
|
||||||
}
|
|
||||||
|
|
||||||
// tryClickChain 对元素自身及其若干父级尝试点击(scrollIntoView + js click + rod click)
|
|
||||||
func tryClickChain(el *rod.Element) bool {
|
|
||||||
current := el
|
|
||||||
for i := 0; i < 6 && current != nil; i++ {
|
|
||||||
if clickElement(current) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
parent, _ := current.Parent()
|
|
||||||
current = parent
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func clickElement(el *rod.Element) bool {
|
|
||||||
defer func() { _ = recover() }()
|
|
||||||
// 滚动到可见区域
|
|
||||||
_, _ = el.Eval(`() => { try { this.scrollIntoView({block: "center", inline: "center", behavior: "instant"}); } catch (e) {} return true }`)
|
|
||||||
|
|
||||||
// 检查元素类型,对SVG元素使用特殊处理 - 简化处理,直接尝试所有方法
|
|
||||||
// 不检查元素类型,直接尝试多种点击方式
|
|
||||||
|
|
||||||
// 1. 尝试触发MouseEvent(对SVG元素特别有效)
|
|
||||||
_, jsErr := el.Eval(`() => {
|
|
||||||
try {
|
|
||||||
const event = new MouseEvent('click', {
|
|
||||||
view: window,
|
|
||||||
bubbles: true,
|
|
||||||
cancelable: true
|
|
||||||
});
|
|
||||||
this.dispatchEvent(event);
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
console.error('MouseEvent click error:', e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}`)
|
|
||||||
if jsErr == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// 优先尝试标准 JS click
|
|
||||||
_, jsErr2 := el.Eval(`() => {
|
|
||||||
try {
|
|
||||||
this.click();
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
console.error('JS click error:', e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}`)
|
|
||||||
if jsErr2 == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// 再尝试 rod 的 Click
|
|
||||||
if err := el.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// regexpEscape 对用户文案做正则转义,避免特殊字符
|
|
||||||
func regexpEscape(s string) string {
|
|
||||||
replacer := strings.NewReplacer(
|
|
||||||
"\\", "\\\\",
|
|
||||||
".", "\\.",
|
|
||||||
"+", "\\+",
|
|
||||||
"*", "\\*",
|
|
||||||
"?", "\\?",
|
|
||||||
"(", "\\(",
|
|
||||||
")", "\\)",
|
|
||||||
"[", "\\[",
|
|
||||||
"]", "\\]",
|
|
||||||
"{", "\\{",
|
|
||||||
"}", "\\}",
|
|
||||||
"^", "\\^",
|
|
||||||
"$", "\\$",
|
|
||||||
"|", "\\|",
|
|
||||||
)
|
|
||||||
return replacer.Replace(s)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user