|
7 | 7 | import backoff |
8 | 8 | from arclet.alconna import Arg |
9 | 9 | from httpx import AsyncClient |
10 | | -from nonebot import get_driver, get_plugin_config, logger |
| 10 | +from nonebot import get_driver, get_plugin_config |
11 | 11 | from nonebot.plugin import PluginMetadata |
12 | 12 | from nonebot_plugin_alconna import Alconna, Image, UniMessage, on_alconna |
13 | 13 | from nonebot_plugin_apscheduler import scheduler |
|
20 | 20 |
|
21 | 21 | from .....utils import ADMIN, run_task, send_message |
22 | 22 | from ... import plugin_config as bilibili_config |
23 | | -from .._utils import get_share_click, raise_for_status |
| 23 | +from .._utils import get_share_click, handle_error, raise_for_status |
24 | 24 | from .config import Config |
25 | 25 | from .models import Dynamic, Dynamics, Subscription |
26 | 26 |
|
@@ -168,9 +168,9 @@ async def broadcast(dynamic: Dynamic): |
168 | 168 |
|
169 | 169 |
|
170 | 170 | @backoff.on_exception(backoff.constant, TimeoutError, max_tries=3) |
171 | | -async def render_screenshot(dynamic: Dynamic) -> bytes: |
| 171 | +async def render_screenshot(id_str: str) -> bytes: |
172 | 172 | async with get_new_page() as page: |
173 | | - await page.goto(f"https://t.bilibili.com/{dynamic['id_str']}") |
| 173 | + await page.goto(f"https://t.bilibili.com/{id_str}") |
174 | 174 | await page.wait_for_load_state("networkidle") |
175 | 175 |
|
176 | 176 | if "opus" in page.url: |
@@ -222,9 +222,7 @@ async def _(db: async_scoped_session, sess: EventSession, uid: str): |
222 | 222 | ] not in {1, 2, 6}: |
223 | 223 | await modify_relation(uid, 1) |
224 | 224 | except Exception: |
225 | | - logger.error("订阅B站动态失败") |
226 | | - await UniMessage("订阅B站动态失败").send() |
227 | | - raise |
| 225 | + await handle_error("订阅B站动态失败") |
228 | 226 |
|
229 | 227 | sub = Subscription(uid=int(uid), session_id=await get_session_persist_id(sess)) |
230 | 228 | if sub in dynamic_subs[uid]: |
@@ -258,7 +256,7 @@ async def _(db: async_scoped_session, sess: EventSession, uid: str): |
258 | 256 | ] in {1, 2, 6}: |
259 | 257 | await modify_relation(uid, 2) |
260 | 258 | except Exception: |
261 | | - logger.error("取消关注失败") |
| 259 | + await handle_error("取消关注失败") |
262 | 260 |
|
263 | 261 | await UniMessage(f"成功取订 UID:{uid} 的动态").send() |
264 | 262 |
|
@@ -287,3 +285,25 @@ async def _(db: async_scoped_session, sess: EventSession): |
287 | 285 | await UniMessage( |
288 | 286 | "已订阅动态:\n" + "\n".join(f"UID:{sub.uid}" for sub in subs) |
289 | 287 | ).send() |
| 288 | + |
| 289 | + |
| 290 | +@on_alconna(Alconna("展示B站动态", Arg("id_str", r"re:\d+")), permission=ADMIN).handle() |
| 291 | +async def _(id_str: str): |
| 292 | + try: |
| 293 | + dynamic = raise_for_status( |
| 294 | + await client.get("/polymer/web-dynamic/v1/detail", params={"id": id_str}) |
| 295 | + )["item"] |
| 296 | + except Exception: |
| 297 | + await handle_error("获取动态信息失败") |
| 298 | + |
| 299 | + screenshot, url = await gather( |
| 300 | + render_screenshot(id_str), |
| 301 | + get_share_click(id_str, "dynamic", "dt.dt-detail.0.0.pv"), |
| 302 | + ) |
| 303 | + await plugin_config.template.format( |
| 304 | + name=dynamic["modules"]["module_author"]["name"], |
| 305 | + action=dynamic["modules"]["module_author"]["pub_action"] |
| 306 | + or plugin_config.types[dynamic["type"]], |
| 307 | + screenshot=Image(raw=screenshot), |
| 308 | + url=url, |
| 309 | + ).send() |
0 commit comments