Skip to content

Commit 4a7909f

Browse files
committed
feat: experimental htmlkit
1 parent 385e211 commit 4a7909f

File tree

7 files changed

+829
-484
lines changed

7 files changed

+829
-484
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:alpine AS base
1+
FROM python:3.13-alpine AS base
22
WORKDIR /app
33

44

pdm.lock

Lines changed: 480 additions & 463 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ name = ""
33
version = "0.1.0"
44
description = "Twelve"
55
readme = "README.md"
6-
requires-python = "~=3.12"
6+
requires-python = "~=3.13"
77
dependencies = [
88
"backoff~=2.2",
99
"nonebot2[fastapi,httpx,websockets]~=2.4",
10-
"nonebot-adapter-milky~=0.5.0rc3",
11-
"nonebot-plugin-alconna~=0.59",
10+
"nonebot-adapter-milky~=1.0",
11+
"nonebot-plugin-alconna~=0.60",
1212
"nonebot-plugin-apscheduler~=0.5",
13+
"nonebot-plugin-htmlkit~=0.1.0rc4",
1314
"nonebot-plugin-htmlrender~=0.6",
1415
"nonebot-plugin-orm[sqlite]~=0.8",
1516
"nonebot-plugin-uninfo @ https://github.com/RF-Tar-Railt/nonebot-plugin-uninfo/archive/5b87d916af09d6a878039d3445193efda2ed1af3.zip",
@@ -30,6 +31,7 @@ adapters = [
3031
plugins = [
3132
"nonebot_plugin_alconna",
3233
"nonebot_plugin_apscheduler",
34+
"nonebot_plugin_htmlkit",
3335
"nonebot_plugin_htmlrender",
3436
"nonebot_plugin_orm",
3537
"nonebot_plugin_uninfo",

src/plugins/bilibili/plugins/dynamic/__init__.py

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from asyncio import gather
22
from contextlib import asynccontextmanager
3+
from importlib.resources import as_file, files
34
from queue import PriorityQueue
45
from typing import Annotated, Any, AsyncGenerator
56

@@ -11,6 +12,7 @@
1112
from nonebot.plugin import PluginMetadata
1213
from nonebot_plugin_alconna import Alconna, Image, Subcommand, UniMessage, on_alconna
1314
from nonebot_plugin_apscheduler import scheduler
15+
from nonebot_plugin_htmlkit import template_to_pic
1416
from nonebot_plugin_htmlrender.browser import get_browser
1517
from nonebot_plugin_orm import async_scoped_session, get_session
1618
from nonebot_plugin_uninfo import MEMBER
@@ -21,6 +23,7 @@
2123
from .....utils import run_task, send_message
2224
from ... import plugin_config as bilibili_config
2325
from ...utils import UID_ARG, get_share_click, handle_error, raise_for_status
26+
from . import templates
2427
from .config import Config
2528
from .models import Dynamic, Dynamics, Subscription
2629

@@ -82,7 +85,19 @@ async def get_dynamics(page: int = 1) -> Dynamics:
8285
"type": "all",
8386
"page": page,
8487
"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+
)
86101
),
87102
},
88103
)
@@ -163,7 +178,7 @@ async def render_screenshot(id_str: str) -> bytes:
163178
)
164179
await page.add_style_tag(
165180
content="""
166-
* {
181+
body {
167182
font-family: "LXGW ZhenKai GB", "LXGW WenKai GB", sans-serif !important;
168183
}
169184
@@ -198,15 +213,33 @@ async def render_screenshot(id_str: str) -> bytes:
198213
async def broadcast(dynamics: list[Dynamic]):
199214
async with get_session() as session:
200215
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+
)
210243

211244
msg = plugin_config.template.format(
212245
name=dynamic["modules"]["module_author"]["name"],
@@ -310,16 +343,48 @@ async def _(id_str: str):
310343
dynamic = raise_for_status(
311344
await client.get(
312345
"/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+
},
314364
)
315365
)["item"]
316366
except Exception:
317367
await handle_error("获取动态信息失败")
318368

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+
)
323388
await plugin_config.template.format(
324389
name=dynamic["modules"]["module_author"]["name"],
325390
action=dynamic["modules"]["module_author"]["pub_action"]

src/plugins/bilibili/plugins/dynamic/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypedDict
1+
from typing import Any, TypedDict
22

33
from nonebot_plugin_orm import Model
44
from nonebot_plugin_uninfo.orm import SceneModel
@@ -14,6 +14,7 @@ class ModuleAuthor(TypedDict):
1414

1515
class Modules(TypedDict):
1616
module_author: ModuleAuthor
17+
module_dynamic: dict[str, Any]
1718

1819

1920
class Dynamic(TypedDict):

0 commit comments

Comments
 (0)