feat: 添加标题长度校验防止小红书API拒绝 (#53)

- 新增go-runewidth依赖用于计算字符显示宽度
- 在PublishContent方法中添加标题长度验证
- 标题超过40个单位时返回"标题长度超过限制"错误
- 中文字符计2个单位,ASCII字符计1个单位
- 确保标题符合小红书API要求:最多20个中文字符或40个ASCII字符

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
zy
2025-09-10 01:07:09 +08:00
committed by GitHub
parent f21183fa48
commit adabc4ba57
3 changed files with 15 additions and 0 deletions

View File

@@ -2,7 +2,9 @@ package main
import (
"context"
"fmt"
"github.com/mattn/go-runewidth"
"github.com/xpzouying/xiaohongshu-mcp/browser"
"github.com/xpzouying/xiaohongshu-mcp/configs"
"github.com/xpzouying/xiaohongshu-mcp/pkg/downloader"
@@ -70,6 +72,13 @@ func (s *XiaohongshuService) CheckLoginStatus(ctx context.Context) (*LoginStatus
// PublishContent 发布内容
func (s *XiaohongshuService) PublishContent(ctx context.Context, req *PublishRequest) (*PublishResponse, error) {
// 验证标题长度
// 小红书限制最大40个单位长度
// 中文/日文/韩文占2个单位英文/数字占1个单位
if titleWidth := runewidth.StringWidth(req.Title); titleWidth > 40 {
return nil, fmt.Errorf("标题长度超过限制")
}
// 处理图片下载URL图片或使用本地路径
imagePaths, err := s.processImages(req.Images)
if err != nil {