mirror of
https://github.com/PaiGramTeam/MibooGram.git
synced 2024-11-16 04:45:27 +00:00
✨ 支持使用 query_selector 限制截图区域
This commit is contained in:
parent
95b1381975
commit
9b10e382b0
@ -54,7 +54,8 @@ class TemplateService:
|
||||
return html
|
||||
|
||||
async def render(self, template_path: str, template_name: str, template_data: dict,
|
||||
viewport: ViewportSize, full_page: bool = True, evaluate: Optional[str] = None) -> bytes:
|
||||
viewport: ViewportSize = None, full_page: bool = True, evaluate: Optional[str] = None,
|
||||
query_selector: str = None) -> bytes:
|
||||
"""模板渲染成图片
|
||||
:param template_path: 模板目录
|
||||
:param template_name: 模板文件名
|
||||
@ -62,6 +63,7 @@ class TemplateService:
|
||||
:param viewport: 截图大小
|
||||
:param full_page: 是否长截图
|
||||
:param evaluate: 页面加载后运行的 js
|
||||
:param query_selector: 截图选择器
|
||||
:return:
|
||||
"""
|
||||
start_time = time.time()
|
||||
@ -76,7 +78,16 @@ class TemplateService:
|
||||
await page.set_content(html, wait_until="networkidle")
|
||||
if evaluate:
|
||||
await page.evaluate(evaluate)
|
||||
png_data = await page.screenshot(full_page=full_page)
|
||||
clip = None
|
||||
if query_selector:
|
||||
try:
|
||||
card = await page.query_selector(query_selector)
|
||||
assert card
|
||||
clip = await card.bounding_box()
|
||||
assert clip
|
||||
except AssertionError:
|
||||
logger.warning(f"未找到 {query_selector} 元素")
|
||||
png_data = await page.screenshot(clip=clip, full_page=full_page)
|
||||
await page.close()
|
||||
logger.debug(f"{template_name} 图片渲染使用了 {str(time.time() - start_time)}")
|
||||
return png_data
|
||||
|
@ -278,6 +278,7 @@ class RenderTemplate:
|
||||
data,
|
||||
{"width": 950, "height": 1080},
|
||||
full_page=True,
|
||||
query_selector=".text-neutral-200"
|
||||
)
|
||||
|
||||
async def de_stats(self) -> List[Tuple[str, Any]]:
|
||||
|
Loading…
Reference in New Issue
Block a user