feat(digest): 每天把当天视频情况画成一张图推到企业微信
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

运营要看的是「他们今天发了什么」,文字版在企业微信手机上没法对齐,
所以直接用 PIL 画成表:博主 / 标题 / 点赞 / 评论 / 收藏 / 分享,
按发布时间取当天,指标取最近一轮,按点赞降序。

- 播放量没进表:抖音对普通作品恒返回 0,摆一列 0 只会误导
- 定时按「今天发过没有」判断,进程重启也不会重复推
- 发送失败不写标记,下一拍继续重试
- 设置页有开关和时间;Webhook 面板加了「预览今天」和「发今天图」
- 容器需要中文字体,Dockerfile 装上 fonts-wqy-zenhei
This commit is contained in:
2026-10-23 08:24:19 +08:00
parent ae9af5e53f
commit 6ea8f24041
10 changed files with 738 additions and 30 deletions
+183
View File
@@ -0,0 +1,183 @@
# -*- coding: utf-8 -*-
"""每日「视频情况」图:取数、版式、发送前的大小闸门。"""
from datetime import date, datetime, timedelta
import pytest
import pytest_asyncio
from api.monitor import db as monitor_db
from api.monitor import digest
from api.monitor.models import (
MODE_CREATOR,
MonitorNote,
MonitorNoteMetric,
MonitorTask,
)
from tools.time_util import get_current_timestamp
MS_PER_DAY = 86_400_000
@pytest_asyncio.fixture
async def db(tmp_path):
monitor_db.set_sqlite_path(tmp_path / "digest.db")
await monitor_db.init_db()
yield monitor_db
await monitor_db.dispose_engine()
async def _task(platform: str = "dy", name: str = "图") -> int:
async with monitor_db.get_session() as session:
task = MonitorTask(
name=name,
platform=platform,
mode=MODE_CREATOR,
enabled=True,
interval_minutes=60,
max_notes_count=20,
enable_comments=False,
max_comments_count=10,
run_timeout_seconds=3600,
created_at=0,
updated_at=0,
)
session.add(task)
await session.flush()
return task.id
async def _note(task_id: int, note_id: str, name: str, published_at: int) -> None:
async with monitor_db.get_session() as session:
session.add(
MonitorNote(
task_id=task_id,
note_id=note_id,
title=f"标题-{note_id}",
hashtags="",
note_url="",
cover="",
creator_hash="hash-a",
creator_name=name,
source_kind="video",
published_at=published_at,
first_seen_at=published_at,
last_seen_at=published_at,
)
)
async def _metric(task_id: int, note_id: str, run_id: int, liked: int | None) -> None:
async with monitor_db.get_session() as session:
session.add(
MonitorNoteMetric(
task_id=task_id,
note_id=note_id,
run_id=run_id,
captured_at=run_id,
liked_count=liked,
comment_count=liked,
collected_count=liked,
share_count=liked,
)
)
@pytest.mark.asyncio
async def test_only_todays_notes_are_listed(db):
task_id = await _task()
now = get_current_timestamp()
await _note(task_id, "today", "今天", now - 1000)
await _note(task_id, "yesterday", "昨天", now - MS_PER_DAY)
async with monitor_db.get_session() as session:
rows = await digest.daily_rows(session, "dy")
assert [row["note_id"] for row in rows] == ["today"]
@pytest.mark.asyncio
async def test_latest_metric_wins_and_rows_sort_by_likes(db):
task_id = await _task()
now = get_current_timestamp()
await _note(task_id, "a", "博主A", now - 1000)
await _note(task_id, "b", "博主B", now - 2000)
await _metric(task_id, "a", run_id=1, liked=5)
await _metric(task_id, "a", run_id=2, liked=99)
await _metric(task_id, "b", run_id=1, liked=500)
async with monitor_db.get_session() as session:
rows = await digest.daily_rows(session, "dy")
assert [row["note_id"] for row in rows] == ["b", "a"] # 点赞降序
assert rows[1]["likes"] == 99 # 取最近一轮,不是先写进去的那轮
@pytest.mark.asyncio
async def test_the_same_note_under_two_tasks_is_listed_once(db):
first = await _task(name="一")
second = await _task(name="二")
now = get_current_timestamp()
await _note(first, "same", "博主A", now - 1000)
await _note(second, "same", "博主A", now - 1000)
async with monitor_db.get_session() as session:
rows = await digest.daily_rows(session, "dy")
assert len(rows) == 1
def _time_reached(send_at: str, now: datetime) -> bool:
from api.monitor.scheduler import _time_reached as reached
return reached(now, send_at)
def test_time_reached_handles_bad_config():
now = datetime(2026, 10, 23, 21, 30)
assert _time_reached("21:00", now) is True
assert _time_reached("22:00", now) is False
# 写错就按 22:00 算,而不是永远不发
assert _time_reached("十点半", datetime(2026, 10, 23, 22, 1)) is True
assert _time_reached("", datetime(2026, 10, 23, 21, 0)) is False
@pytest.mark.skipif(digest._font_path() is None, reason="本机没有中文字体")
def test_render_table_returns_a_png():
rows = [
{
"note_id": "1",
"creator": "博主A",
"title": "一条标题",
"hashtags": "",
"likes": 12,
"comments": 3,
"collected": 4,
"shares": 5,
}
]
image = digest.render_table(rows, date.today(), "抖音")
assert image.startswith(b"\x89PNG")
assert len(image) > 1000
@pytest.mark.skipif(digest._font_path() is None, reason="本机没有中文字体")
def test_render_table_handles_an_empty_day():
image = digest.render_table([], date.today(), "抖音")
assert image.startswith(b"\x89PNG")
@pytest.mark.asyncio
async def test_image_sender_guards_missing_webhook_and_oversize():
from api.monitor import notify
ok, detail = await notify.send_wecom_image("", b"x")
assert ok is False and "未配置" in detail
ok, detail = await notify.send_wecom_image(
"http://example.invalid", b"x" * (notify.WECOM_IMAGE_MAX_BYTES + 1)
)
assert ok is False and "太大" in detail