monsterxcn / nonebot-plugin-gsmaterial

🤖 NoneBot2 原神每日材料查询、升级材料计算插件,数据由安柏计划自动更新
https://pypi.python.org/pypi/nonebot-plugin-gsmaterial
MIT License
20 stars 2 forks source link

“原神计算”命令图片素材下载失败 #10

Closed KishibeRohan1979 closed 1 year ago

KishibeRohan1979 commented 1 year ago

在使用“/原神计算 绫华”命令时,出现。并且我尝试使用其他角色、武器的素材图片均下载失败。以下是其中一个报错信息。(镜像使用的是默认镜像,没开代理)

06-15 15:06:08 [ERROR] nonebot_plugin_gsmaterial | 文件 摩拉.png 下载失败!
Traceback (most recent call last):
  File "<string>", line 17, in <module>
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\site-packages\nonebot\__init__.py", line 309, in run
    get_driver().run(*args, **kwargs)
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\site-packages\nonebot\drivers\fastapi.py", line 198, in run
    uvicorn.run(
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\site-packages\uvicorn\main.py", line 578, in run
    server.run()
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\site-packages\uvicorn\server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\base_events.py", line 640, in run_until_complete
    self.run_forever()
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\windows_events.py", line 321, in run_forever
    super().run_forever()
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\base_events.py", line 607, in run_forever
    self._run_once()
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\base_events.py", line 1922, in _run_once
    handle._run()
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
> File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\site-packages\nonebot_plugin_gsmaterial\data_source.py", line 379, in download
    userImage = Image.open(BytesIO(res.content))
  File "C:\Users\Administrator\Desktop\software\work\python\python3.11\Lib\site-packages\PIL\Image.py", line 3298, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x00000110B7721990>

在同样的环境下(包括文件夹、网络环境),我模仿你的代码写了test如下

import asyncio
import httpx
from io import BytesIO
from PIL import Image

async def download_image(url, file_path):

    # 远程文件下载
    async with httpx.AsyncClient(verify=False) as client:
        try:
            res = await client.get(url, timeout=20.0)
            userImage = Image.open(BytesIO(res.content))
            userImage.save(file_path, quality=100)
        except Exception as e:
            print(f"文件下载失败!{e}")

async def main():
    url = "https://act-webstatic.mihoyo.com/hk4e/e20200928calculate/item_icon_u82ase/7bd4f8aa5659289f5e70c1c8b97f2f03.png"
    file_path = 'image.png'
    await download_image(url, file_path)

# 运行异步函数
asyncio.run(main())

发现运行成功,确实下载了图片到该文件夹下。所以想问问这个错误有什么办法解决吗

monsterxcn commented 1 year ago

报错的 摩拉.png 下载地址确认是你这个嘛?最近米游社那个 b 图片下载地址好像改过,这插件可能需要更新下

monsterxcn commented 1 year ago

测试的话要照着这个写,type 传的应该是 mihoyo

https://github.com/monsterxcn/nonebot-plugin-gsmaterial/blob/main/nonebot_plugin_gsmaterial/data_source.py#L312-L387

KishibeRohan1979 commented 1 year ago

报错的 摩拉.png 下载地址确认是你这个嘛?最近米游社那个 b 图片下载地址好像改过,这插件可能需要更新下

我确认是这个地址。在日志的其他输出中找的,然后我自己也访问了这个url也是摩拉.png

06-15 18:58:47 [INFO] nonebot_plugin_gsmaterial | 正在下载文件 摩拉.png
>>>>> https://act-webstatic.mihoyo.com/hk4e/e20200928calculate/item_icon_u82ase/7bd4f8aa5659289f5e70c1c8b97f2f03.png
monsterxcn commented 1 year ago

https://github.com/monsterxcn/nonebot-plugin-gsmaterial/blob/9b17d6bbb0cfcfbd07c28a8ee98f00f0f0a4b288/nonebot_plugin_gsmaterial/data_source.py#L357

修改为:

                            "host": "act-webstatic.mihoyo.com",

即可。

KishibeRohan1979 commented 1 year ago

非常感谢,已经可以正常运行了。感谢!