diff --git a/src/gyxx_flow/modules/content_marketing/data/tools/sync_notes_master.py b/src/gyxx_flow/modules/content_marketing/data/tools/sync_notes_master.py index 0da03a3..ceac953 100644 --- a/src/gyxx_flow/modules/content_marketing/data/tools/sync_notes_master.py +++ b/src/gyxx_flow/modules/content_marketing/data/tools/sync_notes_master.py @@ -219,6 +219,36 @@ def parse_wan_count(text: str) -> int | None: return None +# 抖音"复制分享文案"格式:标题 + 话题链接段 [话题](url)(个别登记写成了 {url} 花括号包裹), +# 或裸 URL。登记人常把整段分享文案粘进「发布笔记标题」列,入库前剥掉链接只留标题文本。 +_TITLE_LINK_RE = re.compile( + r"\[[^\]]*\]\([^)]*\)" # [text](url) 圆括号链接 + r"|\[[^\]]*\]\{[^}]*\}" # [text]{url} 花括号链接 + r"|\{https?://[^}]*\}" # {url} 裸花括号链接 + r"|https?://[^\s))\]]+" # 裸 URL +) + + +def clean_note_title(raw: str) -> str: + """剥掉标题里混入的话题链接段与裸 URL,只留标题文本。 + + 兜底顺序:剥链接后非空 → 用它;整段都是链接(标题只剩话题/搜索词)→ + 取方括号内的文本去重拼接;再不行 → 保留原文,避免落空。 + """ + text = raw or "" + cleaned = _TITLE_LINK_RE.sub("", text) + cleaned = cleaned.replace("%20", " ") # 链接段之间残留的 URL 编码空格 + cleaned = re.sub(r"\s{2,}", " ", cleaned).strip() + if cleaned: + return cleaned + inner = [t.strip() for t in re.findall(r"\[([^\]]*)\]", text)] + seen: list[str] = [] + for item in inner: + if item and item not in seen: + seen.append(item) + return " ".join(seen) if seen else text.strip() + + def normalize_publish_time(cell: Any) -> str | None: text = cell_text(cell) if not text: @@ -340,7 +370,7 @@ def parse_records( record_id=record_id, platform=resolve_platform(value(record, "platform"), url), creator_name=creator_name, - title=cell_text(value(record, "note_title"))[:512], + title=clean_note_title(cell_text(value(record, "note_title")))[:512], publish_time=normalize_publish_time(value(record, "publish_time")), url=url, creator_attrs=creator_attrs,