nonebot-plugin-bugu 0.4.0__tar.gz → 0.4.1__tar.gz

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.
Files changed (33) hide show
  1. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/PKG-INFO +14 -6
  2. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/README.md +13 -5
  3. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/__init__.py +4 -0
  4. nonebot_plugin_bugu-0.4.1/nonebot_plugin_bugu/warmup.py +121 -0
  5. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/pyproject.toml +1 -1
  6. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/LICENSE +0 -0
  7. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/api.py +0 -0
  8. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/commands.py +0 -0
  9. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/config.py +0 -0
  10. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/draw.py +0 -0
  11. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/__init__.py +0 -0
  12. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/common.py +0 -0
  13. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/flowers/__init__.py +0 -0
  14. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/flowers/data.py +0 -0
  15. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/flowers/render.py +0 -0
  16. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/flowers/reply.py +0 -0
  17. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/neighbors/__init__.py +0 -0
  18. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/neighbors/data.py +0 -0
  19. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/neighbors/render.py +0 -0
  20. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/neighbors/reply.py +0 -0
  21. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/recipes/__init__.py +0 -0
  22. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/recipes/data.py +0 -0
  23. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/recipes/render.py +0 -0
  24. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/recipes/reply.py +0 -0
  25. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/recipes/search.py +0 -0
  26. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/starsea/__init__.py +0 -0
  27. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/starsea/data.py +0 -0
  28. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/starsea/render.py +0 -0
  29. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/starsea/reply.py +0 -0
  30. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/features/starsea/search.py +0 -0
  31. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/fonts.py +0 -0
  32. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/runtime.py +0 -0
  33. {nonebot_plugin_bugu-0.4.0 → nonebot_plugin_bugu-0.4.1}/nonebot_plugin_bugu/values.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot-plugin-bugu
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: 布谷工具箱(星布谷地小助手):菜谱、星海地图、友邻备忘与鲜花培育
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -95,7 +95,7 @@ nonebot.load_plugin("nonebot_plugin_bugu")
95
95
  | --- | --- | --- |
96
96
  | `BUGU_API_BASE` | `https://bugu.nsun200512.cc` | 数据站点地址 |
97
97
  | `BUGU_REQUEST_TIMEOUT` | `15.0` | 单次请求超时(秒) |
98
- | `BUGU_DATA_CACHE_TTL` | `21600` | 数据内存缓存时长(秒) |
98
+ | `BUGU_DATA_CACHE_TTL` | `21600` | 数据内存缓存时长(秒);后台刷新间隔 = 它的一半(最低 30 分钟) |
99
99
  | `BUGU_IMAGE_CACHE_TTL` | `2592000` | 底图 / 图标 / 立绘磁盘缓存时长(秒) |
100
100
  | `BUGU_IMAGE_ENABLED` | `true` | 是否出图;关闭后只发文字 |
101
101
  | `BUGU_COOLDOWN_SECONDS` | `3` | 同一用户两次出图的最小间隔(秒),`0` 不限制 |
@@ -188,8 +188,16 @@ nonebot.load_plugin("nonebot_plugin_bugu")
188
188
  - 接口:`/api/bootstrap`(菜谱)、`/api/starsea`(星海)、`/api/neighbors`(友邻)、`/api/flowers`(鲜花)
189
189
  - 图片:底图 / 菜品 / 图标 / 立绘都从同站点下载并本地缓存
190
190
 
191
- 本插件只做聊天查询,**数据版权归布谷工具箱所有**。站点更新后最迟等缓存过期(默认 6 小时),
192
- 或者让超管发一次 `/布谷刷新`。
191
+ 本插件只做聊天查询,**数据版权归布谷工具箱所有**。
192
+
193
+ ### 数据刷新
194
+
195
+ 四套数据是常开后台预热的,没有开关:
196
+
197
+ - **启动时**先拉一遍(第一条命令就是热的)
198
+ - 之后**每 3 小时**自动刷新一次(= `BUGU_DATA_CACHE_TTL` 的一半,最低 30 分钟)
199
+ - 刷新失败不会清掉旧数据,**10 分钟后重试**,期间照常使用旧缓存
200
+ - 需要立刻同步站点改动时,超管发 `/布谷刷新`
193
201
 
194
202
  ## 常见问题
195
203
 
@@ -200,8 +208,8 @@ nonebot.load_plugin("nonebot_plugin_bugu")
200
208
 
201
209
  **Q:会不会被站点限流?**
202
210
 
203
- 四个接口都是按需拉取并缓存 6 小时,图片按 URL 缓存 30 天,正常使用打不了几发请求。
204
- 遇到 429 会提示「请求太频繁」,等一会儿再试。
211
+ 四个接口每 3 小时才拉一次(后台预热),图片按 URL 缓存 30 天,正常使用打不了几发请求。
212
+ 遇到 429 会提示「请求太频繁」,后台刷新会自动退避重试,旧缓存继续可用。
205
213
 
206
214
  ## 开发
207
215
 
@@ -67,7 +67,7 @@ nonebot.load_plugin("nonebot_plugin_bugu")
67
67
  | --- | --- | --- |
68
68
  | `BUGU_API_BASE` | `https://bugu.nsun200512.cc` | 数据站点地址 |
69
69
  | `BUGU_REQUEST_TIMEOUT` | `15.0` | 单次请求超时(秒) |
70
- | `BUGU_DATA_CACHE_TTL` | `21600` | 数据内存缓存时长(秒) |
70
+ | `BUGU_DATA_CACHE_TTL` | `21600` | 数据内存缓存时长(秒);后台刷新间隔 = 它的一半(最低 30 分钟) |
71
71
  | `BUGU_IMAGE_CACHE_TTL` | `2592000` | 底图 / 图标 / 立绘磁盘缓存时长(秒) |
72
72
  | `BUGU_IMAGE_ENABLED` | `true` | 是否出图;关闭后只发文字 |
73
73
  | `BUGU_COOLDOWN_SECONDS` | `3` | 同一用户两次出图的最小间隔(秒),`0` 不限制 |
@@ -160,8 +160,16 @@ nonebot.load_plugin("nonebot_plugin_bugu")
160
160
  - 接口:`/api/bootstrap`(菜谱)、`/api/starsea`(星海)、`/api/neighbors`(友邻)、`/api/flowers`(鲜花)
161
161
  - 图片:底图 / 菜品 / 图标 / 立绘都从同站点下载并本地缓存
162
162
 
163
- 本插件只做聊天查询,**数据版权归布谷工具箱所有**。站点更新后最迟等缓存过期(默认 6 小时),
164
- 或者让超管发一次 `/布谷刷新`。
163
+ 本插件只做聊天查询,**数据版权归布谷工具箱所有**。
164
+
165
+ ### 数据刷新
166
+
167
+ 四套数据是常开后台预热的,没有开关:
168
+
169
+ - **启动时**先拉一遍(第一条命令就是热的)
170
+ - 之后**每 3 小时**自动刷新一次(= `BUGU_DATA_CACHE_TTL` 的一半,最低 30 分钟)
171
+ - 刷新失败不会清掉旧数据,**10 分钟后重试**,期间照常使用旧缓存
172
+ - 需要立刻同步站点改动时,超管发 `/布谷刷新`
165
173
 
166
174
  ## 常见问题
167
175
 
@@ -172,8 +180,8 @@ nonebot.load_plugin("nonebot_plugin_bugu")
172
180
 
173
181
  **Q:会不会被站点限流?**
174
182
 
175
- 四个接口都是按需拉取并缓存 6 小时,图片按 URL 缓存 30 天,正常使用打不了几发请求。
176
- 遇到 429 会提示「请求太频繁」,等一会儿再试。
183
+ 四个接口每 3 小时才拉一次(后台预热),图片按 URL 缓存 30 天,正常使用打不了几发请求。
184
+ 遇到 429 会提示「请求太频繁」,后台刷新会自动退避重试,旧缓存继续可用。
177
185
 
178
186
  ## 开发
179
187
 
@@ -28,3 +28,7 @@ __plugin_meta__ = PluginMetadata(
28
28
  require("nonebot_plugin_localstore")
29
29
 
30
30
  from . import commands as commands # noqa: E402,F401
31
+ from .warmup import setup as setup_warmup # noqa: E402
32
+
33
+ # 后台定时预热(常开):启动先刷一遍,之后按缓存 TTL 的一半循环刷新
34
+ setup_warmup()
@@ -0,0 +1,121 @@
1
+ """后台预热:定时把四套数据刷一遍,保证缓存常热。
2
+
3
+ 不依赖 apscheduler:启动时起一个 asyncio 任务,按「数据缓存 TTL 的一半」
4
+ 循环刷新(最低 30 分钟),进程关闭时取消。
5
+
6
+ 这样仓库里的数据不会等到有人查、发现过期了才去拉:
7
+ 最坏情况也只是「上次刷新后站点刚好更新」,而不是「6 小时前的旧数据」。
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import asyncio
13
+ from contextlib import suppress
14
+ from typing import Any, Optional
15
+
16
+ import nonebot
17
+ from nonebot.log import logger
18
+
19
+ from .api import BuguClient, BuguError
20
+ from .config import Config
21
+ from .runtime import get_client, get_config
22
+
23
+ # 刷新间隔下限:即使把缓存 TTL 调得很小,也别把站点当靶子打
24
+ MIN_INTERVAL = 30 * 60
25
+ # 刷新失败后的重试间隔
26
+ RETRY_INTERVAL = 10 * 60
27
+
28
+ TASK_ATTR = "_bugu_warmup_task"
29
+ REGISTERED_ATTR = "_bugu_warmup_registered"
30
+
31
+
32
+ def refresh_interval(config: Config) -> int:
33
+ """刷新间隔:缓存 TTL 的一半,最低 30 分钟。
34
+
35
+ TTL 默认 6 小时 → 每 3 小时刷一次,永远在过期前把数据换新。
36
+ """
37
+ ttl = max(0, int(config.bugu_data_cache_ttl))
38
+ if ttl <= 0:
39
+ return MIN_INTERVAL
40
+ return max(MIN_INTERVAL, ttl // 2)
41
+
42
+
43
+ async def refresh_once(client: Optional[BuguClient] = None) -> dict:
44
+ """立刻把四套数据刷一遍,返回各模块的快照。"""
45
+ if client is None:
46
+ client = get_client(get_config())
47
+ data = await client.preload_all(force=True)
48
+ logger.info(
49
+ "布谷工具箱缓存已刷新:"
50
+ f"菜谱 {data['recipes'].count} 道 · "
51
+ f"星海 {data['starsea'].point_count} 点位 / {data['starsea'].species_count} 物种 · "
52
+ f"友邻 {len(data['neighbors'].neighbors)} 位 · "
53
+ f"鲜花 {len(data['flowers'].flowers)} 朵"
54
+ )
55
+ return data
56
+
57
+
58
+ async def _run() -> None:
59
+ client: Optional[BuguClient] = None
60
+ delay = 0.0 # 第一轮立即执行,相当于启动预热
61
+ while True:
62
+ try:
63
+ # 即使是 0 也要 sleep:让出事件循环,避免空转
64
+ await asyncio.sleep(delay)
65
+ if client is None:
66
+ client = get_client(get_config())
67
+ await refresh_once(client)
68
+ delay = float(refresh_interval(get_config()))
69
+ except asyncio.CancelledError:
70
+ raise
71
+ except BuguError as exc:
72
+ logger.warning(
73
+ f"布谷工具箱定时刷新失败:{exc.message},{RETRY_INTERVAL // 60} 分钟后重试"
74
+ )
75
+ delay = float(RETRY_INTERVAL)
76
+ except Exception:
77
+ logger.opt(exception=True).error("布谷工具箱定时刷新异常,稍后重试")
78
+ delay = float(RETRY_INTERVAL)
79
+
80
+
81
+ async def _start() -> None:
82
+ driver = nonebot.get_driver()
83
+ old = getattr(driver, TASK_ATTR, None)
84
+ if old is not None and not old.done():
85
+ old.cancel()
86
+ with suppress(asyncio.CancelledError):
87
+ await old
88
+ task = asyncio.create_task(_run(), name="bugu-warmup")
89
+ setattr(driver, TASK_ATTR, task)
90
+
91
+
92
+ async def _stop() -> None:
93
+ driver = nonebot.get_driver()
94
+ task = getattr(driver, TASK_ATTR, None)
95
+ if task is None:
96
+ return
97
+ task.cancel()
98
+ with suppress(asyncio.CancelledError):
99
+ await task
100
+ setattr(driver, TASK_ATTR, None)
101
+
102
+
103
+ def setup() -> None:
104
+ """注册启动 / 关闭钩子;重复加载插件时不会叠加多条循环。"""
105
+ try:
106
+ driver = nonebot.get_driver()
107
+ except Exception: # 没初始化 driver(纯单测)
108
+ return
109
+ if getattr(driver, REGISTERED_ATTR, False):
110
+ return
111
+ setattr(driver, REGISTERED_ATTR, True)
112
+ driver.on_startup(_start)
113
+ driver.on_shutdown(_stop)
114
+
115
+
116
+ def current_task() -> Optional[Any]:
117
+ """调试用:看当前后台任务在不在。"""
118
+ try:
119
+ return getattr(nonebot.get_driver(), TASK_ATTR, None)
120
+ except Exception:
121
+ return None
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nonebot-plugin-bugu"
3
- version = "0.4.0"
3
+ version = "0.4.1"
4
4
  description = "布谷工具箱(星布谷地小助手):菜谱、星海地图、友邻备忘与鲜花培育"
5
5
  authors = [{ name = "TonyLiangP2010405" }]
6
6
  license = "MIT"