feat: implement xiaohongshu automation with MCP server
Complete implementation of xiaohongshu (Little Red Book) automation system: ### Core Features: - **QR Code Login**: Automated login with cookie persistence - **Content Publishing**: Post text, images with AI-powered descriptions - **Browser Management**: Headless Chrome automation via go-rod - **Cookie Persistence**: Session management for login state - **MCP Server**: Model Context Protocol integration for Claude ### Technical Components: - go-rod browser automation with stealth mode - MCP server for Claude Code integration - Cookie-based session management - Robust error handling and logging - Cross-platform compatibility ### API Endpoints: - Login status checking and QR code authentication - Content publishing with image upload support - Navigation and page interaction utilities This provides a complete foundation for xiaohongshu automation with proper session management and MCP integration. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
57
xiaohongshu/login.go
Normal file
57
xiaohongshu/login.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package xiaohongshu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-rod/rod"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type LoginAction struct {
|
||||
page *rod.Page
|
||||
}
|
||||
|
||||
func NewLogin(page *rod.Page) *LoginAction {
|
||||
return &LoginAction{page: page}
|
||||
}
|
||||
|
||||
func (a *LoginAction) CheckLoginStatus(ctx context.Context) (bool, error) {
|
||||
pp := a.page.Context(ctx)
|
||||
pp.MustNavigate("https://www.xiaohongshu.com/explore").MustWaitLoad()
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
exists, _, err := pp.Has(`.main-container .user .link-wrapper .channel`)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "check login status failed")
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return false, errors.Wrap(err, "login status element not found")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *LoginAction) Login(ctx context.Context) error {
|
||||
pp := a.page.Context(ctx)
|
||||
|
||||
// 导航到小红书首页,这会触发二维码弹窗
|
||||
pp.MustNavigate("https://www.xiaohongshu.com/explore").MustWaitLoad()
|
||||
|
||||
// 等待一小段时间让页面完全加载
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// 检查是否已经登录
|
||||
if exists, _, _ := pp.Has(".main-container .user .link-wrapper .channel"); exists {
|
||||
// 已经登录,直接返回
|
||||
return nil
|
||||
}
|
||||
|
||||
// 等待扫码成功提示或者登录完成
|
||||
// 这里我们等待登录成功的元素出现,这样更简单可靠
|
||||
pp.MustElement(".main-container .user .link-wrapper .channel")
|
||||
|
||||
return nil
|
||||
}
|
||||
25
xiaohongshu/navigate.go
Normal file
25
xiaohongshu/navigate.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package xiaohongshu
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-rod/rod"
|
||||
)
|
||||
|
||||
type NavigateAction struct {
|
||||
page *rod.Page
|
||||
}
|
||||
|
||||
func NewNavigate(page *rod.Page) *NavigateAction {
|
||||
return &NavigateAction{page: page}
|
||||
}
|
||||
|
||||
func (n *NavigateAction) ToExplorePage(ctx context.Context) error {
|
||||
page := n.page.Context(ctx)
|
||||
|
||||
page.MustNavigate("https://www.xiaohongshu.com/explore").
|
||||
MustWaitLoad().
|
||||
MustElement(`div#app`)
|
||||
|
||||
return nil
|
||||
}
|
||||
116
xiaohongshu/publish.go
Normal file
116
xiaohongshu/publish.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package xiaohongshu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/go-rod/rod"
|
||||
"github.com/go-rod/rod/lib/proto"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// PublishImageContent 发布图文内容
|
||||
type PublishImageContent struct {
|
||||
Title string
|
||||
Content string
|
||||
ImagePaths []string
|
||||
}
|
||||
|
||||
type PublishAction struct {
|
||||
page *rod.Page
|
||||
}
|
||||
|
||||
const (
|
||||
urlOfPublic = `https://creator.xiaohongshu.com/publish/publish?source=official`
|
||||
)
|
||||
|
||||
func NewPublishImageAction(page *rod.Page) (*PublishAction, error) {
|
||||
|
||||
pp := page.Timeout(60 * time.Second)
|
||||
|
||||
pp.MustNavigate(urlOfPublic)
|
||||
|
||||
pp.MustElement(`div.upload-content`).MustWaitVisible()
|
||||
slog.Info("wait for upload-content visible success")
|
||||
|
||||
// 等待一段时间确保页面完全加载
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
createElems := pp.MustElements("div.creator-tab")
|
||||
slog.Info("foundcreator-tab elements", "count", len(createElems))
|
||||
for _, elem := range createElems {
|
||||
text, err := elem.Text()
|
||||
if err != nil {
|
||||
slog.Error("获取元素文本失败", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if text == "上传图文" {
|
||||
if err := elem.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
||||
slog.Error("点击元素失败", "error", err)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
return &PublishAction{
|
||||
page: pp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *PublishAction) Publish(ctx context.Context, content PublishImageContent) error {
|
||||
if len(content.ImagePaths) == 0 {
|
||||
return errors.New("图片不能为空")
|
||||
}
|
||||
|
||||
page := p.page.Context(ctx)
|
||||
|
||||
if err := uploadImages(page, content.ImagePaths); err != nil {
|
||||
return errors.Wrap(err, "小红书上传图片失败")
|
||||
}
|
||||
|
||||
if err := submitPublish(page, content.Title, content.Content); err != nil {
|
||||
return errors.Wrap(err, "小红书发布失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func uploadImages(page *rod.Page, imagesPaths []string) error {
|
||||
pp := page.Timeout(30 * time.Second)
|
||||
|
||||
// 等待上传输入框出现
|
||||
uploadInput := pp.MustElement(".upload-input")
|
||||
|
||||
// 上传多个文件
|
||||
uploadInput.MustSetFiles(imagesPaths...)
|
||||
|
||||
// 等待上传完成
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func submitPublish(page *rod.Page, title, content string) error {
|
||||
|
||||
titleElem := page.MustElement("div.d-input input")
|
||||
titleElem.MustInput(title)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
contentElem := page.MustElement("div.ql-editor")
|
||||
contentElem.MustInput(content)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
submitButton := page.MustElement("div.submit div.d-button-content")
|
||||
submitButton.MustClick()
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
return nil
|
||||
}
|
||||
32
xiaohongshu/publish_test.go
Normal file
32
xiaohongshu/publish_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package xiaohongshu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/xpzouying/xiaohongshu-mcp/browser"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPublish(t *testing.T) {
|
||||
|
||||
t.Skip("SKIP: 测试发布")
|
||||
|
||||
_ = browser.Init(false)
|
||||
defer browser.Close()
|
||||
|
||||
page := browser.NewPage()
|
||||
defer page.Close()
|
||||
|
||||
action, err := NewPublishImageAction(page)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = action.Publish(context.Background(), PublishImageContent{
|
||||
Title: "Claude移动端重大更新!随时随地高效办公",
|
||||
Content: "Claude移动端重大更新!随时随地高效办公",
|
||||
ImagePaths: []string{"1948784311265894447"},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user