diff --git a/backend/app/main.py b/backend/app/main.py index eb0bf90..db89403 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -400,6 +400,13 @@ def _enrich_shorts_job(job: dict[str, Any] | None) -> dict[str, Any] | None: out = dict(job) clip = str(out.get("clip_path") or "").strip() out["clip_ready"] = bool(clip) and Path(clip).is_file() + ch = db.get_shorts_channel(str(out.get("channel_pk") or "")) or {} + if ch: + out["channel_name"] = str(ch.get("name") or out.get("channel_name") or "") + out["oauth_profile_id"] = str(ch.get("oauth_profile_id") or "").strip().upper() + else: + out.setdefault("channel_name", "") + out.setdefault("oauth_profile_id", "") return out diff --git a/backend/app/shorts_schedule.py b/backend/app/shorts_schedule.py index cc4776c..5a68b2c 100644 --- a/backend/app/shorts_schedule.py +++ b/backend/app/shorts_schedule.py @@ -185,6 +185,7 @@ def schedule_board(limit: int = 24) -> dict[str, Any]: "room_id": j.get("room_id"), "channel_pk": j.get("channel_pk"), "channel_name": ch.get("name") or "", + "oauth_profile_id": str(ch.get("oauth_profile_id") or "").strip().upper(), "status": j.get("status"), "clip_ready": clip_ready, "can_upload_now": str(j.get("status") or "") in {"scheduled", "recorded"} @@ -215,10 +216,11 @@ def schedule_board(limit: int = 24) -> dict[str, Any]: "id": cid, "name": c.get("name"), "youtube_channel_id": c.get("youtube_channel_id"), + "oauth_profile_id": str(c.get("oauth_profile_id") or "").strip().upper(), "enabled": bool(c.get("enabled")), + "authorized": bool(str(c.get("refresh_token") or "").strip()), "daily_min": daily_min, "daily_max": daily_max, - "authorized": bool(str(c.get("refresh_token") or "").strip()), "gap": gap, "min_met": min_met, "max_met": used >= daily_max, diff --git a/frontend/src/app/shorts/page.tsx b/frontend/src/app/shorts/page.tsx index 81ac815..a16d403 100644 --- a/frontend/src/app/shorts/page.tsx +++ b/frontend/src/app/shorts/page.tsx @@ -20,6 +20,7 @@ import { Shell } from "@/components/Shell"; import { useConfirm } from "@/components/ConfirmDialog"; import { useToast } from "@/components/Toast"; import { GoogleOauthSetupWizard } from "@/components/GoogleOauthSetupWizard"; +import { OauthClientBadge, normalizeOauthProfileId } from "@/components/OauthClientBadge"; import { ShortsJobDetailModal } from "@/components/ShortsJobDetailModal"; import { ShortsUploadConfirmModal, @@ -67,6 +68,7 @@ type BoardChannel = { id: string; name?: string; youtube_channel_id?: string; + oauth_profile_id?: string; enabled?: boolean; daily_min: number; daily_max: number; @@ -272,19 +274,104 @@ export default function ShortsPage() { for (const c of boardChannels) if (c.name) m[c.id] = c.name; return m; }, [channels, boardChannels]); + const channelProfileMap = useMemo(() => { + const m: Record = {}; + for (const c of channels) m[c.id] = normalizeOauthProfileId(c.oauth_profile_id); + for (const c of boardChannels) { + const pid = normalizeOauthProfileId(c.oauth_profile_id); + if (pid) m[c.id] = pid; + } + return m; + }, [channels, boardChannels]); + + const displayChannels = useMemo(() => { + const base: BoardChannel[] = boardChannels.length + ? boardChannels + : channels.map((c) => ({ + id: c.id, + name: c.name, + youtube_channel_id: c.youtube_channel_id, + oauth_profile_id: c.oauth_profile_id, + enabled: c.enabled, + daily_min: Number(c.daily_min ?? 9), + daily_max: Number(c.daily_max ?? 9), + authorized: Boolean(c.authorized || c.has_refresh_token), + used: 0, + scheduled: 0, + uploaded: 0, + pending: 0, + gap: Number(c.daily_max ?? 9), + min_met: false, + max_met: false, + behind_pace: false, + })); + return base.map((ch) => ({ + ...ch, + oauth_profile_id: + normalizeOauthProfileId(ch.oauth_profile_id) || channelProfileMap[ch.id] || "", + })); + }, [boardChannels, channels, channelProfileMap]); + + const channelGroups = useMemo(() => { + const buckets = new Map(); + for (const ch of displayChannels) { + const key = normalizeOauthProfileId(ch.oauth_profile_id) || "_"; + const arr = buckets.get(key) || []; + arr.push(ch); + buckets.set(key, arr); + } + const order = ["A", "B", "C", "D", "_"]; + const keys = [...order.filter((k) => buckets.has(k)), ...[...buckets.keys()].filter((k) => !order.includes(k))]; + return keys.map((k) => ({ + profileId: k === "_" ? "" : k, + channels: buckets.get(k) || [], + })); + }, [displayChannels]); + + const upcomingGroups = useMemo(() => { + const buckets = new Map(); + for (const u of upcoming) { + const pk = String(u.channel_pk || ""); + const key = + normalizeOauthProfileId(u.oauth_profile_id) || channelProfileMap[pk] || "_"; + const arr = buckets.get(key) || []; + arr.push(u); + buckets.set(key, arr); + } + const order = ["A", "B", "C", "D", "_"]; + const keys = [...order.filter((k) => buckets.has(k)), ...[...buckets.keys()].filter((k) => !order.includes(k))]; + return keys.map((k) => ({ + profileId: k === "_" ? "" : k, + items: buckets.get(k) || [], + })); + }, [upcoming, channelProfileMap]); + + const profileLabelMap = useMemo(() => { + const m: Record = {}; + for (const p of (status?.oauth_profiles || []) as OauthProfile[]) { + m[normalizeOauthProfileId(p.id)] = String(p.label || `谷歌账号 ${p.id}`); + } + for (const c of channels) { + const pid = normalizeOauthProfileId(c.oauth_profile_id); + if (pid && c.oauth_profile_label) m[pid] = c.oauth_profile_label; + } + return m; + }, [status?.oauth_profiles, channels]); const totals = useMemo(() => { - const uploaded = boardChannels.reduce((s, c) => s + (c.uploaded || 0), 0); - const scheduled = boardChannels.reduce((s, c) => s + (c.scheduled || 0), 0); - const used = boardChannels.reduce((s, c) => s + (c.used || 0), 0); - const cap = boardChannels.reduce((s, c) => s + (c.daily_max || 9), 0); - const minTarget = boardChannels.filter((c) => c.enabled !== false).reduce((s, c) => s + (c.daily_min || 9), 0); - const gap = boardChannels.filter((c) => c.enabled !== false).reduce((s, c) => s + (c.gap ?? Math.max(0, (c.daily_max || 9) - (c.used || 0))), 0); + const uploaded = displayChannels.reduce((s, c) => s + (c.uploaded || 0), 0); + const scheduled = displayChannels.reduce((s, c) => s + (c.scheduled || 0), 0); + const used = displayChannels.reduce((s, c) => s + (c.used || 0), 0); + const cap = displayChannels.reduce((s, c) => s + (c.daily_max || 9), 0); + const minTarget = displayChannels.filter((c) => c.enabled !== false).reduce((s, c) => s + (c.daily_min || 9), 0); + const gap = displayChannels + .filter((c) => c.enabled !== false) + .reduce((s, c) => s + (c.gap ?? Math.max(0, (c.daily_max || 9) - (c.used || 0))), 0); return { uploaded, scheduled, used, cap, minTarget, gap }; - }, [boardChannels]); + }, [displayChannels]); const hoursLeft = secondsLeft > 0 ? `${Math.floor(secondsLeft / 3600)}h${Math.floor((secondsLeft % 3600) / 60)}m` : "—"; - const behindChannels = boardChannels.filter((c) => c.enabled !== false && c.behind_pace); + const behindChannels = displayChannels.filter((c) => c.enabled !== false && c.behind_pace); const oauthIssues = (status?.oauth_issues || []) as Array<{ id: string; name?: string; @@ -482,12 +569,15 @@ export default function ShortsPage() { } function toUploadTarget(u: Record, channelMap: Record): ShortsUploadTarget { + const pk = String(u.channel_pk || ""); return { id: String(u.id || ""), anchor_name: String(u.anchor_name || ""), room_id: String(u.room_id || ""), - channel_name: String(u.channel_name || channelMap[String(u.channel_pk || "")] || ""), - channel_pk: String(u.channel_pk || ""), + channel_name: String(u.channel_name || channelMap[pk] || ""), + channel_pk: pk, + oauth_profile_id: + normalizeOauthProfileId(String(u.oauth_profile_id || "")) || channelProfileMap[pk] || "", upload_at: Number(u.upload_at || 0) || undefined, clip_ready: u.clip_ready !== false, status: String(u.status || ""), @@ -949,125 +1039,130 @@ export default function ShortsPage() {

频道配额({channels.length})

- 北京日 {beijingWindow || "—"} · 每频道强制 9 条/日 + 按 Client A/B/C/D 分组 · 北京日 {beijingWindow || "—"} · 每频道 9 条/日
-
- {(boardChannels.length ? boardChannels : channels.map((c) => ({ - id: c.id, - name: c.name, - youtube_channel_id: c.youtube_channel_id, - enabled: c.enabled, - daily_min: Number(c.daily_min ?? 9), - daily_max: Number(c.daily_max ?? 9), - authorized: Boolean(c.authorized || c.has_refresh_token), - used: 0, - scheduled: 0, - uploaded: 0, - pending: 0, - gap: Number(c.daily_max ?? 9), - min_met: false, - max_met: false, - behind_pace: false, - }))).map((ch) => { - const pct = Math.min(100, Math.round((ch.used / Math.max(1, ch.daily_max)) * 100)); - const minPct = Math.round((ch.daily_min / Math.max(1, ch.daily_max)) * 100); - const full = channels.find((x) => x.id === ch.id); - const gap = ch.gap ?? Math.max(0, ch.daily_max - ch.used); - const warn = ch.enabled !== false && (ch.behind_pace || (!ch.min_met && gap > 0)); - return ( -
-
-
-
-
{ch.name || "未命名"}
- -
-
- {ch.youtube_channel_id || "—"} -
-
-
- - {ch.enabled === false ? "停用" : "启用"} - - x.id === ch.id) - ? "text-rose-300" - : ch.authorized - ? "text-teal-300" - : "text-amber-300" - } - > - {oauthIssues.some((x) => x.id === ch.id) - ? "OAuth 异常" - : ch.authorized - ? "已授权" - : "未授权"} - - {full?.oauth_profile_id ? ( - - OAuth {full.oauth_profile_id} - - ) : full?.oauth_client_source === "global" ? ( - - 默认 Client - - ) : null} -
-
-
-
+ {channelGroups.length === 0 ? ( +
+ 暂无频道。点「新频道」并选择 OAuth 配置档 A/B/C/D。 +
+ ) : ( + channelGroups.map((group) => ( +
+
+ -
= 100 ? "bg-teal-400" : pct >= minPct ? "bg-amber-400" : "bg-rose-400"}`} - style={{ width: `${pct}%` }} - /> -
-
- - {ch.used}/{ch.daily_max || 9} - (强制 9) + + {profileLabelMap[group.profileId] || (group.profileId ? `谷歌账号 ${group.profileId}` : "未绑定配置档")} + + + {group.channels.length} 个频道 - {gap > 0 ? 缺 {gap} : 已满} - 上传 {ch.uploaded} - 排队 {ch.scheduled} - 待录 {ch.pending}
-
- - {full ? ( - - ) : null} - +
+ {group.channels.map((ch) => { + const pct = Math.min(100, Math.round((ch.used / Math.max(1, ch.daily_max)) * 100)); + const minPct = Math.round((ch.daily_min / Math.max(1, ch.daily_max)) * 100); + const full = channels.find((x) => x.id === ch.id); + const gap = ch.gap ?? Math.max(0, ch.daily_max - ch.used); + const warn = ch.enabled !== false && (ch.behind_pace || (!ch.min_met && gap > 0)); + return ( +
+
+
+
+ +
{ch.name || "未命名"}
+ +
+
+ {ch.youtube_channel_id || "—"} +
+
+
+ + {ch.enabled === false ? "停用" : "启用"} + + x.id === ch.id) + ? "text-rose-300" + : ch.authorized + ? "text-teal-300" + : "text-amber-300" + } + > + {oauthIssues.some((x) => x.id === ch.id) + ? "OAuth 异常" + : ch.authorized + ? "已授权" + : "未授权"} + +
+
+
+
+
= 100 ? "bg-teal-400" : pct >= minPct ? "bg-amber-400" : "bg-rose-400"}`} + style={{ width: `${pct}%` }} + /> +
+
+ + {ch.used}/{ch.daily_max || 9} + (强制 9) + + {gap > 0 ? 缺 {gap} : 已满} + 上传 {ch.uploaded} + 排队 {ch.scheduled} + 待录 {ch.pending} +
+
+ + {full ? ( + + ) : null} + +
+
+ ); + })}
- ); - })} + )) + )}
@@ -1082,22 +1177,34 @@ export default function ShortsPage() {

- 北京 {status?.board?.beijing_hint || "—"} · 点击「立即上传」确认后发布 · 上传过程有进度提示 + 北京 {status?.board?.beijing_hint || "—"} · 按 Client A/B/C/D 分组 · 点击「立即上传」确认后发布

-
+
{upcoming.length === 0 ? (
暂无排程。取样或开播入队后,成片会自动分散到今日各时段。
) : ( - upcoming.map((u: any) => ( - + upcomingGroups.map((group) => ( +
+
+ + {group.items.length} 条待传 +
+ {group.items.map((u: any) => ( + + ))} +
)) )}
@@ -1255,7 +1362,9 @@ export default function ShortsPage() { {channels.map((c) => ( ))} @@ -1354,7 +1463,13 @@ export default function ShortsPage() { - {channelName[String(j.channel_pk || "")] || "—"} +
+ + {channelName[String(j.channel_pk || "")] || "—"} +
{j.upload_at ? fmtTs(Number(j.upload_at)) : "—"} diff --git a/frontend/src/components/OauthClientBadge.tsx b/frontend/src/components/OauthClientBadge.tsx new file mode 100644 index 0000000..25e27bc --- /dev/null +++ b/frontend/src/components/OauthClientBadge.tsx @@ -0,0 +1,57 @@ +"use client"; + +/** Distinct A/B/C/D OAuth client tags for same-named YouTube channels. */ + +const PROFILE_STYLE: Record = { + A: "border-sky-400/50 bg-sky-500/25 text-sky-100", + B: "border-violet-400/50 bg-violet-500/25 text-violet-100", + C: "border-amber-400/50 bg-amber-500/25 text-amber-100", + D: "border-rose-400/50 bg-rose-500/25 text-rose-100", +}; + +export function normalizeOauthProfileId(raw?: string | null): string { + return String(raw || "").trim().toUpperCase(); +} + +export function OauthClientBadge({ + profileId, + label, + size = "md", + className = "", +}: { + profileId?: string | null; + label?: string | null; + size?: "sm" | "md" | "lg"; + className?: string; +}) { + const id = normalizeOauthProfileId(profileId); + if (!id) { + return ( + + Client — + + ); + } + const style = PROFILE_STYLE[id] || "border-teal-400/40 bg-teal-500/20 text-teal-100"; + const sizeCls = + size === "lg" ? "px-2.5 py-1 text-xs" : size === "sm" ? "px-1.5 py-0 text-[10px]" : "px-2 py-0.5 text-[11px]"; + return ( + + Client {id} + + ); +} + +export function channelLabelWithProfile(name?: string | null, profileId?: string | null): string { + const n = String(name || "").trim() || "未命名"; + const id = normalizeOauthProfileId(profileId); + return id ? `${n} · Client ${id}` : n; +} diff --git a/frontend/src/components/ShortsJobDetailModal.tsx b/frontend/src/components/ShortsJobDetailModal.tsx index 0e9c435..1fb8a03 100644 --- a/frontend/src/components/ShortsJobDetailModal.tsx +++ b/frontend/src/components/ShortsJobDetailModal.tsx @@ -4,6 +4,7 @@ import { ExternalLink, Loader2, X } from "lucide-react"; import { useEffect, useState, type ReactNode } from "react"; import { Modal } from "@/components/Modal"; +import { OauthClientBadge } from "@/components/OauthClientBadge"; import { fetchJson, fmtTs } from "@/lib/api"; type JobDetail = { @@ -13,6 +14,7 @@ type JobDetail = { room_id?: string; channel_name?: string; channel_pk?: string; + oauth_profile_id?: string; push_date?: string; clip_path?: string; clip_ready?: boolean; @@ -124,7 +126,12 @@ export function ShortsJobDetailModal({ {job.status} - {job.channel_name || "—"} + +
+ + {job.channel_name || "—"} +
+
{job.push_date || "—"} {job.upload_at ? fmtTs(Number(job.upload_at)) : "—"} {job.created_at ? fmtTs(Number(job.created_at)) : "—"} diff --git a/frontend/src/components/ShortsUploadUx.tsx b/frontend/src/components/ShortsUploadUx.tsx index dc00da2..fef3cdc 100644 --- a/frontend/src/components/ShortsUploadUx.tsx +++ b/frontend/src/components/ShortsUploadUx.tsx @@ -11,6 +11,7 @@ import { } from "lucide-react"; import { Modal } from "@/components/Modal"; +import { OauthClientBadge } from "@/components/OauthClientBadge"; import { useToast } from "@/components/Toast"; import { fetchJson, fmtTs } from "@/lib/api"; @@ -20,6 +21,7 @@ export type ShortsUploadTarget = { room_id?: string; channel_name?: string; channel_pk?: string; + oauth_profile_id?: string; upload_at?: number; clip_ready?: boolean; status?: string; @@ -200,7 +202,10 @@ export function ShortsUploadConfirmModal({
{label}
-
目标频道 · {target.channel_name || "—"}
+
+ 目标频道 · {target.channel_name || "—"} + +
原错峰排程 · {scheduled}
{target.clip_ready === false ? "成片状态 · 等待录制完成" : "成片状态 · 已就绪"}
@@ -301,8 +306,11 @@ export function ShortsUpcomingUploadRow({ )}
-
{label}
-
+
+
{label}
+ +
+
{item.channel_name || "频道"} {item.upload_at ? ` · 排程 ${fmtTs(Number(item.upload_at))}` : " · 待排程"} {!clipReady ? " · 等待成片" : phase === "idle" ? " · 成片就绪" : ""}