feat: 添加定时发布功能 (#377)
- publish_content 和 publish_with_video 支持 schedule_at 参数 - 支持 ISO8601 格式时间,范围为 1 小时至 14 天 - 优化页面导航等待策略,提升发布稳定性 Co-authored-by: tanjun <tanjun@tanjundeMac-mini.local> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,10 +17,11 @@ import (
|
||||
|
||||
// PublishImageContent 发布图文内容
|
||||
type PublishImageContent struct {
|
||||
Title string
|
||||
Content string
|
||||
Tags []string
|
||||
ImagePaths []string
|
||||
Title string
|
||||
Content string
|
||||
Tags []string
|
||||
ImagePaths []string
|
||||
ScheduleTime *time.Time // 定时发布时间,nil 表示立即发布
|
||||
}
|
||||
|
||||
type PublishAction struct {
|
||||
@@ -35,7 +36,21 @@ func NewPublishImageAction(page *rod.Page) (*PublishAction, error) {
|
||||
|
||||
pp := page.Timeout(300 * time.Second)
|
||||
|
||||
pp.MustNavigate(urlOfPublic).MustWaitIdle().MustWaitDOMStable()
|
||||
// 使用更稳健的导航和等待策略
|
||||
if err := pp.Navigate(urlOfPublic); err != nil {
|
||||
return nil, errors.Wrap(err, "导航到发布页面失败")
|
||||
}
|
||||
|
||||
// 等待页面加载,使用 WaitLoad 代替 WaitIdle(更宽松)
|
||||
if err := pp.WaitLoad(); err != nil {
|
||||
logrus.Warnf("等待页面加载出现问题: %v,继续尝试", err)
|
||||
}
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// 等待页面稳定
|
||||
if err := pp.WaitDOMStable(time.Second, 0.1); err != nil {
|
||||
logrus.Warnf("等待 DOM 稳定出现问题: %v,继续尝试", err)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
if err := mustClickPublishTab(pp, "上传图文"); err != nil {
|
||||
@@ -67,9 +82,9 @@ func (p *PublishAction) Publish(ctx context.Context, content PublishImageContent
|
||||
tags = tags[:10]
|
||||
}
|
||||
|
||||
logrus.Infof("发布内容: title=%s, images=%v, tags=%v", content.Title, len(content.ImagePaths), tags)
|
||||
logrus.Infof("发布内容: title=%s, images=%v, tags=%v, schedule=%v", content.Title, len(content.ImagePaths), tags, content.ScheduleTime)
|
||||
|
||||
if err := submitPublish(page, content.Title, content.Content, tags); err != nil {
|
||||
if err := submitPublish(page, content.Title, content.Content, tags, content.ScheduleTime); err != nil {
|
||||
return errors.Wrap(err, "小红书发布失败")
|
||||
}
|
||||
|
||||
@@ -239,7 +254,7 @@ func waitForUploadComplete(page *rod.Page, expectedCount int) error {
|
||||
return errors.New("上传超时,请检查网络连接和图片大小")
|
||||
}
|
||||
|
||||
func submitPublish(page *rod.Page, title, content string, tags []string) error {
|
||||
func submitPublish(page *rod.Page, title, content string, tags []string, scheduleTime *time.Time) error {
|
||||
|
||||
titleElem := page.MustElement("div.d-input input")
|
||||
titleElem.MustInput(title)
|
||||
@@ -270,6 +285,14 @@ func submitPublish(page *rod.Page, title, content string, tags []string) error {
|
||||
}
|
||||
slog.Info("检查正文长度:通过")
|
||||
|
||||
// 处理定时发布
|
||||
if scheduleTime != nil {
|
||||
if err := setSchedulePublish(page, *scheduleTime); err != nil {
|
||||
return errors.Wrap(err, "设置定时发布失败")
|
||||
}
|
||||
slog.Info("定时发布设置完成", "schedule_time", scheduleTime.Format("2006-01-02 15:04"))
|
||||
}
|
||||
|
||||
submitButton := page.MustElement("div.submit div.d-button-content")
|
||||
submitButton.MustClick()
|
||||
|
||||
@@ -499,3 +522,132 @@ func isElementVisible(elem *rod.Element) bool {
|
||||
|
||||
return visible
|
||||
}
|
||||
|
||||
// setSchedulePublish 设置定时发布时间
|
||||
func setSchedulePublish(page *rod.Page, t time.Time) error {
|
||||
// 1. 点击"定时发布" radio button
|
||||
if err := clickScheduleRadio(page); err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
// 2. 点击时间选择器打开面板
|
||||
if err := clickDateTimePicker(page); err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
// 3. 设置日期和时间
|
||||
if err := setDateTime(page, t); err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
// 4. 点击确定按钮
|
||||
if err := clickConfirmButton(page); err != nil {
|
||||
return err
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// clickScheduleRadio 点击定时发布 radio
|
||||
func clickScheduleRadio(page *rod.Page) error {
|
||||
labels, err := page.Elements("span.el-radio__label")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "查找 radio label 失败")
|
||||
}
|
||||
|
||||
for _, label := range labels {
|
||||
text, err := label.Text()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(text) == "定时发布" {
|
||||
if err := label.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
||||
return errors.Wrap(err, "点击定时发布按钮失败")
|
||||
}
|
||||
slog.Info("已点击定时发布按钮")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return errors.New("未找到定时发布按钮")
|
||||
}
|
||||
|
||||
// clickDateTimePicker 点击时间选择器
|
||||
func clickDateTimePicker(page *rod.Page) error {
|
||||
// 查找日期时间选择器输入框
|
||||
picker, err := page.Element("input.el-input__inner[placeholder='选择日期和时间']")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "查找时间选择器失败")
|
||||
}
|
||||
|
||||
if err := picker.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
||||
return errors.Wrap(err, "点击时间选择器失败")
|
||||
}
|
||||
slog.Info("已点击时间选择器")
|
||||
return nil
|
||||
}
|
||||
|
||||
// setDateTime 设置日期和时间
|
||||
func setDateTime(page *rod.Page, t time.Time) error {
|
||||
dateStr := t.Format("2006-01-02")
|
||||
timeStr := t.Format("15:04")
|
||||
|
||||
// 设置日期
|
||||
dateInput, err := page.Element("input.el-input__inner[placeholder='选择日期']")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "查找日期输入框失败")
|
||||
}
|
||||
if err := dateInput.SelectAllText(); err != nil {
|
||||
return errors.Wrap(err, "选择日期文本失败")
|
||||
}
|
||||
if err := dateInput.Input(dateStr); err != nil {
|
||||
return errors.Wrap(err, "输入日期失败")
|
||||
}
|
||||
slog.Info("已设置日期", "date", dateStr)
|
||||
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
// 设置时间
|
||||
timeInput, err := page.Element("input.el-input__inner[placeholder='选择时间']")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "查找时间输入框失败")
|
||||
}
|
||||
if err := timeInput.SelectAllText(); err != nil {
|
||||
return errors.Wrap(err, "选择时间文本失败")
|
||||
}
|
||||
if err := timeInput.Input(timeStr); err != nil {
|
||||
return errors.Wrap(err, "输入时间失败")
|
||||
}
|
||||
slog.Info("已设置时间", "time", timeStr)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// clickConfirmButton 点击确定按钮
|
||||
func clickConfirmButton(page *rod.Page) error {
|
||||
// 查找日期选择器弹窗中的确定按钮
|
||||
buttons, err := page.Elements("button.el-picker-panel__link-btn")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "查找确定按钮失败")
|
||||
}
|
||||
|
||||
for _, btn := range buttons {
|
||||
text, err := btn.Text()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(text) == "确定" {
|
||||
if err := btn.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
||||
return errors.Wrap(err, "点击确定按钮失败")
|
||||
}
|
||||
slog.Info("已点击确定按钮")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return errors.New("未找到确定按钮")
|
||||
}
|
||||
|
||||
@@ -14,10 +14,11 @@ import (
|
||||
|
||||
// PublishVideoContent 发布视频内容
|
||||
type PublishVideoContent struct {
|
||||
Title string
|
||||
Content string
|
||||
Tags []string
|
||||
VideoPath string
|
||||
Title string
|
||||
Content string
|
||||
Tags []string
|
||||
VideoPath string
|
||||
ScheduleTime *time.Time // 定时发布时间,nil 表示立即发布
|
||||
}
|
||||
|
||||
// NewPublishVideoAction 进入发布页并切换到“上传视频”
|
||||
@@ -48,7 +49,7 @@ func (p *PublishAction) PublishVideo(ctx context.Context, content PublishVideoCo
|
||||
return errors.Wrap(err, "小红书上传视频失败")
|
||||
}
|
||||
|
||||
if err := submitPublishVideo(page, content.Title, content.Content, content.Tags); err != nil {
|
||||
if err := submitPublishVideo(page, content.Title, content.Content, content.Tags, content.ScheduleTime); err != nil {
|
||||
return errors.Wrap(err, "小红书发布失败")
|
||||
}
|
||||
return nil
|
||||
@@ -116,7 +117,7 @@ func waitForPublishButtonClickable(page *rod.Page) (*rod.Element, error) {
|
||||
}
|
||||
|
||||
// submitPublishVideo 填写标题、正文、标签并点击发布(等待按钮可点击后再提交)
|
||||
func submitPublishVideo(page *rod.Page, title, content string, tags []string) error {
|
||||
func submitPublishVideo(page *rod.Page, title, content string, tags []string, scheduleTime *time.Time) error {
|
||||
// 标题
|
||||
titleElem := page.MustElement("div.d-input input")
|
||||
titleElem.MustInput(title)
|
||||
@@ -132,6 +133,14 @@ func submitPublishVideo(page *rod.Page, title, content string, tags []string) er
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// 处理定时发布
|
||||
if scheduleTime != nil {
|
||||
if err := setSchedulePublish(page, *scheduleTime); err != nil {
|
||||
return errors.Wrap(err, "设置定时发布失败")
|
||||
}
|
||||
slog.Info("定时发布设置完成", "schedule_time", scheduleTime.Format("2006-01-02 15:04"))
|
||||
}
|
||||
|
||||
// 等待发布按钮可点击
|
||||
btn, err := waitForPublishButtonClickable(page)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user