Skip to main content

ncm-api-py

基于 ncm-api-rs 的 PyO3 绑定, 只提供 search、song_detail、song_url_v1 三个异步接口。

安装

pip install ncm-api-py

需要 CPython 3.10 或更高版本。

用法

三个方法返回可 await 的对象;在运行中的 asyncio 事件循环内调用。 结果是 ApiResponse,具有只读属性 status、body、cookie。 body 保留上游原始 JSON 数据,不转换成额外的数据模型。

import asyncio

import ncm_api_py


async def main() -> None:
    client = ncm_api_py.create_client()  # 或 Client(cookie="...")

    response = await client.search("晴天 周杰伦", type=1, limit=3)
    for song in response.body["result"].get("songs", []):
        print(song["id"], song["name"])

    detail = await client.song_detail([186016, 186017])
    for song in detail.body["songs"]:
        print(song["name"])

    urls = await client.song_url_v1([186016], level="exhigh")
    for item in urls.body["data"]:
        url = item["url"]
        if url is not None:
            print(url)
        else:
            print("无可用播放链接")


asyncio.run(main())

参数与类型

  • ids:必须是非空 list[int],每个 ID 在 1..2**63-1 范围内。 不接受单个整数、逗号字符串、tuple、bool、浮点数或仅实现 __index__ 的对象。
  • search:type 默认 1,limit 默认 30 且必须大于零, offset 默认 0 且不得为负。三个整数参数均不接受 bool,使用有符号 64 位整数范围。 搜索类型:1 单曲、10 专辑、100 歌手、1000 歌单、1002 用户、 1004 MV、1006 歌词、1009 电台、1014 视频、1018 综合、2000 声音。
  • song_url_v1:level 默认 standard,支持 standard、higher、exhigh、 lossless、hires、jyeffect、sky、dolby、jymaster。
  • 所有可选参数继续接受 None,表示使用默认值。
  • 错误参数在发起请求前抛出 TypeError、ValueError 或 OverflowError。 上游请求失败仍抛出 RuntimeError,保留上游错误文本。
  • 成功响应不保证有播放权限;url 明确为 str | None,必须判空。

随包提供类型 stub:ApiResponse[BodyT]、搜索类型/音质 Literal, 以及 SongSearchBody、SongDetailBody、SongUrlBody 等 TypedDict。 默认/单曲搜索返回歌曲结构;其他搜索类型返回递归 JSON 类型,避免把专辑、 用户等结果错误标成歌曲。可变搜索类型返回两类响应的联合类型。 这些类型仅用于静态检查,不验证上游响应;只描述常用字段,实际响应可能有更多字段。 Python 类型系统把 bool 视为 int 子类,静态检查无法完全排除 bool,运行时会严格拒绝。 类型别名在 stub 中使用,运行时注解建议使用 from __future__ import annotations 及 if TYPE_CHECKING 导入。

构建与检查

需要 Rust 工具链、Python 和 maturin。 在虚拟环境中构建:

maturin develop --release

Windows 需要 Visual Studio C++ Build Tools 和 Windows SDK,推荐使用 Developer PowerShell/Command Prompt,确保 PATH、INCLUDE、LIB 配置完整。 不要设置全局 CRATE_CC_NO_DEFAULTS=1:这会禁用 cc 的平台默认编译标志, 包括 Linux 共享库所需的 PIC、交叉编译目标及 sysroot 等。 CI 使用原生 ARM runner 构建 ARM Linux wheel,并在匹配 glibc/musl 的环境中安装测试。

pip install ruff mypy
cargo fmt --all -- --check
cargo clippy --all-targets --locked -- -D warnings
cargo test --locked
ruff check .
ruff format --check .
mypy --strict tests/typecheck.py
python -m unittest discover -s tests -v

Rust 测试需要能够链接 Python,故不启用 extension-module;maturin 构建 wheel 时自动使用配置中的 extension-module 和 abi3,生成 cp310-abi3 wheel。 Python 测试只验证接口、参数和边界条件,不连接网易云、不需要 cookie。

许可证

MIT。

Metadata

Release files for ncm-api-py 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ncm-api-py 0.1.1
File Size Uploaded
ncm_api_py-0.1.1.tar.gz 29.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for ncm-api-py 0.1.1
File
ncm_api_py-0.1.1-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
ncm_api_py-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details
ncm_api_py-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
ncm_api_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
ncm_api_py-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 11.5 MB

Release files / ncm_api_py-0.1.1.tar.gz

Download URL ncm_api_py-0.1.1.tar.gz
Size 29.5 kB
Tags Source
SHA-256 checksum
How to use checksums
02030735ef4b7ecba4bd0df4f87690881ad4dc1a0b3578ff57249398ce58843c
BLAKE2b-256 checksum
How to use checksums
0eea1871c5d98b7cbf025ae76fadceb3d39e7bc8ea06fa1dd7dbf14775fac8f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / ncm_api_py-0.1.1-cp310-abi3-win_amd64.whl

Download URL ncm_api_py-0.1.1-cp310-abi3-win_amd64.whl
Size 2.0 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
7f237993092991e5e2c18886e215472931f9e2887788a22123a09026556be33e
BLAKE2b-256 checksum
How to use checksums
619843ce7d0d2f83c7148b3bbb9f8a8cd6a680aa9c979060eade9a5b6aec977a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / ncm_api_py-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL ncm_api_py-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl
Size 2.7 MB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
a5c92143bf87ff554f285d8cc86939ba86aade029c9b7c3a922cf3769e24cf5b
BLAKE2b-256 checksum
How to use checksums
da1c783614c6dff40010fbdd69713125a33591fc695eb35d6d9049c6f9883293
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / ncm_api_py-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL ncm_api_py-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
67fd5a16596b82de0ba25678dede521ef9d8b416a59273daf70e14bb5e3c9c23
BLAKE2b-256 checksum
How to use checksums
2aaff8a3d5f8446fa6510a573012ea72bc77043de65a3e549b8263bdb08f8fee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / ncm_api_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl

Download URL ncm_api_py-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Size 2.2 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bd2666d390f303dc157578f6cb5ae2453254388acc3101614cc8fc86c210d7af
BLAKE2b-256 checksum
How to use checksums
21297912d104ca3692368d03ecb85a479ee8ff2c1b0db300c8a290aa3fcbd18c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / ncm_api_py-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl

Download URL ncm_api_py-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl
Size 2.2 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
ccfdbb4475388e7e6e58df00db2a110b7607063238b29da74f1b919f3206e051
BLAKE2b-256 checksum
How to use checksums
c0738336e87327e6dee01b2ac617ccf9fc2837b598d87b0658ddb8c5438f92ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

6 release files

0.1.0

9 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page