feat(publish): 30-min slot grid with silent slots and loud morning/evening anchors
This commit is contained in:
+2
-1
@@ -1015,7 +1015,8 @@ def plan_dequeue(post_id: int, request: Request):
|
||||
def plan_publish_now(post_id: int, request: Request):
|
||||
"""Опубликовать мимо расписания (кнопка 🚀 на /plan, подтверждение в форме)."""
|
||||
_require_auth(request)
|
||||
err = impl_approve(post_id)
|
||||
# «🚀 Опубликовать сейчас» — человек нажал сам, значит со звуком, мимо расписания
|
||||
err = impl_approve(post_id, silent=False)
|
||||
if err:
|
||||
return RedirectResponse(url=f"/plan?error=publish:{quote(err)}", status_code=302)
|
||||
return RedirectResponse(url="/published", status_code=302)
|
||||
|
||||
+15
-2
@@ -15,6 +15,7 @@ from config import (
|
||||
AUTOREWRITE_ENQUEUE,
|
||||
AUTOREWRITE_HOURS,
|
||||
AUTOREWRITE_MIN_TEXT,
|
||||
AUTOREWRITE_MAX_AGE_DAYS,
|
||||
AUTOREWRITE_QUEUE_DAYS,
|
||||
FIND_SOURCES_ENABLED,
|
||||
)
|
||||
@@ -96,10 +97,11 @@ def stale_posts(conn: sqlite3.Connection, hours: int | None = None,
|
||||
AND (p.rewritten_text IS NULL OR p.rewritten_text = '')
|
||||
AND length(COALESCE(p.text, '')) >= ?
|
||||
AND COALESCE(p.selected_at, p.fetched_at) <= datetime('now', ?)
|
||||
AND COALESCE(p.fetched_at, p.selected_at) >= datetime('now', ?)
|
||||
ORDER BY COALESCE(p.selected_at, p.fetched_at) ASC
|
||||
LIMIT ?
|
||||
""",
|
||||
(AUTOREWRITE_MIN_TEXT, f"-{hours} hours", limit),
|
||||
(AUTOREWRITE_MIN_TEXT, f"-{hours} hours", f"-{AUTOREWRITE_MAX_AGE_DAYS} days", limit),
|
||||
).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
@@ -134,6 +136,16 @@ def topup_queue(conn: sqlite3.Connection, live: bool) -> int:
|
||||
return added
|
||||
|
||||
|
||||
def expired_posts(conn: sqlite3.Connection) -> int:
|
||||
"""Сколько отобранных постов уже старше допустимого возраста (не публикуем, ждут решения)."""
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) c FROM posts WHERE status='selected' "
|
||||
"AND COALESCE(fetched_at, selected_at) < datetime('now', ?)",
|
||||
(f"-{AUTOREWRITE_MAX_AGE_DAYS} days",),
|
||||
).fetchone()
|
||||
return int(row["c"] or 0)
|
||||
|
||||
|
||||
def hours_in_selected(post: dict) -> float:
|
||||
"""Сколько часов пост в «Отобранных». selected_at пишется в UTC (datetime('now') в SQLite)."""
|
||||
stamp = post.get("selected_at") or post.get("fetched_at") or ""
|
||||
@@ -214,7 +226,8 @@ def run(conn: sqlite3.Connection, *, live: bool | None = None,
|
||||
from config import AUTOREWRITE_LIVE
|
||||
live = AUTOREWRITE_LIVE if live is None else live
|
||||
posts = stale_posts(conn, hours=hours, limit=limit)
|
||||
summary = {"live": live, "found": len(posts), "reports": [], "queued_topup": 0}
|
||||
summary = {"live": live, "found": len(posts), "reports": [], "queued_topup": 0,
|
||||
"expired": expired_posts(conn)}
|
||||
# сначала дозаправляем очередь уже переписанными, чтобы слоты не простаивали
|
||||
summary["queued_topup"] = topup_queue(conn, live)
|
||||
for post in posts:
|
||||
|
||||
+12
-2
@@ -3,12 +3,20 @@
|
||||
# публикатор очереди (scripts/publish_runner.py), которому незачем поднимать app.
|
||||
import json
|
||||
|
||||
from config import BASE_DIR
|
||||
from config import BASE_DIR, PUBLISH_LOUD_SLOTS
|
||||
from publisher.card import make_card
|
||||
from web.db import db
|
||||
|
||||
|
||||
def impl_approve(post_id: int, comment: str = "", dirs_selected: list[str] | None = None) -> str | None:
|
||||
def _silent_for(scheduled_at: str | None) -> bool:
|
||||
"""Тихий режим: посты из очереди в не-«якорные» слоты. Ручная публикация всегда со звуком."""
|
||||
if not scheduled_at:
|
||||
return False
|
||||
return str(scheduled_at)[11:16] not in PUBLISH_LOUD_SLOTS
|
||||
|
||||
|
||||
def impl_approve(post_id: int, comment: str = "", dirs_selected: list[str] | None = None,
|
||||
silent: bool | None = None) -> str | None:
|
||||
"""Публикация поста: publisher + бандл + статус published. Возвращает None при успехе,
|
||||
строку ошибки при неудаче (для bulk — собираем ошибки, для POST — редирект с error)."""
|
||||
from web.publisher_client import publish as http_publish
|
||||
@@ -53,6 +61,8 @@ def impl_approve(post_id: int, comment: str = "", dirs_selected: list[str] | Non
|
||||
card = make_card(post, comment)
|
||||
card["direction"] = dirn
|
||||
card["lang"] = lang
|
||||
# тихий слот — без звука; кнопка «🚀 сейчас» передаёт silent=False и всегда гремит
|
||||
card["silent"] = _silent_for(post.get("scheduled_at")) if silent is None else silent
|
||||
|
||||
try:
|
||||
res = http_publish(card)
|
||||
|
||||
Reference in New Issue
Block a user