|
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 |
|
21 | 23 | from .....utils import 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 |
|
@@ -82,7 +85,19 @@ async def get_dynamics(page: int = 1) -> Dynamics: |
82 | 85 | "type": "all", |
83 | 86 | "page": page, |
84 | 87 | "features": ",".join( |
85 | | - ("itemOpusStyle", "listOnlyfans", "opusBigCover", "onlyfansVote") |
| 88 | + ( |
| 89 | + "itemOpusStyle", |
| 90 | + "listOnlyfans", |
| 91 | + "opusBigCover", |
| 92 | + "onlyfansVote", |
| 93 | + "decorationCard", |
| 94 | + "onlyfansAssetsV2", |
| 95 | + "forwardListHidden", |
| 96 | + "ugcDelete", |
| 97 | + "onlyfansQaCard", |
| 98 | + "commentsNewVersion", |
| 99 | + "avatarAutoTheme", |
| 100 | + ) |
86 | 101 | ), |
87 | 102 | }, |
88 | 103 | ) |
@@ -163,7 +178,7 @@ async def render_screenshot(id_str: str) -> bytes: |
163 | 178 | ) |
164 | 179 | await page.add_style_tag( |
165 | 180 | content=""" |
166 | | - * { |
| 181 | + body { |
167 | 182 | font-family: "LXGW ZhenKai GB", "LXGW WenKai GB", sans-serif !important; |
168 | 183 | } |
169 | 184 |
|
@@ -198,15 +213,33 @@ async def render_screenshot(id_str: str) -> bytes: |
198 | 213 | async def broadcast(dynamics: list[Dynamic]): |
199 | 214 | async with get_session() as session: |
200 | 215 | 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 | | - ) |
| 216 | + with as_file(files(templates)) as templates_path: |
| 217 | + screenshot, url, subs = await gather( |
| 218 | + ( |
| 219 | + template_to_pic( |
| 220 | + str(templates_path), |
| 221 | + "draw.html.j2", |
| 222 | + dynamic, |
| 223 | + max_width=360 * 3, |
| 224 | + device_height=640 * 3, |
| 225 | + allow_refit=False, |
| 226 | + image_format="jpeg", |
| 227 | + jpeg_quality=80, |
| 228 | + ) |
| 229 | + if dynamic["type"] == "DYNAMIC_TYPE_DRAW" |
| 230 | + and not dynamic["modules"]["module_dynamic"]["additional"] |
| 231 | + else render_screenshot(dynamic["id_str"]) |
| 232 | + ), |
| 233 | + get_share_click( |
| 234 | + dynamic["id_str"], "dynamic", "dt.dt-detail.0.0.pv" |
| 235 | + ), |
| 236 | + session.scalars( |
| 237 | + select(Subscription).where( |
| 238 | + Subscription.uid |
| 239 | + == dynamic["modules"]["module_author"]["mid"] |
| 240 | + ) |
| 241 | + ), |
| 242 | + ) |
210 | 243 |
|
211 | 244 | msg = plugin_config.template.format( |
212 | 245 | name=dynamic["modules"]["module_author"]["name"], |
@@ -310,16 +343,48 @@ async def _(id_str: str): |
310 | 343 | dynamic = raise_for_status( |
311 | 344 | await client.get( |
312 | 345 | "/polymer/web-dynamic/v1/detail", |
313 | | - params={"id": id_str, "features": "itemOpusStyle"}, |
| 346 | + params={ |
| 347 | + "id": id_str, |
| 348 | + "features": ",".join( |
| 349 | + ( |
| 350 | + "itemOpusStyle", |
| 351 | + "listOnlyfans", |
| 352 | + "opusBigCover", |
| 353 | + "onlyfansVote", |
| 354 | + "decorationCard", |
| 355 | + "onlyfansAssetsV2", |
| 356 | + "forwardListHidden", |
| 357 | + "ugcDelete", |
| 358 | + "onlyfansQaCard", |
| 359 | + "commentsNewVersion", |
| 360 | + "avatarAutoTheme", |
| 361 | + ) |
| 362 | + ), |
| 363 | + }, |
314 | 364 | ) |
315 | 365 | )["item"] |
316 | 366 | except Exception: |
317 | 367 | await handle_error("获取动态信息失败") |
318 | 368 |
|
319 | | - screenshot, url = await gather( |
320 | | - render_screenshot(id_str), |
321 | | - get_share_click(id_str, "dynamic", "dt.dt-detail.0.0.pv"), |
322 | | - ) |
| 369 | + with as_file(files(templates)) as templates_path: |
| 370 | + screenshot, url = await gather( |
| 371 | + ( |
| 372 | + template_to_pic( |
| 373 | + str(templates_path), |
| 374 | + "draw.html.j2", |
| 375 | + dynamic, |
| 376 | + max_width=360 * 3, |
| 377 | + device_height=640 * 3, |
| 378 | + allow_refit=False, |
| 379 | + image_format="jpeg", |
| 380 | + jpeg_quality=80, |
| 381 | + ) |
| 382 | + if dynamic["type"] == "DYNAMIC_TYPE_DRAW" |
| 383 | + and not dynamic["modules"]["module_dynamic"]["additional"] |
| 384 | + else render_screenshot(dynamic["id_str"]) |
| 385 | + ), |
| 386 | + get_share_click(id_str, "dynamic", "dt.dt-detail.0.0.pv"), |
| 387 | + ) |
323 | 388 | await plugin_config.template.format( |
324 | 389 | name=dynamic["modules"]["module_author"]["name"], |
325 | 390 | action=dynamic["modules"]["module_author"]["pub_action"] |
|
0 commit comments