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:
75
cmd/login/main.go
Normal file
75
cmd/login/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-rod/rod"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/xpzouying/xiaohongshu-mcp/browser"
|
||||
"github.com/xpzouying/xiaohongshu-mcp/cookies"
|
||||
"github.com/xpzouying/xiaohongshu-mcp/xiaohongshu"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
headlessModel := false
|
||||
if err := browser.Init(headlessModel); err != nil {
|
||||
logrus.Fatalf("failed to init browser: %v", err)
|
||||
}
|
||||
defer browser.Close()
|
||||
|
||||
page := browser.NewPage()
|
||||
defer page.Close()
|
||||
|
||||
action := xiaohongshu.NewLogin(page)
|
||||
|
||||
status, err := action.CheckLoginStatus(context.Background())
|
||||
if err != nil {
|
||||
logrus.Fatalf("failed to check login status: %v", err)
|
||||
}
|
||||
|
||||
logrus.Infof("当前登录状态: %v", status)
|
||||
|
||||
if status {
|
||||
return
|
||||
}
|
||||
|
||||
// 开始登录流程
|
||||
logrus.Info("开始登录流程...")
|
||||
if err = action.Login(context.Background()); err != nil {
|
||||
logrus.Fatalf("登录失败: %v", err)
|
||||
} else {
|
||||
if err := saveCookies(page); err != nil {
|
||||
logrus.Fatalf("failed to save cookies: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 再次检查登录状态确认成功
|
||||
status, err = action.CheckLoginStatus(context.Background())
|
||||
if err != nil {
|
||||
logrus.Fatalf("failed to check login status after login: %v", err)
|
||||
}
|
||||
|
||||
if status {
|
||||
logrus.Info("登录成功!")
|
||||
} else {
|
||||
logrus.Error("登录流程完成但仍未登录")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func saveCookies(page *rod.Page) error {
|
||||
cks, err := page.Browser().GetCookies()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.Marshal(cks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cookieLoader := cookies.NewLoadCookie(cookies.GetCookiesFilePath())
|
||||
return cookieLoader.SaveCookies(data)
|
||||
}
|
||||
Reference in New Issue
Block a user