-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.py
More file actions
99 lines (71 loc) · 2.71 KB
/
room.py
File metadata and controls
99 lines (71 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from nonebot import require, get_driver
require("nonebot_plugin_alconna")
from nonebot.plugin import PluginMetadata
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Match, UniMessage
from playwright.async_api import async_playwright
from arclet.alconna import CommandMeta
import asyncio
import os
__plugin_meta__ = PluginMetadata(
name="Phira房间截图工具",
description="通过/room指令获取Phira房间列表截图",
usage="/room [等待秒数] - 获取房间列表截图\n可添加等待秒数参数让页面加载更长时间",
config=None,
extra={
"example": "/room 3\n房间 5\n房间列表"
}
)
room_cmd = on_alconna(
Alconna(
"room",
Args["wait_second?", int],
meta=CommandMeta(description="获取在线房间列表"),
),
aliases={"房间", "房间列表"},
use_cmd_start=True,
auto_send_output=True
)
BROWSER_PATH = os.environ.get("PLAYWRIGHT_BROWSERS_PATH", None)
async def capture_phira_screenshot(wait_second: int = 0) -> bytes:
"""使用Playwright"""
async with async_playwright() as p:
browser = await p.chromium.launch(
executable_path=BROWSER_PATH,
args=["--disable-gpu", "--no-sandbox"] if BROWSER_PATH else None
)
context = await browser.new_context(
viewport={"width": 1280, "height": 720},
device_scale_factor=1.0
)
page = await context.new_page()
try:
await page.goto(
"https://phira.htadiy.cc/rooms.html",
wait_until="networkidle",
timeout=20000
)
await asyncio.sleep(wait_second)
screenshot = await page.screenshot(
full_page=False,
type="jpeg"
)
return screenshot
except Exception as e:
raise RuntimeError(f"截图失败: {str(e)}")
finally:
await browser.close()
@room_cmd.handle()
async def handle_room_cmd(wait_second: Match[int]):
wait = wait_second.result if wait_second.available else 0
if wait < 0 or wait > 30:
await room_cmd.finish("响应超时")
try:
await room_cmd.send("正在获取房间列表截图,请稍候...")
screenshot = await capture_phira_screenshot(wait)
await UniMessage.image(raw=screenshot).send()
except Exception as e:
await room_cmd.finish(f"❌ 截图失败: {str(e)}\n请稍后再试或联系管理员进行处理。")
driver = get_driver()
@driver.on_shutdown
async def close_playwright():
pass