fix(test1): 放宽短中文文案校验,短主播名自动补后缀

「小夏妹妹」等短名不再被误判正文过短;默认模板改为带「直播精彩瞬间」。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
eric 2026-08-15 08:12:58 -05:00
parent ba2ab0400a
commit e0d4a95c0c
6 changed files with 44 additions and 16 deletions

View File

@ -493,7 +493,7 @@ def _seed_defaults(conn: sqlite3.Connection) -> None:
"x_clips_upload_min_gap_sec": 600,
"x_clips_delete_clip_after_post": True,
"x_clips_daily_max": 20,
"x_clips_caption_template": "{anchor}",
"x_clips_caption_template": "{anchor} 直播精彩瞬间",
"tiktok_auto_retry_failed": False,
"tiktok_auto_retry_max_attempts": 3,
"tiktok_auto_retry_interval_sec": 300,

View File

@ -58,7 +58,7 @@ def _clip_sec(cfg: dict[str, Any], room: dict[str, Any] | None = None) -> int:
def build_caption(job: dict[str, Any], cfg: dict[str, Any] | None = None) -> str:
settings = cfg or db.get_settings_map()
tpl = str(settings.get("x_clips_caption_template") or "{anchor}").strip() or "{anchor}"
tpl = str(settings.get("x_clips_caption_template") or "{anchor} 直播精彩瞬间").strip() or "{anchor} 直播精彩瞬间"
anchor = str(job.get("anchor_name") or "").strip() or "Live"
room = str(job.get("room_id") or "").strip()
text = (
@ -66,7 +66,12 @@ def build_caption(job: dict[str, Any], cfg: dict[str, Any] | None = None) -> str
.replace("{room}", room)
.replace("{title}", anchor)
).strip()
# X hard limit ~280; leave headroom for media
bare = "".join(ch for ch in text if not ch.isspace())
# Pad very short captions (短主播名会触发旧插件 length>=12 校验)
if len(bare) < 8:
suffix = "直播精彩瞬间"
if suffix not in text:
text = f"{text} {suffix}".strip()
return text[:270]
@ -246,7 +251,7 @@ class XClipsPipeline:
"x_clips_trigger": cfg.get("x_clips_trigger") or "tagged",
"x_clips_tag": cfg.get("x_clips_tag") or "x",
"x_clips_daily_max": x_clips_db.x_clips_daily_max(cfg),
"x_clips_caption_template": cfg.get("x_clips_caption_template") or "{anchor}",
"x_clips_caption_template": cfg.get("x_clips_caption_template") or "{anchor} 直播精彩瞬间",
"x_clips_live_sample": bool(cfg.get("x_clips_live_sample", True)),
"x_clips_live_sample_interval_sec": int(cfg.get("x_clips_live_sample_interval_sec") or 180),
"x_clips_live_sample_batch": int(cfg.get("x_clips_live_sample_batch") or 1),
@ -484,6 +489,9 @@ class XClipsPipeline:
jid = str(job.get("id") or "")
cfg = db.get_settings_map()
caption = str(job.get("caption") or "").strip() or build_caption(job, cfg)
bare = "".join(ch for ch in caption if not ch.isspace())
if len(bare) < 8:
caption = build_caption(job, cfg)
media_url = str(job.get("media_url") or "").strip()
media_key = str(job.get("media_key") or "").strip()
clip = str(job.get("clip_path") or "").strip()

View File

@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "dylive Helper",
"version": "2.2.1",
"version": "2.2.2",
"description": "TikTok Cookie + X2 Ops(推送到 dylive /x)",
"permissions": ["cookies", "storage", "tabs", "alarms", "scripting", "clipboardWrite"],
"host_permissions": [

View File

@ -1,6 +1,6 @@
{
"name": "dylive-helper",
"version": "2.2.1",
"version": "2.2.2",
"private": true,
"type": "module",
"scripts": {

View File

@ -1116,6 +1116,26 @@ function captionOnly(s: string): string {
.trim();
}
/** Reject empty / URL-only; allow short CJK names (旧规则 length>=12 会误杀「小夏妹妹」). */
function isCaptionAcceptable(body: string): boolean {
const raw = String(body || "").trim();
if (!raw) return false;
if (/^https?:\/\/\S+$/i.test(raw)) return false;
const token = captionOnly(raw);
if (!token) return false;
const cjk = (token.match(/[\u4e00-\u9fff\u3400-\u4dbf]/g) || []).join("").length;
if (cjk >= 2) return true;
return token.replace(/\s+/g, "").length >= 6;
}
function captionMatchToken(got: string, token: string): boolean {
const t = captionOnly(token);
const g = captionOnly(got);
if (!t || !g) return false;
const needle = t.slice(0, Math.min(10, t.length));
return g.includes(needle);
}
function looksLikePlaceholder(s: string): boolean {
return /^(what('|’)s happening|what is happening|有什么新鲜事|发生什么了|有什麼新鮮事)/i.test(
s.trim(),
@ -1202,7 +1222,7 @@ async function setComposerText(box: HTMLElement, text: string): Promise<boolean>
if (looksLikePlaceholder(got)) got = "";
let cap = captionOnly(got);
const expect = captionOnly(flat).slice(0, 16);
if (expect.length >= 8 && cap.includes(expect.slice(0, 10))) return true;
if (expect.length >= 2 && cap.includes(expect.slice(0, Math.min(10, expect.length)))) return true;
// Fallback once: insertText whole string (still no chunking)
await clearComposer(box);
@ -1221,8 +1241,8 @@ async function setComposerText(box: HTMLElement, text: string): Promise<boolean>
got = composerPlain(box);
if (looksLikePlaceholder(got)) got = "";
cap = captionOnly(got);
if (expect.length >= 8 && cap.includes(expect.slice(0, 10))) return true;
return cap.length >= 12;
if (expect.length >= 2 && cap.includes(expect.slice(0, Math.min(10, expect.length)))) return true;
return isCaptionAcceptable(cap);
}
async function ensureComposerCaption(text: string, token: string): Promise<HTMLElement> {
@ -1243,7 +1263,7 @@ async function ensureComposerCaption(text: string, token: string): Promise<HTMLE
await sleep(250);
continue;
}
if (ok && got.length >= 8 && got.includes(token.slice(0, 10))) {
if (ok && captionMatchToken(got, token) && isCaptionAcceptable(got)) {
return live;
}
await sleep(300);
@ -1272,8 +1292,8 @@ async function postTweet(
}
const token = captionOnly(body);
if (token.length < 12) {
throw new Error("正文过短(X 规范:不能发纯链接)。已中止");
if (!isCaptionAcceptable(body)) {
throw new Error("正文过短或仅含链接/话题(需可读正文)。已中止");
}
let box = findTweetComposeBox();
@ -1303,8 +1323,8 @@ async function postTweet(
let finalCaption = captionOnly(finalRaw);
if (
/^https?:\/\/\S+$/i.test(finalRaw.trim()) ||
finalCaption.length < 12 ||
!finalCaption.includes(token.slice(0, 10))
!isCaptionAcceptable(finalRaw) ||
!captionMatchToken(finalCaption, token)
) {
throw new Error(
`发送前校验失败:正文丢失(「${finalRaw.slice(0, 40)}」)。不会发出纯链接`,

View File

@ -81,7 +81,7 @@ export function ClipsPanel({ toast }: { toast: (msg: string, kind?: "ok" | "err"
const s = status?.settings || {};
setClipSec(Number(s.x_clips_clip_sec || status?.clip_sec || 12));
setDailyMax(Number(s.x_clips_daily_max || status?.daily_max || 20));
setCaptionTpl(String(s.x_clips_caption_template || "{anchor}"));
setCaptionTpl(String(s.x_clips_caption_template || "{anchor} 直播精彩瞬间"));
setTrigger(String(s.x_clips_trigger || "tagged"));
setSampleAny(Boolean(s.x_clips_sample_any_live ?? true));
setAutoPost(Boolean(s.x_clips_auto_post ?? true));
@ -191,7 +191,7 @@ export function ClipsPanel({ toast }: { toast: (msg: string, kind?: "ok" | "err"
</label>
<label className={`${label} sm:col-span-2`}>
文案模板(可用 {"{anchor}"} / {"{room}"})
<input className={field} value={captionTpl} onChange={(e) => setCaptionTpl(e.target.value)} />
<input className={field} value={captionTpl} onChange={(e) => setCaptionTpl(e.target.value)} placeholder="{anchor} 直播精彩瞬间" />
</label>
<label className={label}>
取样范围