nonebot-plugin-rollpig 0.2.2__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,33 @@
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
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()
30
+
9
31
  # 确保依赖插件先被 NoneBot 注册
10
32
  require("nonebot_plugin_htmlrender")
11
33
  require("nonebot_plugin_localstore")
@@ -19,6 +41,7 @@ __plugin_meta__ = PluginMetadata(
19
41
  description="抽取属于自己的小猪",
20
42
  usage="""
21
43
  今日小猪 - 抽取今天属于你的小猪
44
+ 随机小猪 - 从PigHub随机获取一张猪猪图
22
45
  """,
23
46
  type="application",
24
47
  homepage="https://github.com/Bearlele/nonebot-plugin-rollpig",
@@ -34,7 +57,33 @@ RES_DIR = PLUGIN_DIR / "resource"
34
57
  # 今日记录
35
58
  TODAY_PATH = store.get_plugin_data_file("today.json")
36
59
 
37
- cmd = on_command("今天是什么小猪", aliases={"今日小猪"})
60
+ cmd = on_command("今天是什么小猪", aliases={"今日小猪"}, block=True)
61
+ roll_pig = on_command("随机小猪", block=True)
62
+
63
+ @roll_pig.handle()
64
+ async def _(event: Event):
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))
38
87
 
39
88
 
40
89
  def load_json(path, default):
@@ -57,11 +106,10 @@ def find_image_file(pig_id: str) -> Path | None:
57
106
  return file
58
107
  return None
59
108
 
60
-
61
109
  # 载入小猪信息
62
110
  PIG_LIST = load_json(PIGINFO_PATH, [])
63
111
  if not PIG_LIST:
64
- logger.error("小猪信息为空或不存在,请检查资源文件!")
112
+ logger.error("猪圈空荡荡,请检查资源文件!")
65
113
 
66
114
  # 主函数
67
115
  @cmd.handle()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot_plugin_rollpig
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: 今天是什么小猪
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -57,6 +57,8 @@ nb plugin install nonebot_plugin_rollpig
57
57
  - 重复抽取不会改变结果 🐷
58
58
  - 每天 0 点自动重置 🐖
59
59
 
60
+ **随机小猪** - 从PigHub随机获取一张猪猪图 🐖
61
+
60
62
  ---
61
63
 
62
64
  ### 🐖 新增小猪 🐖
@@ -1,4 +1,4 @@
1
- nonebot_plugin_rollpig/__init__.py,sha256=paIp_Tqb-qBhe3tbvD5kEm2QIAHf3Frr6kDOY-ZexwI,3607
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.2.dist-info/licenses/LICENSE,sha256=zn7XqavdCw11Nln_m8Eu0S7C3rZ9C-VGbqbrVGeSTGg,1087
49
- nonebot_plugin_rollpig-0.2.2.dist-info/METADATA,sha256=LAeigOITB1Ay6mdVZbxnNOTmxWz8g9QaSo10u0ZYA-o,2658
50
- nonebot_plugin_rollpig-0.2.2.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
51
- nonebot_plugin_rollpig-0.2.2.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,,