From c387c47422b7fb418fa627d207179f34a7dfa984 Mon Sep 17 00:00:00 2001 From: Well404 <40534114+Well2333@users.noreply.github.com> Date: Fri, 3 Jan 2025 23:47:04 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=20API=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=9C=AC=E5=9C=B0=20API=20=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=AF=E7=94=A8=E6=A0=87=E5=BF=97=20(#276)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 4 ++-- nonebot_plugin_bilichat/model/config.py | 5 ++++- nonebot_plugin_bilichat/request_api/__init__.py | 3 ++- nonebot_plugin_bilichat/request_api/base.py | 10 ++++++---- nonebot_plugin_bilichat/request_api/local.py | 2 +- nonebot_plugin_bilichat/static/config.yaml | 15 ++++++++++----- pdm.lock | 6 +++--- pyproject.toml | 2 +- 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/bot.py b/bot.py index d05c11c..3456c3e 100644 --- a/bot.py +++ b/bot.py @@ -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 diff --git a/nonebot_plugin_bilichat/model/config.py b/nonebot_plugin_bilichat/model/config.py index 0bbf62e..2e26f3d 100644 --- a/nonebot_plugin_bilichat/model/config.py +++ b/nonebot_plugin_bilichat/model/config.py @@ -6,6 +6,7 @@ class LocalApiConfig(BaseModel): + enable: bool = False retry: int = 3 playwright_download_host: str = "" api_path: str = "bilichatapi" @@ -81,6 +82,8 @@ class RequestApiInfo(BaseModel): """API Token, 服务端未设置则留空""" weight: int = 1 """权重, 用于负载均衡, 越大越优先""" + enabled: bool = True + """是否启用""" note: str = "" """备注""" @@ -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() diff --git a/nonebot_plugin_bilichat/request_api/__init__.py b/nonebot_plugin_bilichat/request_api/__init__.py index 4deca20..cf55934 100644 --- a/nonebot_plugin_bilichat/request_api/__init__.py +++ b/nonebot_plugin_bilichat/request_api/__init__.py @@ -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( diff --git a/nonebot_plugin_bilichat/request_api/base.py b/nonebot_plugin_bilichat/request_api/base.py index a2c1f49..b61c399 100644 --- a/nonebot_plugin_bilichat/request_api/base.py +++ b/nonebot_plugin_bilichat/request_api/base.py @@ -1,4 +1,3 @@ - import httpx from httpx import AsyncClient from nonebot.log import logger @@ -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: @@ -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}") @@ -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: diff --git a/nonebot_plugin_bilichat/request_api/local.py b/nonebot_plugin_bilichat/request_api/local.py index bc0b0e8..be31d18 100644 --- a/nonebot_plugin_bilichat/request_api/local.py +++ b/nonebot_plugin_bilichat/request_api/local.py @@ -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 diff --git a/nonebot_plugin_bilichat/static/config.yaml b/nonebot_plugin_bilichat/static/config.yaml index 76ecff5..db42c7a 100644 --- a/nonebot_plugin_bilichat/static/config.yaml +++ b/nonebot_plugin_bilichat/static/config.yaml @@ -1,5 +1,3 @@ -_version: "1.0.0" # nonebot_plugin_bilichat 的版本 - nonebot: # nonebot 相关配置, 无法热修改 block: false # 是否拦截事件(防止其他插件二次解析) @@ -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: @@ -69,4 +74,4 @@ subs: dynamic_interval: 90 # 动态轮询间隔, 单位为秒 live_interval: 30 # 直播轮询间隔, 单位为秒 push_delay: 3 # 每条推送的延迟, 单位为秒 - users: {} # 已添加订阅的用户 + users: {} # 已添加订阅的用户, 请勿手动添加 diff --git a/pdm.lock b/pdm.lock index c93c06e..22d189e 100644 --- a/pdm.lock +++ b/pdm.lock @@ -107,7 +107,7 @@ files = [ [[package]] name = "bilichat-request" -version = "0.1.1" +version = "0.1.2" summary = "" dependencies = [ "bcrypt", @@ -129,8 +129,8 @@ dependencies = [ "yarl", ] files = [ - {file = "bilichat_request-0.1.1-py3-none-any.whl", hash = "sha256:765eed57ee2c48cc65d4afb10c342e9fb7a4c09dd516b0247d60cdd979e9f7b4"}, - {file = "bilichat_request-0.1.1.tar.gz", hash = "sha256:39abfc07d2ee9bfef8b9949aaee3775d9cf6bb5c9e603b8e4c21a0c03bc62dfb"}, + {file = "bilichat_request-0.1.2-py3-none-any.whl", hash = "sha256:676a3a6ad0eaa0536eb5965d320c6f3c320523e0cc44169b8fa9812bb516a4df"}, + {file = "bilichat_request-0.1.2.tar.gz", hash = "sha256:e3254b4d62bf4e01c5d5a5b20457af431bb858013d90ebdaa11c7d79730f12f4"}, ] [[package]] diff --git a/pyproject.toml b/pyproject.toml index f1fd460..828a42b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "nonebot-plugin-bilichat" -version = "6.0.1" +version = "6.0.2" description = "全功能的 bilibili 内容解析器及订阅器" authors = [