nonebot-plugin-rollpig 0.2.3__py3-none-any.whl → 0.2.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,32 @@
1
- import json, random, datetime
1
+ import json, random, datetime, asyncio, requests
2
2
  from pathlib import Path
3
3
 
4
- from nonebot import on_command, require
4
+ from nonebot import on_command, require, on_startup
5
5
  from nonebot.adapters.onebot.v11 import Event, MessageSegment
6
6
  from nonebot.log import logger
7
7
  from nonebot.plugin import PluginMetadata
8
- import requests
8
+
9
+ pig_images = []
10
+
11
+ @on_startup
12
+ async def load_pig_images():
13
+ global pig_images
14
+ try:
15
+ data = await asyncio.to_thread(sync_fetch_pig_data, "https://pighub.top/api/all-images")
16
+
17
+ if data and data.get("images"):
18
+ pig_images = data["images"]
19
+ logger.success(f"成功从 PigHub 缓存 {len(pig_images)} 头猪猪")
20
+ else:
21
+ logger.warning("PigHub 中找不到猪猪")
22
+ except requests.exceptions.RequestException as e:
23
+ logger.error(f"从PigHub中获取猪猪失败: {e}")
24
+
25
+ def sync_fetch_pig_data(url: str):
26
+ response = requests.get(url, timeout=30)
27
+ response.raise_for_status()
28
+
29
+ return response.json()
9
30
 
10
31
  # 确保依赖插件先被 NoneBot 注册
11
32
  require("nonebot_plugin_htmlrender")
@@ -20,7 +41,7 @@ __plugin_meta__ = PluginMetadata(
20
41
  description="抽取属于自己的小猪",
21
42
  usage="""
22
43
  今日小猪 - 抽取今天属于你的小猪
23
- 随机小猪 - 从pighub随机获取一张猪猪图
44
+ 随机小猪 - 从PigHub随机获取一张猪猪图
24
45
  """,
25
46
  type="application",
26
47
  homepage="https://github.com/Bearlele/nonebot-plugin-rollpig",
@@ -41,18 +62,28 @@ roll_pig = on_command("随机小猪", block=True)
41
62
 
42
63
  @roll_pig.handle()
43
64
  async def _(event: Event):
44
- try:
45
- response = requests.get("https://pighub.top/api/all-images")
46
- response.raise_for_status() # 检查请求是否成功
47
- data = response.json()
48
- if data and data["images"]:
49
- pig = random.choice(data["images"])
50
- image_url = "https://pighub.top/data/" + pig["thumbnail"].split("/")[-1]
51
- await roll_pig.finish(MessageSegment.image(image_url))
52
- else:
53
- await roll_pig.finish("没有找到小猪图片")
54
- except requests.exceptions.RequestException as e:
55
- await roll_pig.finish(f"请求出错:{e}")
65
+ global pig_images
66
+ if not pig_images:
67
+ await roll_pig.finish("猪圈空荡荡,猪猪还没加载出来或者加载失败了...")
68
+
69
+ # 再次尝试
70
+ try:
71
+ data = await asyncio.to_thread(sync_fetch_pig_data, "https://pighub.top/api/all-images")
72
+ if data and data.get("images"):
73
+ pig_images = data["images"]
74
+ logger.success(f"成功从 PigHub 缓存 {len(pig_images)} 头猪猪")
75
+ else:
76
+ logger.warning("PigHub 中找不到猪猪")
77
+ except requests.exceptions.RequestException as e:
78
+ logger.error(f"从PigHub中获取猪猪失败: {e}")
79
+
80
+ if not pig_images:
81
+ await roll_pig.finish("猪圈空荡荡,猪猪还是没加载出来...")
82
+ return
83
+
84
+ pig = random.choice(pig_images)
85
+ image_url = "https://pighub.top/data/" + pig["thumbnail"].split("/")[-1]
86
+ await roll_pig.finish(MessageSegment.image(image_url))
56
87
 
57
88
 
58
89
  def load_json(path, default):
@@ -78,7 +109,7 @@ def find_image_file(pig_id: str) -> Path | None:
78
109
  # 载入小猪信息
79
110
  PIG_LIST = load_json(PIGINFO_PATH, [])
80
111
  if not PIG_LIST:
81
- logger.error("小猪信息为空或不存在,请检查资源文件!")
112
+ logger.error("猪圈空荡荡,请检查资源文件!")
82
113
 
83
114
  # 主函数
84
115
  @cmd.handle()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot_plugin_rollpig
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: 今天是什么小猪
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -1,4 +1,4 @@
1
- nonebot_plugin_rollpig/__init__.py,sha256=LQf4_AmGVwpiiVMMMeaeHZCXZ8E_NDvkLqZ4HU6XW-w,4396
1
+ nonebot_plugin_rollpig/__init__.py,sha256=BkTa4KmzREAFDwqm_VaEdQJIPxgzwe26lNcVFXmT7A0,5501
2
2
  nonebot_plugin_rollpig/resource/image/android-pig.png,sha256=WwY2rtsABH8YbDDIoQDR6WdHW8qpStm_lF8eXZ4K-Tc,37845
3
3
  nonebot_plugin_rollpig/resource/image/apple-pig.png,sha256=HSh8SRguD65ZZLnbnTgtNiAqqNzZKuFijtKIC3TLZDU,41533
4
4
  nonebot_plugin_rollpig/resource/image/bacon.png,sha256=7G86_W17C14qCRDKCGuRES4yfidIrIMz2gxOat3Gnn8,55722
@@ -45,7 +45,7 @@ nonebot_plugin_rollpig/resource/image/zhuge-liang.png,sha256=yvF5hNUzBXJIHi-vwg5
45
45
  nonebot_plugin_rollpig/resource/image/zombie-pig.png,sha256=fKQlp_kI3x8wMlVUT6U-rilMLpH0TpT_-ViNVWBSbcM,20797
46
46
  nonebot_plugin_rollpig/resource/pig.json,sha256=0YHz5eLZOSwo-3Yh44UgXrZ4vTmpvSDMx9Ejh0htlxM,13236
47
47
  nonebot_plugin_rollpig/resource/template.html,sha256=vX7vbT3BmmsXhswiUIHqHNLds_CcUQIuZKdnz5bVthc,1060
48
- nonebot_plugin_rollpig-0.2.3.dist-info/licenses/LICENSE,sha256=zn7XqavdCw11Nln_m8Eu0S7C3rZ9C-VGbqbrVGeSTGg,1087
49
- nonebot_plugin_rollpig-0.2.3.dist-info/METADATA,sha256=oZINPcuDR7NUb6oIPeN4RYfNuRGw8o5AyFisOp3xy5w,2720
50
- nonebot_plugin_rollpig-0.2.3.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
51
- nonebot_plugin_rollpig-0.2.3.dist-info/RECORD,,
48
+ nonebot_plugin_rollpig-0.2.4.dist-info/licenses/LICENSE,sha256=zn7XqavdCw11Nln_m8Eu0S7C3rZ9C-VGbqbrVGeSTGg,1087
49
+ nonebot_plugin_rollpig-0.2.4.dist-info/METADATA,sha256=Ewp0ze1G1_24hTqyDlWkcA-xYhgRH_i9_igiBFRlAng,2720
50
+ nonebot_plugin_rollpig-0.2.4.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
51
+ nonebot_plugin_rollpig-0.2.4.dist-info/RECORD,,