From 765501e225231a7071fae406a024e0bc7ea2518d Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 11 Aug 2026 04:31:49 -0500 Subject: [PATCH] Shorts dedup: per-channel room_id within same Beijing day Co-authored-by: Cursor --- backend/app/db.py | 36 ++++++++++++++++++++++++---------- backend/app/shorts_pipeline.py | 6 ++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/backend/app/db.py b/backend/app/db.py index d372280..867d664 100644 --- a/backend/app/db.py +++ b/backend/app/db.py @@ -3442,21 +3442,37 @@ def claim_shorts_job_manual_upload(job_id: str) -> dict[str, Any] | None: conn.close() -def has_shorts_job_for_room_day(room_id: str, push_date: str) -> bool: +def has_shorts_job_for_room_day(room_id: str, push_date: str, channel_id: str = "") -> bool: + """Return True if this channel already has a non-failed job for room_id on push_date. + + Dedup scope: channel_id + room_id + push_date (Beijing day 00:00-24:00). + If channel_id is empty, falls back to global room_id + push_date dedup. + """ if not room_id: return False with _lock: conn = _connect() try: - row = conn.execute( - """ - SELECT 1 FROM shorts_jobs - WHERE room_id = ? AND push_date = ? - AND status NOT IN ('failed', 'skipped') - LIMIT 1 - """, - (room_id, push_date), - ).fetchone() + if channel_id: + row = conn.execute( + """ + SELECT 1 FROM shorts_jobs + WHERE room_id = ? AND push_date = ? AND channel_id = ? + AND status NOT IN ('failed', 'skipped') + LIMIT 1 + """, + (room_id, push_date, channel_id), + ).fetchone() + else: + row = conn.execute( + """ + SELECT 1 FROM shorts_jobs + WHERE room_id = ? AND push_date = ? + AND status NOT IN ('failed', 'skipped') + LIMIT 1 + """, + (room_id, push_date), + ).fetchone() return bool(row) finally: conn.close() diff --git a/backend/app/shorts_pipeline.py b/backend/app/shorts_pipeline.py index 4fcee1a..406794a 100644 --- a/backend/app/shorts_pipeline.py +++ b/backend/app/shorts_pipeline.py @@ -63,8 +63,6 @@ def _enqueue_job( if room_id.startswith("yt:"): return None day = push_date_from_ts(float((session or {}).get("started_at") or time.time())) - if settings.get("shorts_dedupe_room_day", True) and room_id and db.has_shorts_job_for_room_day(room_id, day): - return None channel: dict[str, Any] | None = None if channel_pk: ch = db.get_shorts_channel(channel_pk) @@ -77,6 +75,10 @@ def _enqueue_job( channel = db.pick_shorts_channel(day, settings) if not channel: return None + # Dedup: same channel + same room_id within the same Beijing day is not allowed + if settings.get("shorts_dedupe_room_day", True) and room_id: + if db.has_shorts_job_for_room_day(room_id, day, channel_id=str(channel["id"])): + return None sid = str((session or {}).get("id") or room.get("active_session_id") or "") job = db.create_shorts_job( session_id=sid,