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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user