Skip to content

Commit

Permalink
Fix: 修复没有vb图时,截图错误 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fllesser authored Jan 11, 2025
1 parent 68dcd3c commit 18b7cac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
19 changes: 14 additions & 5 deletions nonebot_plugin_fortnite/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from nonebot import (
require,
get_driver, # @get_driver().on_startup 装饰启动时运行函数
get_bots # dict[str, BaseBot]
get_bots, # dict[str, BaseBot]
get_plugin
)
from nonebot.log import logger
from nonebot.plugin import PluginMetadata

require("nonebot_plugin_uninfo")
require("nonebot_plugin_alconna")
require("nonebot_plugin_apscheduler")
require("nonebot_plugin_localstore")
from nonebot_plugin_apscheduler import scheduler

from .config import Config
Expand All @@ -21,17 +25,22 @@
type="application",
config=Config,
homepage="https://github.com/fllesser/nonebot-plugin-fortnite",
supported_adapters=None
supported_adapters=get_plugin("nonebot_plugin_alconna").metadata.supported_adapters
)



@scheduler.scheduled_job(
"cron",
id = 'fortnite',
hour = 8,
minute = 5,
)
async def _():
await screenshot_shop_img()
await screenshot_vb_img()
try:
await screenshot_shop_img()
except Exception as e:
logger.warning(f'商城更新失败: {e}')
try:
await screenshot_vb_img()
except Exception:
logger.warning(f'vb图更新失败: {e}')
3 changes: 1 addition & 2 deletions nonebot_plugin_fortnite/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pydantic import BaseModel
from typing import Optional
from nonebot import get_plugin_config, require
from nonebot import get_plugin_config

from pathlib import Path
require("nonebot_plugin_localstore")
import nonebot_plugin_localstore as store


Expand Down
3 changes: 0 additions & 3 deletions nonebot_plugin_fortnite/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
from nonebot import require
from nonebot.plugin.on import on_command
from nonebot.permission import SUPERUSER

require("nonebot_plugin_uninfo")
from nonebot_plugin_uninfo import (
get_session,
Uninfo
)
require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import (
on_alconna,
AlconnaMatcher,
Expand Down
32 changes: 15 additions & 17 deletions nonebot_plugin_fortnite/pve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from PIL import Image
from pathlib import Path
from playwright.async_api import async_playwright
from .config import data_dir
from .config import data_dir, cache_dir

vb_file = data_dir / "vb.png"
hot_info_1_path = data_dir / 'hot_info_1.png'
container_hidden_xs_path = data_dir / 'container_hidden_xs.png'
hot_info_2_path = data_dir / 'hot_info_2.png'
hot_info_1_path = cache_dir / 'hot_info_1.png'
container_hidden_xs_path = cache_dir / 'container_hidden_xs.png'
hot_info_2_path = cache_dir / 'hot_info_2.png'

async def screenshot_vb_img() -> Path:
url = "https://freethevbucks.com/timed-missions"
Expand All @@ -22,8 +22,6 @@ async def screenshot_vb_img() -> Path:
# 截图函数,超时则跳过
async def take_screenshot(locator, path):
try:
await locator.scroll_into_view_if_needed()

# 检查元素内容是否为空
content = await locator.inner_html()
if content.strip(): # 如果内容不为空
Expand Down Expand Up @@ -54,14 +52,14 @@ async def take_screenshot(locator, path):
return vb_file

def combine_imgs():
# 打开截图文件(如果存在)
combined_image = None
img_paths = [hot_info_1_path, container_hidden_xs_path, hot_info_2_path]
img_paths = [i for i in img_paths if i.exists()]
if not img_paths:
raise Exception('所有选择器的截图文件均不存在')
try:
# 打开截图文件(如果存在)
images = []
image_paths = [hot_info_1_path, container_hidden_xs_path, hot_info_2_path]
for image_path in image_paths:
if img_path.exists():
images.append(Image.open(img_path))

images = [Image.open(img_path) for img_path in img_paths]
# 获取尺寸并创建新图像
widths, heights = zip(*(img.size for img in images))
total_width = max(widths)
Expand All @@ -80,7 +78,7 @@ def combine_imgs():
# 关闭并删除所有截图文件
for img in images:
img.close()
for img_path in image_paths:
if img_path.exists():
img_path.unlink()
combined_image.close()
for img_path in img_paths:
img_path.unlink()
if combined_image:
combined_image.close()
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-fortnite"
version = "0.1.2"
version = "0.1.3"
description = "堡垒之夜插件"
authors = ["fllesser <[email protected]>"]
readme = "README.md"
Expand All @@ -11,7 +11,7 @@ keywords = ["nonebot", "nonebot2"]

[tool.poetry.dependencies]
python = "^3.10"
httpx = "^0.27.2"
httpx = ">=0.27.0,<1.0.0"
pillow = "^10.4.0"
playwright = "^1.4.9.1"
nonebot2 = "^2.4.0"
Expand Down

0 comments on commit 18b7cac

Please sign in to comment.