Commit 3fe25414 authored by 郭阔's avatar 郭阔

Check creator editor readiness before publishing

parent 64170d5f
package xiaohongshu
import (
"net/url"
"time"
"github.com/go-rod/rod"
"github.com/pkg/errors"
"github.com/xpzouying/xiaohongshu-mcp/humanize"
)
const (
urlOfCreatorHome = `https://creator.xiaohongshu.com/?source=official`
const creatorSessionTimeout = 15 * time.Second
creatorSessionCookie = "galaxy_creator_session_id"
creatorSessionTimeout = 15 * time.Second
)
// ensureCreatorSession 浏览器里没有创作者中心会话时,先打开创作者中心首页,等会话建立
// ensureCreatorSession verifies the authenticated editor instead of relying on a
// cookie whose name and presence are controlled by the platform.
func ensureCreatorSession(page *rod.Page) error {
if hasCreatorSession(page) {
return nil
}
if err := page.Navigate(urlOfCreatorHome); err != nil {
return errors.Wrap(err, "打开创作者中心失败")
if err := page.Navigate(urlOfPublic); err != nil {
return errors.Wrap(err, "打开创作者中心编辑器失败")
}
deadline := time.Now().Add(creatorSessionTimeout)
var lastPath string
for time.Now().Before(deadline) {
time.Sleep(500 * time.Millisecond)
if hasCreatorSession(page) {
humanize.Delay(page.GetContext(), humanize.AfterNavigate)
return nil
info, err := page.Info()
if err != nil {
return errors.Wrap(err, "读取创作者中心页面地址失败")
}
current, err := url.Parse(info.URL)
if err != nil {
return errors.New("无法解析创作者中心页面地址")
}
return errors.New("创作者中心登录失效,请重新扫码登录")
}
func hasCreatorSession(page *rod.Page) bool {
cks, err := page.Browser().GetCookies()
lastPath = current.Host + current.Path
if current.Scheme == "https" && current.Host == "creator.xiaohongshu.com" &&
current.Path == "/publish/publish" {
found, _, err := page.Has("div.creator-tab, .upload-input")
if err != nil {
return false
return errors.Wrap(err, "检查创作者中心编辑器失败")
}
if found {
return nil
}
for _, c := range cks {
if c.Name == creatorSessionCookie {
return true
}
time.Sleep(500 * time.Millisecond)
}
return false
return errors.Errorf("创作者中心编辑器未就绪(当前页面:%s),请检查页面是否要求登录", lastPath)
}
......@@ -54,10 +54,6 @@ func NewPublishImageAction(page *rod.Page) (*PublishAction, error) {
return nil, err
}
if err := pp.Navigate(urlOfPublic); err != nil {
return nil, errors.Wrap(err, "导航到发布页面失败")
}
if err := pp.WaitLoad(); err != nil {
logrus.Warnf("等待页面加载出现问题: %v,继续尝试", err)
}
......
......@@ -32,10 +32,6 @@ func NewPublishVideoAction(page *rod.Page) (*PublishAction, error) {
return nil, err
}
if err := pp.Navigate(urlOfPublic); err != nil {
return nil, errors.Wrap(err, "导航到发布页面失败")
}
// 使用 WaitLoad 代替 WaitIdle(更宽松)
if err := pp.WaitLoad(); err != nil {
logrus.Warnf("等待页面加载出现问题: %v,继续尝试", err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment