Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/nonebot_plugin_parser/renders/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,12 @@ async def _load_cover(self) -> PILImage | None:
if self.result.video is None:
return None

cover_task = self.result.video.cover
cover_path = None
if cover_task := self.result.video.cover:
cover_path = await cover_task.safe_get()

if cover_task is None:
return None

cover_path = await cover_task.safe_get()

if cover_path is None or not cover_path.exists():
return None
if cover_path is None:
return Image.open(resources.random_fallback_pic())

with Image.open(cover_path) as img:
if img.mode != "RGBA":
Expand Down Expand Up @@ -473,7 +470,7 @@ async def _render_image_grid(self) -> None:
for content in display_contents:
path = await content.safe_get()
if path is None or not path.exists():
continue
path = resources.random_fallback_pic()
if img := self._load_grid_image(path, len(display_contents)):
images.append(img)

Expand Down Expand Up @@ -578,7 +575,7 @@ async def _render_img_in_graphics(self, image_content: ImageContent) -> None:
"""渲染图片"""
path = await image_content.path_task.safe_get()
if path is None or not path.exists():
return
path = resources.random_fallback_pic()

with Image.open(path) as img:
if img.width > self.content_width:
Expand Down
6 changes: 1 addition & 5 deletions src/nonebot_plugin_parser/renders/htmlrender.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
from typing_extensions import override

from nonebot import require
Expand All @@ -23,18 +22,15 @@ async def render_image(self) -> bytes:
font = pconfig.custom_font or resources.DEFAULT_FONT_PATH
font = font.as_uri() if font.exists() else None

fallback_pics = list(resources.FAILED_PIC_DIR.glob("*.jpg"))
fallback_pic = random.choice(fallback_pics).as_uri() if fallback_pics else None

return await template_to_pic(
template_path=str(self.templates_dir),
template_name="card.html.jinja2",
templates={
"logo": logo,
"font": font,
"result": self.result,
"fallback_pic": fallback_pic,
"font_weight": pconfig.custom_font_weight,
"fallback_pic": resources.random_fallback_pic().as_uri(),
"play_button": resources.DEFAULT_VIDEO_BUTTON_PATH.as_uri(),
"default_avatar": resources.DEFAULT_AVATAR_PATH.as_uri(),
},
Expand Down
7 changes: 6 additions & 1 deletion src/nonebot_plugin_parser/renders/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
from pathlib import Path

RESOURCES_DIR = Path(__file__).parent
Expand All @@ -8,5 +9,9 @@
"""默认头像文件路径"""
DEFAULT_VIDEO_BUTTON_PATH = RESOURCES_DIR / "play.png"
"""默认视频播放按钮文件路径"""
FAILED_PIC_DIR = RESOURCES_DIR / "failed_pic"
FALLBACK_PIC_DIR = RESOURCES_DIR / "fallback_pic"
"""下载失败显示的图片文件路径"""


def random_fallback_pic() -> Path:
return FALLBACK_PIC_DIR / f"{random.randint(1, 9)}.jpg"
Loading