From af2c21e02942fa2a81b7127d9160ed641daf85bd Mon Sep 17 00:00:00 2001 From: fllesser Date: Thu, 26 Feb 2026 23:31:37 +0800 Subject: [PATCH] refactor: streamline msgspec JSON decoder and error imports by directly importing Decoder and DecodeError. --- src/nonebot_plugin_parser/matchers/rule.py | 17 +++++++++-------- .../parsers/douyin/slides.py | 5 +++-- .../parsers/douyin/video.py | 5 +++-- .../parsers/kuaishou/states.py | 5 +++-- .../parsers/weibo/article.py | 5 +++-- .../parsers/weibo/common.py | 5 +++-- .../parsers/youtube/meta.py | 5 +++-- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/nonebot_plugin_parser/matchers/rule.py b/src/nonebot_plugin_parser/matchers/rule.py index d5730a2f..64f1eeab 100644 --- a/src/nonebot_plugin_parser/matchers/rule.py +++ b/src/nonebot_plugin_parser/matchers/rule.py @@ -1,8 +1,9 @@ import re from typing import Literal -import msgspec +from msgspec import Struct, DecodeError from nonebot import logger +from msgspec.json import Decoder from nonebot.rule import Rule from nonebot.params import Depends from nonebot.typing import T_State @@ -20,29 +21,29 @@ # 定义 JSON 卡片的数据结构 -class MetaDetail(msgspec.Struct): +class MetaDetail(Struct): qqdocurl: str | None = None -class MetaNews(msgspec.Struct): +class MetaNews(Struct): jumpUrl: str | None = None -class MetaMusic(msgspec.Struct): +class MetaMusic(Struct): jumpUrl: str | None = None -class Meta(msgspec.Struct): +class Meta(Struct): detail_1: MetaDetail | None = None news: MetaNews | None = None music: MetaMusic | None = None -class RawData(msgspec.Struct): +class RawData(Struct): meta: Meta | None = None -raw_decoder = msgspec.json.Decoder(RawData) +raw_decoder = Decoder(RawData) class SearchResult: @@ -81,7 +82,7 @@ def _extract_url(hyper: Hyper) -> str | None: try: raw = raw_decoder.decode(raw_str) - except msgspec.DecodeError: + except DecodeError: logger.exception(f"json 卡片解析失败: {raw_str}") return None diff --git a/src/nonebot_plugin_parser/parsers/douyin/slides.py b/src/nonebot_plugin_parser/parsers/douyin/slides.py index 11a481a0..6e109975 100644 --- a/src/nonebot_plugin_parser/parsers/douyin/slides.py +++ b/src/nonebot_plugin_parser/parsers/douyin/slides.py @@ -1,6 +1,7 @@ from random import choice -from msgspec import Struct, json, field +from msgspec import Struct, field +from msgspec.json import Decoder class PlayAddr(Struct): @@ -59,4 +60,4 @@ class SlidesInfo(Struct): aweme_details: list[SlidesData] = field(default_factory=list) -decoder = json.Decoder(SlidesInfo) +decoder = Decoder(SlidesInfo) diff --git a/src/nonebot_plugin_parser/parsers/douyin/video.py b/src/nonebot_plugin_parser/parsers/douyin/video.py index f60f4ad4..4493e469 100644 --- a/src/nonebot_plugin_parser/parsers/douyin/video.py +++ b/src/nonebot_plugin_parser/parsers/douyin/video.py @@ -1,7 +1,8 @@ from random import choice from typing import Any -from msgspec import Struct, json, field +from msgspec import Struct, field +from msgspec.json import Decoder from ..base import ParseException @@ -95,4 +96,4 @@ def video_data(self) -> VideoData: raise ParseException("can't find video_(id)/page or note_(id)/page in router data") -decoder = json.Decoder(RouterData) +decoder = Decoder(RouterData) diff --git a/src/nonebot_plugin_parser/parsers/kuaishou/states.py b/src/nonebot_plugin_parser/parsers/kuaishou/states.py index 18ca4a7c..4815a214 100644 --- a/src/nonebot_plugin_parser/parsers/kuaishou/states.py +++ b/src/nonebot_plugin_parser/parsers/kuaishou/states.py @@ -1,6 +1,7 @@ from random import choice -from msgspec import Struct, json, field +from msgspec import Struct, field +from msgspec.json import Decoder class CdnUrl(Struct): @@ -59,4 +60,4 @@ class TusjohData(Struct): photo: Photo | None = None -decoder = json.Decoder(dict[str, TusjohData]) +decoder = Decoder(dict[str, TusjohData]) diff --git a/src/nonebot_plugin_parser/parsers/weibo/article.py b/src/nonebot_plugin_parser/parsers/weibo/article.py index ff10853d..567877d5 100644 --- a/src/nonebot_plugin_parser/parsers/weibo/article.py +++ b/src/nonebot_plugin_parser/parsers/weibo/article.py @@ -1,4 +1,5 @@ -from msgspec import Struct, json +from msgspec import Struct +from msgspec.json import Decoder class UserInfo(Struct): @@ -20,4 +21,4 @@ class Detail(Struct): data: Data -decoder = json.Decoder(Detail) +decoder = Decoder(Detail) diff --git a/src/nonebot_plugin_parser/parsers/weibo/common.py b/src/nonebot_plugin_parser/parsers/weibo/common.py index ba8fa89a..c773ec3d 100644 --- a/src/nonebot_plugin_parser/parsers/weibo/common.py +++ b/src/nonebot_plugin_parser/parsers/weibo/common.py @@ -1,6 +1,7 @@ from re import sub -from msgspec import Struct, json +from msgspec import Struct +from msgspec.json import Decoder class LargeInPic(Struct): @@ -107,4 +108,4 @@ class WeiboResponse(Struct): data: WeiboData -decoder = json.Decoder(WeiboResponse) +decoder = Decoder(WeiboResponse) diff --git a/src/nonebot_plugin_parser/parsers/youtube/meta.py b/src/nonebot_plugin_parser/parsers/youtube/meta.py index be395ae2..5884d6c6 100644 --- a/src/nonebot_plugin_parser/parsers/youtube/meta.py +++ b/src/nonebot_plugin_parser/parsers/youtube/meta.py @@ -1,4 +1,5 @@ -from msgspec import Struct, json +from msgspec import Struct +from msgspec.json import Decoder class Thumbnail(Struct): @@ -40,4 +41,4 @@ def description(self) -> str: return self.metadata.channelMetadataRenderer.description -decoder = json.Decoder(BrowseResponse) +decoder = Decoder(BrowseResponse)