Skip to content

Commit

Permalink
🐛 修复 API 版本检查逻辑,更新本地 API 配置,添加启用标志 (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Well2333 authored Jan 3, 2025
1 parent ec6c066 commit c387c47
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import nonebot

# from nonebot.adapters.mirai2 import Adapter as Mirai_Adapter
from nonebot.adapters.onebot.v11 import Adapter as ONEBOT_V11Adapter
from nonebot.adapters.satori import Adapter as Satori_Adapter
from nonebot.adapters.onebot.v11 import Adapter as ONEBOT_V11Adapter # noqa: F401, RUF100
from nonebot.adapters.satori import Adapter as Satori_Adapter # noqa: F401, RUF100

# from nonebot.adapters.qq import Adapter as QQ_Adapter
from nonebot.log import logger
Expand Down
5 changes: 4 additions & 1 deletion nonebot_plugin_bilichat/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


class LocalApiConfig(BaseModel):
enable: bool = False
retry: int = 3
playwright_download_host: str = ""
api_path: str = "bilichatapi"
Expand Down Expand Up @@ -81,6 +82,8 @@ class RequestApiInfo(BaseModel):
"""API Token, 服务端未设置则留空"""
weight: int = 1
"""权重, 用于负载均衡, 越大越优先"""
enabled: bool = True
"""是否启用"""
note: str = ""
"""备注"""

Expand Down Expand Up @@ -114,7 +117,7 @@ class SubscribeConfig(BaseModel):


class Config(BaseModel):
_version: str = version("nonebot_plugin_bilichat")
version: str = version("nonebot_plugin_bilichat")
nonebot: NoneBotConfig = NoneBotConfig()
analyze: AnalyzeConfig = AnalyzeConfig()
api: ApiConfig = ApiConfig()
Expand Down
3 changes: 2 additions & 1 deletion nonebot_plugin_bilichat/request_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
try:
request_api = RequestAPI(URL(api.api), api.token, api.weight, api.note)
request_apis.append(request_api)
logger.info(f"API {api.api} 初始化成功, 权重: {api.weight}, 备注: {api.note}")
except Exception as e: # noqa: PERF203
logger.exception(f"API {api.api} 初始化失败, 跳过: {e}")

if config.api.local_api_config is not None:
if config.api.local_api_config is not None and config.api.local_api_config.enable:
from .local import LOCAL_REQUEST_API_PATH, LOCAL_REQUEST_API_TOKEN

request_apis.append(
Expand Down
10 changes: 6 additions & 4 deletions nonebot_plugin_bilichat/request_api/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import httpx
from httpx import AsyncClient
from nonebot.log import logger
Expand All @@ -10,7 +9,7 @@
from nonebot_plugin_bilichat.model.exception import RequestError
from nonebot_plugin_bilichat.model.request_api import Account, Content, Dynamic, LiveRoom, Note, SearchUp, VersionInfo

MINIMUM_API_VERSION = Version("0.1.2")
MINIMUM_API_VERSION = Version("0.1.3")


class RequestAPI:
Expand All @@ -33,7 +32,7 @@ def __init__(
if not (
(Version(version["version"]) >= MINIMUM_API_VERSION) # API version check
and (version["package"] == "bilichat-request") # API package check
and (Version(version["bilichat_min_version"] <= Version(__version__))) # Bilichat version check
and (Version(version["bilichat_min_version"]) <= Version(__version__)) # Bilichat version check
):
raise RuntimeError(f"API 版本不兼容, {version}")

Expand All @@ -43,7 +42,10 @@ async def _request(self, method: str, url: str, **kwargs) -> httpx.Response:
if not 199 < resp.status_code < 300:
logger.error(f"Request {method} {url} failed: {resp.status_code} {resp.json()}")
raise RequestError(resp.status_code, resp.json()["detail"])
logger.trace(f"Response {resp.status_code} {shorten_long_items(resp.json().copy())}")
try:
logger.trace(f"Response {resp.status_code} {shorten_long_items(resp.json().copy())}")
except AttributeError:
logger.trace(f"Response {resp.status_code} {resp.text}")
return resp

async def _get(self, url: str, **kwargs) -> httpx.Response:
Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_bilichat/request_api/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
LOCAL_REQUEST_API_PATH = ""
LOCAL_REQUEST_API_TOKEN = ""

if bilichat_config.api.local_api_config is not None:
if bilichat_config.api.local_api_config is not None and bilichat_config.api.local_api_config.enable:
try:
from bilichat_request.config import config as bilichat_request_config
from bilichat_request.config import set_config
Expand Down
15 changes: 10 additions & 5 deletions nonebot_plugin_bilichat/static/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
_version: "1.0.0" # nonebot_plugin_bilichat 的版本

nonebot:
# nonebot 相关配置, 无法热修改
block: false # 是否拦截事件(防止其他插件二次解析)
Expand Down Expand Up @@ -58,9 +56,16 @@ api:
- api: "https://api.example.com" # API 地址
token: "your_api_token" # API Token, 服务端未设置则留空
weight: 1 # 权重, 用于负载均衡, 越大越优先
enable: true # 是否启用该 API
# 可以根据需要添加更多 API 配置
enable_local_api: false # 是否启用本地 API
local_api_token: "" # 本地 API 的 Token
local_api_config: # 本地 API 配置
enable: false # 是否启用本地 API
api_access_token: "" # 本地 API Token, 服务端未设置则留空
api_path: bilichatapi # 本地 API 挂载路径
api_sub_dynamic_limit: 12/minute # 动态订阅限速
api_sub_live_limit: 30/minute # 直播订阅限速
playwright_download_host: "" # Playwright 下载服务地址镜像
retry: 3 # API 请求重试次数
browser_shot_quality: 75 # 浏览器截图质量, 范围为 10-100

subs:
Expand All @@ -69,4 +74,4 @@ subs:
dynamic_interval: 90 # 动态轮询间隔, 单位为秒
live_interval: 30 # 直播轮询间隔, 单位为秒
push_delay: 3 # 每条推送的延迟, 单位为秒
users: {} # 已添加订阅的用户
users: {} # 已添加订阅的用户, 请勿手动添加
6 changes: 3 additions & 3 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "nonebot-plugin-bilichat"

version = "6.0.1"
version = "6.0.2"

description = "全功能的 bilibili 内容解析器及订阅器"
authors = [
Expand Down

0 comments on commit c387c47

Please sign in to comment.