Files
nonebot-plugin-tetris-stats/nonebot_plugin_tetris_stats/utils/screenshot.py
呵呵です 259b38fda5 支持设置代理 (#407)
*  添加依赖 yarl

*  添加依赖 msgspec

*  移除依赖 ujson

* ♻️ 重构 request 使其支持分别设置代理

* ♻️ 重构 resource 接口

* ️ 不再重复获取 Config

* ♻️ 使用 yarl 替换 urllib.parse

* ️ 给 get_self_netloc 加个 cache

*  request 使用 proxy

*  更新模板使用 proxy

* 🐛 修复删除 ujson 依赖后 迁移脚本报错的bug
2024-08-19 23:37:51 +00:00

29 lines
1019 B
Python

from playwright.async_api import TimeoutError, ViewportSize
from ..config.config import config
from .browser import BrowserManager
from .retry import retry
from .time_it import time_it
@retry(exception_type=TimeoutError, reply='截图失败, 重试中')
@time_it
async def screenshot(url: str) -> bytes:
browser = await BrowserManager.get_browser()
async with (
await browser.new_page(device_scale_factor=config.tetris.screenshot_quality) as page,
):
await page.goto(url)
size: ViewportSize = await page.evaluate("""
() => {
const element = document.querySelector('#content');
return {
width: element.offsetWidth,
height: element.offsetHeight,
};
};
""")
await page.set_viewport_size(size)
await page.wait_for_load_state('networkidle')
return await page.locator('id=content').screenshot(animations='disabled', timeout=5000, type='png')