feat(digest): 每天把当天视频情况画成一张图推到企业微信
运营要看的是「他们今天发了什么」,文字版在企业微信手机上没法对齐, 所以直接用 PIL 画成表:博主 / 标题 / 点赞 / 评论 / 收藏 / 分享, 按发布时间取当天,指标取最近一轮,按点赞降序。 - 播放量没进表:抖音对普通作品恒返回 0,摆一列 0 只会误导 - 定时按「今天发过没有」判断,进程重启也不会重复推 - 发送失败不写标记,下一拍继续重试 - 设置页有开关和时间;Webhook 面板加了「预览今天」和「发今天图」 - 容器需要中文字体,Dockerfile 装上 fonts-wqy-zenhei
This commit is contained in:
@@ -27,6 +27,8 @@ Two rules shape this module:
|
||||
and swallowed.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
@@ -97,6 +99,49 @@ async def send_wecom(webhook_url: str, content: str) -> tuple[bool, str]:
|
||||
return True, "发送成功"
|
||||
|
||||
|
||||
# 企业微信图片消息的上限就是 2MB;超了会被直接拒掉,所以发送前先挡一道。
|
||||
WECOM_IMAGE_MAX_BYTES = 2 * 1024 * 1024
|
||||
|
||||
|
||||
async def send_wecom_image(webhook_url: str, image_bytes: bytes) -> tuple[bool, str]:
|
||||
"""Post an image to a WeCom group robot.
|
||||
|
||||
WeCom wants the raw base64 **and** the MD5 of the original bytes (not of the
|
||||
base64 string) -- getting that wrong produces a bare `errcode=40058` with no
|
||||
hint about which field is wrong.
|
||||
"""
|
||||
if not webhook_url:
|
||||
return False, "Webhook 未配置"
|
||||
if not image_bytes:
|
||||
return False, "图片内容为空"
|
||||
if len(image_bytes) > WECOM_IMAGE_MAX_BYTES:
|
||||
return False, f"图片太大({len(image_bytes)} 字节),企业微信上限 2MB"
|
||||
|
||||
payload = {
|
||||
"msgtype": "image",
|
||||
"image": {
|
||||
"base64": base64.b64encode(image_bytes).decode("ascii"),
|
||||
"md5": hashlib.md5(image_bytes).hexdigest(),
|
||||
},
|
||||
}
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=WEBHOOK_TIMEOUT_SECONDS) as client:
|
||||
response = await client.post(webhook_url, json=payload)
|
||||
response.raise_for_status()
|
||||
body = response.json()
|
||||
except httpx.HTTPError as exc:
|
||||
return False, f"请求失败:{exc}"
|
||||
except json.JSONDecodeError:
|
||||
return False, "返回内容不是合法 JSON,请检查 Webhook 地址"
|
||||
|
||||
errcode = body.get("errcode")
|
||||
if errcode != 0:
|
||||
return False, f"企业微信返回 errcode={errcode} {body.get('errmsg', '')}"
|
||||
|
||||
return True, "发送成功"
|
||||
|
||||
|
||||
async def get_webhook_url(session: AsyncSession) -> str:
|
||||
from .models import SETTING_WECOM_WEBHOOK
|
||||
|
||||
|
||||
Reference in New Issue
Block a user