fix(content-marketing): 笔记标题入库前剥除话题链接段与裸 URL
登记人常把抖音「复制分享文案」整段粘进发布笔记标题列,含 [话题](url)
链接段(个别为 {url} 花括号包裹)和裸 URL。新增 clean_note_title:
剥链接后非空则采用;整段皆链接时取方括号内话题文本去重拼接;
仍为空则保留原文兜底,避免标题落空。
This commit is contained in:
1 parent
47ed6ec3f6
commit
3cf7048140
1 file changed
+31
-1
@@ -219,6 +219,36 @@ def parse_wan_count(text: str) -> int | None:
|
|||||||
return 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:
|
def normalize_publish_time(cell: Any) -> str | None:
|
||||||
text = cell_text(cell)
|
text = cell_text(cell)
|
||||||
if not text:
|
if not text:
|
||||||
@@ -340,7 +370,7 @@ def parse_records(
|
|||||||
record_id=record_id,
|
record_id=record_id,
|
||||||
platform=resolve_platform(value(record, "platform"), url),
|
platform=resolve_platform(value(record, "platform"), url),
|
||||||
creator_name=creator_name,
|
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")),
|
publish_time=normalize_publish_time(value(record, "publish_time")),
|
||||||
url=url,
|
url=url,
|
||||||
creator_attrs=creator_attrs,
|
creator_attrs=creator_attrs,
|
||||||
|
|||||||
Reference in new issue
Block a user