Skip to main content

NoneBot2 plugin for Kuwo music search and track delivery.

Project description

logo

基于 NoneBot2 的酷我音乐插件

LICENSE python Adapters

uv ruff rust

📖 介绍

nonebot-plugin-kuwo 是一个面向 NoneBot2 的酷我音乐插件,当前已经实现:

  • 搜索歌曲列表:kwsearch / kw搜索
  • 获取关键词搜索结果中的第一首歌:kw
  • 通过 rid 直接获取单曲:kwid
  • 面向 NapCat / OneBot V11 输出 textimagecardrecordfile
  • .mflac 文件执行本地解密,转换为可播放的 .flac

插件当前明确坚持“一条命令只做一件事”,不引入等待用户继续输入序号的多轮选歌会话。

✨ 功能概览

  • 命令解析:nonebot-plugin-alconna
  • 搜索列表渲染:text / image
  • 单曲输出:text / card / record / file
  • 网络请求:httpx.AsyncClient
  • 文件缓存:nonebot-plugin-localstore
  • 图片渲染:nonebot-plugin-htmlrender
  • 原生解密:Rust + PyO3 + maturin

🔌 支持适配器

当前项目按 OneBot V11 协议实现,并优先面向 NapCat 场景优化:

当前已覆盖的消息形态:

  • 图片消息段
  • 语音消息段 record
  • 文件消息段 file
  • 自定义音乐卡片 music/custom

📦 安装

[!IMPORTANT] 普通用户安装已发布 wheel 时不需要额外构建 Rust。 只有源码开发、测试或从源码安装时,才需要本地 Rust 工具链。

使用 nb-cli 安装

在 NoneBot2 项目根目录执行:

nb plugin install nonebot-plugin-kuwo --upgrade

使用 PyPI 源:

nb plugin install nonebot-plugin-kuwo --upgrade -i https://pypi.org/simple
使用包管理器安装

推荐使用 uv

uv add nonebot-plugin-kuwo

安装 GitHub 仓库主分支:

uv add git+https://github.com/006lp/nonebot-plugin-kuwo@main

如果你使用其他包管理器,也可以选择:

pdm add nonebot-plugin-kuwo
poetry add nonebot-plugin-kuwo

安装后,在 NoneBot2 项目的 pyproject.toml 中加入:

plugins = ["nonebot_plugin_kuwo"]

⚙️ 配置

插件运行时会按需读取配置:

  • 每次命令执行时重新读取 .env
  • 如果设置了 ENVIRONMENT,会额外叠加 .env.{ENVIRONMENT}
  • 进程环境变量优先级高于 .env

[!NOTE] 当 KUWO_TRACK_RENDER_MODE=card 且没有显式配置 KUWO_LIST_RENDER_MODE 时, kwsearch 会自动回落到 image 模式,以匹配卡片场景下更合适的搜索列表展示。

配置项

配置项 必填 默认值 说明
KUWO_SEARCH_LIMIT 5 搜索结果条数,当前限制范围为 1-10
KUWO_LIST_RENDER_MODE text 搜索列表渲染模式:text / image
KUWO_TRACK_RENDER_MODE text 单曲输出模式:text / card / record / file
KUWO_DEFAULT_QUALITY standard 默认音质:standard / exhigh / lossless / hires / hifi / sur / jymaster
KUWO_TRACK_CACHE_RETENTION_DAYS 1 file 模式缓存按天数清理;0 表示禁用按时长清理
KUWO_TRACK_CACHE_MAX_SIZE_MB 1024 file 模式缓存总大小上限;0 表示禁用按大小清理,不建议低于 600

配置示例

COMMAND_START=["/"]
LOG_LEVEL=INFO
KUWO_SEARCH_LIMIT=5
KUWO_LIST_RENDER_MODE=text
KUWO_TRACK_RENDER_MODE=text
KUWO_DEFAULT_QUALITY=standard
KUWO_TRACK_CACHE_RETENTION_DAYS=1
KUWO_TRACK_CACHE_MAX_SIZE_MB=1024

🎵 使用

命令列表

[!IMPORTANT] 所有命令均显式启用 use_cmd_start=Trueblock=True,会跟随 NoneBot2 的 COMMAND_START

命令 参数 说明
kwsearch <关键词> 关键词 返回搜索结果列表
kw搜索 <关键词> 关键词 kwsearch 的中文别名
kw <关键词> [-q/--quality <quality>] 关键词,可选音质 只取搜索结果中的第一首歌
kwid <rid> [-q/--quality <quality>] 音乐 ID,可选音质 通过 rid 直接查询单曲

补充说明:

  • kwsearch 只返回搜索结果,不进入多轮选歌会话
  • 非法音质参数会直接报错,不继续请求远程接口

搜索列表输出

kwsearch / kw搜索 当前支持:

  • text
    • 返回格式:序号. 音乐id 歌曲名-歌手
  • image
    • 使用 nonebot-plugin-htmlrender 渲染搜索图片
    • 直接复用搜索接口返回的 web_albumpic_short 拼接封面
    • 渲染失败时自动回退到文本模式

单曲输出

kw / kwid 当前支持:

  • text
    • 如果有封面,则发送 image + text
    • 文本包含:歌名、歌手、专辑、时长、码率、直链
    • 如果直链接口返回 ekey,会一并显示
  • card
    • 发送 OneBot V11 自定义音乐卡片
    • urlaudio 都使用音乐直链,方便桌面端直接下载
  • record
    • 发送 OneBot V11 record 消息段
    • 直接使用远程音频地址,不走本地下载
  • file
    • 下载可播放文件到本地缓存目录后,再发送 OneBot V11 file 消息段
    • 如果源格式是 .mflac,会先本地解密成 .flac

音质规则

  • /kw/kwid 都支持 -q/--quality
  • 未传音质时,使用 KUWO_DEFAULT_QUALITY
  • text / file 使用最终解析后的音质请求远程接口
  • card 支持显式传入音质,但最终上限固定为 lossless
  • record 无论默认值还是显式传参,都会强制回落到 standard
  • record / card 的音质回落只写日志,不额外向用户发送提示消息

🗂️ 文件模式与 .mflac

普通可播放格式当前支持:

  • mp3
  • flac
  • aac
  • ogg
  • wav

文件缓存说明:

  • 缓存目录来自 nonebot-plugin-localstore
  • 子目录为 tracks/
  • 相同 rid + bitrate 优先复用已缓存文件
  • 缓存命中时会刷新文件时间,用于近似 LRU 清理
  • 默认按 1 天和 1024MB 双重策略自动清理
  • KUWO_TRACK_CACHE_RETENTION_DAYS=0 表示禁用按天数清理
  • KUWO_TRACK_CACHE_MAX_SIZE_MB=0 表示禁用按大小清理
  • 两个值都设为 0 时,不做自动缓存清理
  • 不建议将 KUWO_TRACK_CACHE_MAX_SIZE_MB 设低于 600,单个母带 .mflac 加解密过程可能临时占用约 500MB

.mflac 当前处理流程:

  1. 下载原始 .mflac
  2. 使用 Kuwo 返回的 ekey
  3. 兼容 Kuwo kuwodes 解密流程,提取 QMC 原始密钥
  4. 推导最终 QMC 密钥
  5. 本地解密为可播放的 .flac
  6. 发送解密后的 file 消息段
  7. 解密成功后立即删除中间 .mflac,避免长期双份占用

🦀 Rust 解密扩展

当前 qmc 核心已经整体迁移到 Rust,并通过 PyO3 + maturin 暴露给 Python。

项目结构中的关键文件:

  • Rust crate:Cargo.toml
  • PyO3 入口:src/lib.rs
  • 解密实现:src/qmc.rs
  • Python 包装层:nonebot_plugin_kuwo/qmc.py

当前暴露的 Python API

  • kuwo_base64_decrypt(value: str) -> str
  • extract_qmc_raw_key_from_ekey(ekey: str) -> bytes
  • derive_qmc_key(raw_key: bytes | str) -> bytes
  • decrypt_qmc_bytes(data: bytes, raw_key: bytes | str, offset: int = 0) -> bytes
  • decrypt_mflac_file(source_path: Path, target_path: Path, ekey: str, chunk_size: int = 65536) -> Path

设计说明

  • Python 侧只保留轻量包装层,不再承载核心算法
  • CPU 密集型解密逻辑在 Rust 中执行,并脱离 GIL
  • 构建产物作为包内私有扩展模块 nonebot_plugin_kuwo._qmc_rs
  • 包内额外提供 _qmc_rs.pyipy.typed,便于 Pylance / 类型检查器识别原生扩展签名
  • 采用 abi3-py310,便于生成跨 Python 3.10+ 的平台 wheel

🧪 开发

[!IMPORTANT] 源码开发、测试和本地调试需要 Rust 工具链。 已发布 wheel 安装场景不需要再手动构建 _qmc_rs

本地准备

uv sync
uv run maturin develop --release

常用命令

uv run ruff check .

uv run pytest tests/ -q -p no:cacheprovider

cargo fmt --all

⚠️ 当前限制

  • 暂未实现单用户调用频率限制

🗺️ 后续计划

  • 补齐真实 NapCat 环境下的端到端验证
  • 视需要继续优化 Rust 解密核心的吞吐表现
  • 细化 PyPI 发布矩阵与版本发布规范

🙏 致谢

📄 许可证

本项目采用 AGPL v3,详见仓库根目录下的 LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_kuwo-0.2.1.tar.gz (220.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_arm64.whl (150.8 kB view details)

Uploaded CPython 3.10+Windows ARM64

nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_amd64.whl (151.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (250.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (242.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_11_0_arm64.whl (227.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl (229.5 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file nonebot_plugin_kuwo-0.2.1.tar.gz.

File metadata

  • Download URL: nonebot_plugin_kuwo-0.2.1.tar.gz
  • Upload date:
  • Size: 220.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1.tar.gz
Algorithm Hash digest
SHA256 40f0d7d4b53a76009928462f2d5d9656b09942648ecd3c760dbce2b5bf24a522
MD5 58462d09855974fb7a117333bc387af7
BLAKE2b-256 5cc62f4c78b994ca4e49db50679b50e5af738b443512673fc4cc010c4c446f0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1.tar.gz:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 4f4951c199d41fb07096d620ac7b3d86e4edc4e4f24e381392dfd36711f1ca47
MD5 c50a133795b64ff19efbb2a061f61e1f
BLAKE2b-256 58d853b2d87804ceeae621670a7d15887f589d595b05c4ef0fd6f2e779adffc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_arm64.whl:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 13b0a501ea58dcaf11dd1360f4d7887a5bf63bc48752d77b3590ce55d3e49026
MD5 c6644b164911e3d2a10124cf5ec19e10
BLAKE2b-256 d9fe123835b24b5726d0756e65906f1547ff4f2481d740b38f20be6121f91717

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1-cp310-abi3-win_amd64.whl:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b19068eb7511b8500c2e9d2bdb890882acaef9a586380e1adfc88f6df81d602a
MD5 0c3fd7e6f84dd617bae7fe66f8ba8b3f
BLAKE2b-256 f3030d2cb01115c63641a3a091faf4f0a9b658b27cbd870c4282844b00f48314

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7abda6b3cba73956d3c6010eadc97bc4dc6a26f07e909faf1234562d02598455
MD5 7606d74a8111019e138c4694b8ad4b74
BLAKE2b-256 d9f0debd42e289004a0f5864fee0c1932f97c51120233eda92185502a9fb1fe7

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f69f21fccfa811030b656de8f913ccfdd13540ce692b93af29a0c83628ed0767
MD5 801f93e3fa74c6db291b54b370b05674
BLAKE2b-256 f12d7af0d6e2b54805f16fbea2dd7d49f6a5fd708a18cd364be917c6cce5a431

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d6a5a24d236c0f91c67da34cbfeac3e8b0d1465d8e80e7051d210eb269e45b8e
MD5 057f3fe6c1518a08812a370530355c91
BLAKE2b-256 4cb3e9086bdbf682269250da2c02cf882fc8f07a1c5065ceedd2a53b940b1bfb

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_kuwo-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on 006lp/nonebot-plugin-kuwo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page