front/xhs-agent-desktop/engine/scripts/title_utils.py
zzh 5a26ae0048 feat: 小红书模块迁移、登录重构与 xhs-agent-desktop 桌面 Agent
- 新增小红书前端模块(启动服务、内容分析、爆款创作、发布记录、技能卡片、拟人设置、AI 助手)

- 新增 xhs-agent-desktop 独立桌面 Agent,对接 OMS 任务调度

- 登录页重构,整合微信登录与 OMS JWT 认证

- 订单、渠道、用户中心等页面与 API 增强

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-30 16:13:13 +08:00

58 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""UTF-16 标题长度计算,对应 Go pkg/xhsutil/title.go。"""
from __future__ import annotations
MAX_TITLE_LENGTH = 20
def truncate_title(s: str, max_length: int = MAX_TITLE_LENGTH) -> str:
"""将标题裁剪到 max_length 以内(逐字符从末尾去除)。
Args:
s: 原始标题。
max_length: 最大允许长度(默认 20)。
Returns:
满足长度要求的标题字符串。
"""
if calc_title_length(s) <= max_length:
return s
while s and calc_title_length(s) > max_length:
s = s[:-1]
return s
def calc_title_length(s: str) -> int:
"""计算小红书标题长度。
规则(同 Go CalcTitleLength):
- 非 ASCII 字符(中文、全角符号、emoji 代码单元等)算 2
- ASCII 字符算 1
- 最终结果向上取整除以 2,上限 MAX_TITLE_LENGTH = 20
Emoji 按 UTF-16 码元计数:
- 基础 emoji(如 ✨ U+2728, BMP)= 1 码元 → 权重 2 → 贡献 1
- SMP emoji(如 💇 U+1F487,surrogate pair)= 2 码元 → 权重 4 → 贡献 2
- ZWJ 序列(如 💇‍♀️)= 5 码元 → 权重 10 → 贡献 5
- 旗帜(如 🇨🇳,2 个 regional indicator)= 4 码元 → 权重 8 → 贡献 4
Examples:
>>> calc_title_length("你好世界")
4
>>> calc_title_length("hello")
3
>>> calc_title_length("OOTD穿搭分享")
6
>>> calc_title_length("💇\u200d♀️")
5
"""
byte_len = 0
encoded = s.encode("utf-16-le")
for i in range(0, len(encoded), 2):
code_unit = int.from_bytes(encoded[i : i + 2], "little")
if code_unit > 127:
byte_len += 2
else:
byte_len += 1
return (byte_len + 1) // 2