- 新增 SearchFeeds 服务方法,支持关键词搜索小红书内容 - 添加 search_feeds MCP 工具,提供搜索接口 - 新增 /api/v1/feeds/search API 端点 - 实现搜索页面的浏览器自动化操作 - 优化 MCP 协议支持,处理 notifications/initialized 和 notifications/cancelled 通知 - 更新文档,添加搜索功能说明和使用示例 - 重构类型定义,优化数据结构 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com>
170 lines
3.9 KiB
Go
170 lines
3.9 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/xpzouying/xiaohongshu-mcp/browser"
|
||
"github.com/xpzouying/xiaohongshu-mcp/configs"
|
||
"github.com/xpzouying/xiaohongshu-mcp/pkg/downloader"
|
||
"github.com/xpzouying/xiaohongshu-mcp/xiaohongshu"
|
||
)
|
||
|
||
// XiaohongshuService 小红书业务服务
|
||
type XiaohongshuService struct{}
|
||
|
||
// NewXiaohongshuService 创建小红书服务实例
|
||
func NewXiaohongshuService() *XiaohongshuService {
|
||
return &XiaohongshuService{}
|
||
}
|
||
|
||
// PublishRequest 发布请求
|
||
type PublishRequest struct {
|
||
Title string `json:"title" binding:"required"`
|
||
Content string `json:"content" binding:"required"`
|
||
Images []string `json:"images" binding:"required,min=1"`
|
||
}
|
||
|
||
// LoginStatusResponse 登录状态响应
|
||
type LoginStatusResponse struct {
|
||
IsLoggedIn bool `json:"is_logged_in"`
|
||
Username string `json:"username,omitempty"`
|
||
}
|
||
|
||
// PublishResponse 发布响应
|
||
type PublishResponse struct {
|
||
Title string `json:"title"`
|
||
Content string `json:"content"`
|
||
Images int `json:"images"`
|
||
Status string `json:"status"`
|
||
PostID string `json:"post_id,omitempty"`
|
||
}
|
||
|
||
// FeedsListResponse Feeds列表响应
|
||
type FeedsListResponse struct {
|
||
Feeds []xiaohongshu.Feed `json:"feeds"`
|
||
Count int `json:"count"`
|
||
}
|
||
|
||
// CheckLoginStatus 检查登录状态
|
||
func (s *XiaohongshuService) CheckLoginStatus(ctx context.Context) (*LoginStatusResponse, error) {
|
||
b := browser.NewBrowser(configs.IsHeadless())
|
||
defer b.Close()
|
||
|
||
page := b.NewPage()
|
||
defer page.Close()
|
||
|
||
loginAction := xiaohongshu.NewLogin(page)
|
||
|
||
isLoggedIn, err := loginAction.CheckLoginStatus(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
response := &LoginStatusResponse{
|
||
IsLoggedIn: isLoggedIn,
|
||
Username: configs.Username,
|
||
}
|
||
|
||
return response, nil
|
||
}
|
||
|
||
// PublishContent 发布内容
|
||
func (s *XiaohongshuService) PublishContent(ctx context.Context, req *PublishRequest) (*PublishResponse, error) {
|
||
// 处理图片:下载URL图片或使用本地路径
|
||
imagePaths, err := s.processImages(req.Images)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 构建发布内容
|
||
content := xiaohongshu.PublishImageContent{
|
||
Title: req.Title,
|
||
Content: req.Content,
|
||
ImagePaths: imagePaths,
|
||
}
|
||
|
||
// 执行发布
|
||
if err := s.publishContent(ctx, content); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
response := &PublishResponse{
|
||
Title: req.Title,
|
||
Content: req.Content,
|
||
Images: len(imagePaths),
|
||
Status: "发布完成",
|
||
}
|
||
|
||
return response, nil
|
||
}
|
||
|
||
// processImages 处理图片列表,支持URL下载和本地路径
|
||
func (s *XiaohongshuService) processImages(images []string) ([]string, error) {
|
||
processor := downloader.NewImageProcessor()
|
||
return processor.ProcessImages(images)
|
||
}
|
||
|
||
// publishContent 执行内容发布
|
||
func (s *XiaohongshuService) publishContent(ctx context.Context, content xiaohongshu.PublishImageContent) error {
|
||
b := browser.NewBrowser(configs.IsHeadless())
|
||
defer b.Close()
|
||
|
||
page := b.NewPage()
|
||
defer page.Close()
|
||
|
||
action, err := xiaohongshu.NewPublishImageAction(page)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 执行发布
|
||
return action.Publish(ctx, content)
|
||
}
|
||
|
||
// ListFeeds 获取Feeds列表
|
||
func (s *XiaohongshuService) ListFeeds(ctx context.Context) (*FeedsListResponse, error) {
|
||
b := browser.NewBrowser(configs.IsHeadless())
|
||
defer b.Close()
|
||
|
||
page := b.NewPage()
|
||
defer page.Close()
|
||
|
||
// 创建 Feeds 列表 action
|
||
action := xiaohongshu.NewFeedsListAction(page)
|
||
|
||
// 获取 Feeds 列表
|
||
feeds, err := action.GetFeedsList(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
response := &FeedsListResponse{
|
||
Feeds: feeds,
|
||
Count: len(feeds),
|
||
}
|
||
|
||
return response, nil
|
||
}
|
||
|
||
func (s *XiaohongshuService) SearchFeeds(ctx context.Context, keyword string) (*FeedsListResponse, error) {
|
||
b := browser.NewBrowser(configs.IsHeadless())
|
||
defer b.Close()
|
||
|
||
page := b.NewPage()
|
||
defer page.Close()
|
||
|
||
action := xiaohongshu.NewSearchAction(page)
|
||
|
||
feeds, err := action.Search(ctx, keyword)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
response := &FeedsListResponse{
|
||
Feeds: feeds,
|
||
Count: len(feeds),
|
||
}
|
||
|
||
return response, nil
|
||
}
|