66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Title / caption candidates for Shorts & TikTok uploads."""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
|
|
def _fill(template: str, *, anchor: str = "", url: str = "", room: str = "") -> str:
|
|
text = str(template or "")
|
|
reps = {
|
|
"{anchor}": anchor or "直播",
|
|
"{url}": url or "",
|
|
"{room}": room or "",
|
|
"{name}": anchor or "直播",
|
|
}
|
|
for k, v in reps.items():
|
|
text = text.replace(k, v)
|
|
return re.sub(r"\s+", " ", text).strip()
|
|
|
|
|
|
def title_candidates(
|
|
*,
|
|
platform: str = "shorts",
|
|
anchor: str = "",
|
|
url: str = "",
|
|
room: str = "",
|
|
template: str = "",
|
|
extra_tags: list[str] | None = None,
|
|
) -> list[dict[str, str]]:
|
|
"""Return up to 3 editable candidates. No external AI required."""
|
|
plat = str(platform or "shorts").lower()
|
|
anchor = str(anchor or "").strip() or "直播"
|
|
url = str(url or "").strip()
|
|
base = _fill(template, anchor=anchor, url=url, room=room) if template else ""
|
|
tags = [t for t in (extra_tags or []) if str(t).strip()]
|
|
out: list[dict[str, str]] = []
|
|
|
|
def add(kind: str, title: str) -> None:
|
|
t = re.sub(r"\s+", " ", str(title or "").strip())
|
|
if not t:
|
|
return
|
|
if any(x["title"] == t for x in out):
|
|
return
|
|
out.append({"kind": kind, "title": t[:2200]})
|
|
|
|
if base:
|
|
add("template", base)
|
|
|
|
if plat == "tiktok":
|
|
add("plain", f"{anchor} 现场瞬间")
|
|
add("hook", f"跟着 {anchor} 看这一段 #fyp")
|
|
add("tag", f"{anchor} #streetfood #viral" if not tags else f"{anchor} " + " ".join(f"#{t}" for t in tags[:4]))
|
|
else:
|
|
add("plain", f"{anchor} 直播瞬间 #Shorts")
|
|
add("hook", f"别划走!{anchor} 这段太好看 #Shorts")
|
|
add("tag", f"{anchor} #Shorts #直播" + ("" if not url else f"\n{url}"))
|
|
|
|
return out[:3]
|
|
|
|
|
|
def pick_default_title(candidates: list[dict[str, str]], *, fallback: str = "") -> str:
|
|
if candidates:
|
|
return str(candidates[0].get("title") or fallback or "").strip()
|
|
return str(fallback or "").strip()
|