取样录制 10-15s、S3 暂存、插件 engage.post;与 Shorts/TikTok 表与配额隔离。 Co-authored-by: Cursor <cursoragent@cursor.com>
109 lines
4.0 KiB
Python
109 lines
4.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""X clip post scheduling — staggered within Beijing day (isolated)."""
|
|
from __future__ import annotations
|
|
|
|
import random
|
|
import time
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from . import db
|
|
from . import x_clips_db
|
|
from .timeutil import beijing_day_bounds, beijing_from_ts, push_date_from_ts
|
|
|
|
|
|
def _day_end_ts(push_date: str) -> float:
|
|
return beijing_day_bounds(push_date)[1]
|
|
|
|
|
|
def _occupied_slots(push_date: str) -> list[float]:
|
|
data = x_clips_db.list_x_clip_jobs(push_date=push_date, limit=500, offset=0)
|
|
slots: list[float] = []
|
|
for j in data.get("jobs") or []:
|
|
st = str(j.get("status") or "")
|
|
if st in {"scheduled", "recorded", "posting"}:
|
|
ts = float(j.get("upload_at") or 0) or float(j.get("updated_at") or 0)
|
|
if ts > 0:
|
|
slots.append(ts)
|
|
elif st == "posted":
|
|
ts = float(j.get("finished_at") or j.get("updated_at") or 0)
|
|
if ts > 0:
|
|
slots.append(ts)
|
|
return slots
|
|
|
|
|
|
def pick_upload_at(*, push_date: str = "", cfg: dict[str, Any] | None = None) -> float:
|
|
settings = cfg or db.get_settings_map()
|
|
now = time.time()
|
|
day = push_date or push_date_from_ts(now)
|
|
day_end = _day_end_ts(day)
|
|
latest = day_end - 45
|
|
if not settings.get("x_clips_upload_spread", True):
|
|
return min(now + random.uniform(20, 90), latest)
|
|
global_min = max(180.0, float(settings.get("x_clips_upload_min_gap_sec") or 600))
|
|
earliest = now + random.uniform(60, 900)
|
|
if earliest >= latest:
|
|
earliest = now + random.uniform(20, max(60.0, global_min * 0.35))
|
|
slots = _occupied_slots(day)
|
|
|
|
def conflicts(ts: float) -> bool:
|
|
for other_ts in slots:
|
|
if abs(ts - other_ts) < global_min * 0.9:
|
|
return True
|
|
return False
|
|
|
|
window_end = max(earliest + 60, latest)
|
|
for _ in range(40):
|
|
cand = random.uniform(earliest, window_end)
|
|
if not conflicts(cand):
|
|
return float(min(cand, latest))
|
|
last_ts = max(slots, default=now)
|
|
return float(min(max(earliest, last_ts + global_min), latest))
|
|
|
|
|
|
def schedule_board(limit: int = 24) -> dict[str, Any]:
|
|
day = push_date_from_ts(time.time())
|
|
now = time.time()
|
|
day_start, day_end = beijing_day_bounds(day)
|
|
data = x_clips_db.list_x_clip_jobs(push_date=day, limit=500, offset=0)
|
|
upcoming: list[dict[str, Any]] = []
|
|
by_status: dict[str, int] = {}
|
|
for j in data.get("jobs") or []:
|
|
st = str(j.get("status") or "")
|
|
by_status[st] = by_status.get(st, 0) + 1
|
|
clip = str(j.get("clip_path") or "").strip()
|
|
clip_ready = bool(clip) and Path(clip).is_file()
|
|
media_ready = bool(str(j.get("media_url") or "").strip())
|
|
if st in {"scheduled", "recorded"} and (clip_ready or media_ready):
|
|
upcoming.append(j)
|
|
upcoming.sort(key=lambda x: float(x.get("upload_at") or x.get("updated_at") or 0))
|
|
enriched = []
|
|
for j in upcoming[: max(1, min(int(limit), 80))]:
|
|
clip = str(j.get("clip_path") or "").strip()
|
|
enriched.append(
|
|
{
|
|
"id": j.get("id"),
|
|
"upload_at": j.get("upload_at"),
|
|
"anchor_name": j.get("anchor_name"),
|
|
"room_id": j.get("room_id"),
|
|
"status": j.get("status"),
|
|
"caption": j.get("caption") or "",
|
|
"clip_ready": bool(clip) and Path(clip).is_file(),
|
|
"clip_seconds": float(j.get("clip_seconds") or 0),
|
|
"can_post_now": str(j.get("status") or "") in {"scheduled", "recorded", "failed"}
|
|
and (bool(clip) or bool(str(j.get("media_url") or "").strip())),
|
|
}
|
|
)
|
|
bj = beijing_from_ts(now)
|
|
return {
|
|
"push_date": day,
|
|
"now": now,
|
|
"day_start": day_start,
|
|
"day_end": day_end,
|
|
"beijing_hour": bj.hour if bj else None,
|
|
"by_status": by_status,
|
|
"used": x_clips_db.x_clips_quota_used(day),
|
|
"daily_max": x_clips_db.x_clips_daily_max(),
|
|
"upcoming": enriched,
|
|
}
|