# -*- coding: utf-8 -*- """One-shot: strip WeChat OA QR / 引流文案 from tt book data (Douyin audit).""" from __future__ import annotations import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OA_QR_IMG_RE = re.compile(r"!\[[^\]]*\]\([^)]*qrcode_for_gh[^)]*\)\s*", re.I) # real newlines OR escaped \\n inside TS/JSON string literals OA_LINE_RE = re.compile( r"(?:^|(?<=\\n)|(?<=\n))[>\s]*公众号\s*[::][^\n\\]*(?:\\n|\n)?", ) OA_PLACEHOLDER_RE = re.compile( r"(?:^|(?<=\\n)|(?<=\n))[>\s]*(?:)?(?:\\n|\n)?" ) OA_INLINE_RE = re.compile( r"关注公众号[^·\\\"\\\n]*|完整大图见公众号「[^」]*」|完整图见公众号「[^」]*」|看本公众号`?同名账号`?" ) def sanitize(s: str) -> str: s = OA_QR_IMG_RE.sub("", s) s = OA_LINE_RE.sub("", s) s = OA_PLACEHOLDER_RE.sub("", s) s = OA_INLINE_RE.sub("", s) s = s.replace("关注公众号异度世界 · 私信加群", "数字游民 · 离线精选") s = s.replace('"tagline": "· 私信加群"', '"tagline": "数字游民 · 离线精选"') s = s.replace('"tagline": " · 私信加群"', '"tagline": "数字游民 · 离线精选"') s = re.sub(r"(?:\\n){3,}", r"\\n\\n", s) s = re.sub(r"\n{3,}", "\n\n", s) return s def main() -> None: for rel in ("tt/src/data/nomadCodeBook.ts", "tt/src/data/coverBook.ts"): p = ROOT / rel raw = p.read_text(encoding="utf-8") out = sanitize(raw) p.write_text(out, encoding="utf-8") print( rel, "changed" if out != raw else "unchanged", "qrcode=", "qrcode_for_gh" in out, "oa_line=", ("公众号: 异度" in out or "公众号:异度" in out), "公众号 in content=", "公众号" in out, "tagline_bad=", "私信加群" in out and "数字游民" not in out.split("tagline")[1][:40] if "tagline" in out else False, ) if __name__ == "__main__": main()