|
1 | 1 | from asyncio import gather |
2 | 2 | from contextlib import asynccontextmanager |
| 3 | +from importlib.resources import as_file, files |
3 | 4 | from queue import PriorityQueue |
4 | 5 | from typing import Annotated, Any, AsyncGenerator |
5 | 6 |
|
|
11 | 12 | from nonebot.plugin import PluginMetadata |
12 | 13 | from nonebot_plugin_alconna import Alconna, Image, Subcommand, UniMessage, on_alconna |
13 | 14 | from nonebot_plugin_apscheduler import scheduler |
| 15 | +from nonebot_plugin_htmlkit import template_to_pic |
14 | 16 | from nonebot_plugin_htmlrender.browser import get_browser |
15 | 17 | from nonebot_plugin_orm import async_scoped_session, get_session |
16 | 18 | from nonebot_plugin_uninfo import MEMBER |
17 | 19 | from nonebot_plugin_uninfo.orm import SceneModel, SceneOrm |
18 | 20 | from playwright.async_api import BrowserContext, Page |
19 | 21 | from sqlalchemy import exists, select |
20 | 22 |
|
21 | | -from .....utils import run_task, send_message |
| 23 | +from .....utils import css_fetch_fn, img_fetch_fn, run_task, send_message |
22 | 24 | from ... import plugin_config as bilibili_config |
23 | 25 | from ...utils import UID_ARG, get_share_click, handle_error, raise_for_status |
| 26 | +from . import templates |
24 | 27 | from .config import Config |
25 | 28 | from .models import Dynamic, Dynamics, Subscription |
26 | 29 |
|
@@ -163,7 +166,7 @@ async def render_screenshot(id_str: str) -> bytes: |
163 | 166 | ) |
164 | 167 | await page.add_style_tag( |
165 | 168 | content=""" |
166 | | - * { |
| 169 | + body { |
167 | 170 | font-family: "LXGW ZhenKai GB", "LXGW WenKai GB", sans-serif !important; |
168 | 171 | } |
169 | 172 |
|
@@ -198,15 +201,40 @@ async def render_screenshot(id_str: str) -> bytes: |
198 | 201 | async def broadcast(dynamics: list[Dynamic]): |
199 | 202 | async with get_session() as session: |
200 | 203 | for dynamic in dynamics: |
201 | | - screenshot, url, subs = await gather( |
202 | | - render_screenshot(dynamic["id_str"]), |
203 | | - get_share_click(dynamic["id_str"], "dynamic", "dt.dt-detail.0.0.pv"), |
204 | | - session.scalars( |
205 | | - select(Subscription).where( |
206 | | - Subscription.uid == dynamic["modules"]["module_author"]["mid"] |
207 | | - ) |
208 | | - ), |
209 | | - ) |
| 204 | + with as_file(files(templates)) as templates_path: |
| 205 | + screenshot, url, subs = await gather( |
| 206 | + ( |
| 207 | + template_to_pic( |
| 208 | + str(templates_path), |
| 209 | + "draw.html.j2", |
| 210 | + {"dynamic": dynamic}, |
| 211 | + max_width=360 * 3, |
| 212 | + device_height=640 * 3, |
| 213 | + allow_refit=False, |
| 214 | + img_fetch_fn=img_fetch_fn, |
| 215 | + css_fetch_fn=css_fetch_fn, |
| 216 | + image_format="jpeg", |
| 217 | + jpeg_quality=80, |
| 218 | + ) |
| 219 | + if dynamic["type"] == "DYNAMIC_TYPE_DRAW" |
| 220 | + and not ( |
| 221 | + dynamic["modules"]["module_dynamic"]["additional"] |
| 222 | + or dynamic["modules"]["module_dynamic"]["major"]["opus"][ |
| 223 | + "pics" |
| 224 | + ] |
| 225 | + ) |
| 226 | + else render_screenshot(dynamic["id_str"]) |
| 227 | + ), |
| 228 | + get_share_click( |
| 229 | + dynamic["id_str"], "dynamic", "dt.dt-detail.0.0.pv" |
| 230 | + ), |
| 231 | + session.scalars( |
| 232 | + select(Subscription).where( |
| 233 | + Subscription.uid |
| 234 | + == dynamic["modules"]["module_author"]["mid"] |
| 235 | + ) |
| 236 | + ), |
| 237 | + ) |
210 | 238 |
|
211 | 239 | msg = plugin_config.template.format( |
212 | 240 | name=dynamic["modules"]["module_author"]["name"], |
@@ -316,10 +344,30 @@ async def _(id_str: str): |
316 | 344 | except Exception: |
317 | 345 | await handle_error("获取动态信息失败") |
318 | 346 |
|
319 | | - screenshot, url = await gather( |
320 | | - render_screenshot(id_str), |
321 | | - get_share_click(id_str, "dynamic", "dt.dt-detail.0.0.pv"), |
322 | | - ) |
| 347 | + with as_file(files(templates)) as templates_path: |
| 348 | + screenshot, url = await gather( |
| 349 | + ( |
| 350 | + template_to_pic( |
| 351 | + str(templates_path), |
| 352 | + "draw.html.j2", |
| 353 | + {"dynamic": dynamic}, |
| 354 | + max_width=360 * 3, |
| 355 | + device_height=640 * 3, |
| 356 | + allow_refit=False, |
| 357 | + img_fetch_fn=img_fetch_fn, |
| 358 | + css_fetch_fn=css_fetch_fn, |
| 359 | + image_format="jpeg", |
| 360 | + jpeg_quality=80, |
| 361 | + ) |
| 362 | + if dynamic["type"] == "DYNAMIC_TYPE_DRAW" |
| 363 | + and not ( |
| 364 | + dynamic["modules"]["module_dynamic"]["additional"] |
| 365 | + or dynamic["modules"]["module_dynamic"]["major"]["opus"]["pics"] |
| 366 | + ) |
| 367 | + else render_screenshot(dynamic["id_str"]) |
| 368 | + ), |
| 369 | + get_share_click(id_str, "dynamic", "dt.dt-detail.0.0.pv"), |
| 370 | + ) |
323 | 371 | await plugin_config.template.format( |
324 | 372 | name=dynamic["modules"]["module_author"]["name"], |
325 | 373 | action=dynamic["modules"]["module_author"]["pub_action"] |
|
0 commit comments