Skip to main content

ncm-api-py

从 ncm-api-rs 而来,使用 PyO3 制作成了 Python 包。

原样转发下面三个接口,不增删请求参数,也不改动字段名、状态码与错误语义:

Python 对应 Rust 上游路径
Client.search(keywords, type=None, limit=None, offset=None) ApiClient::search /api/search/get(type=2000 时走 /api/search/voice/get)
Client.song_detail(ids) ApiClient::song_detail /api/v3/song/detail
Client.song_url_v1(ids, level=None) ApiClient::song_url_v1 /api/song/enhance/player/url/v1

安装

pip install ncm-api-py

需要 Python 3.10 或更高版本。

扩展基于 CPython stable ABI(abi3)构建,wheel 只带 cp310-abi3 标签, 同一个 wheel 在 3.10 到 3.14 上都能装,不需要按版本单独发行。 代价是只能用 3.10 就已存在的 CPython C API,并且不支持 free-threaded 版本(3.13t/3.14t)。

用法

三个方法都是 async,返回 ApiResponse(含 status / body / cookie):

import asyncio

import ncm_api_py


async def main() -> None:
    client = ncm_api_py.create_client()          # 也可以传 cookie

    # 歌曲搜索
    resp = await client.search("晴天 周杰伦", type=1, limit=3)
    for song in resp.body["result"]["songs"]:
        print(song["id"], song["name"])

    # 通过 id 获取歌曲信息;也接受列表或逗号字符串
    detail = await client.song_detail(186016)
    print(detail.body["songs"][0]["name"])
    await client.song_detail([186016, 186017])
    await client.song_detail("186016,186017")

    # 播放链接
    url_resp = await client.song_url_v1(186016, level="exhigh")
    item = url_resp.body["data"][0]
    print(item["url"])                            # 无版权/需付费时是 None


asyncio.run(main())

ids 统一接受这几种形态:186016 / "186016" / [186016, 186017] / (186016, 186017) / "186016,186017"。字符串按 , 切分、去空白、丢掉空串,这一步由 ncm-api-rs 完成。

需要注意的行为

都是实测出来的,不是照文档抄的:

  • 未显式传入的参数不会写进 Query,由 ncm-api-rs 自己套默认值 (type→1、limit→30、offset→0、level→standard)。
  • song_url_v1 的状态码是 200 也不代表一定拿得到链接。 无版权或需要付费时 body["data"][i]["url"] 是 None,必须自己判空。
  • song_url_v1 返回的 level 字段不可信。 实测它有三种表现:拿不到链接时是 None; 请求的音质真的生效时跟随请求值;没生效时退回 "standard"。判断音质请用 br(码率, 拿不到时是 0),不要用 level。
  • 播放链接是每次请求重新签名的临时 URL,不要缓存、也不要跨请求比较。
  • song_url_v1 的多 id 属未文档化行为。 ncm-api-rs 只取 Query 的 id 且不做切分,直接拼进 format!("[{}]", id);单个 id 时是合法 JSON 数组,传多个 (拼成 "1,2")依赖上游对 [1,2] 的宽容解析 —— 实测可用,但建议只传单个 id。
  • search 的返回结构随 type 变化:普通搜索在 body["result"]["songs"], type=2000(声音)在 body["data"]["resources"]。
  • 上游返回非 200 时抛出 RuntimeError,文案取自 ncm_api_rs::NcmError。

构建

需要 Rust 工具链和 maturin:

maturin develop --release

许可证

MIT。

Metadata

Release files for ncm-api-py 0.1.0

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.0
File Size Uploaded
ncm_api_py-0.1.0.tar.gz 28.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for ncm-api-py 0.1.0
File
ncm_api_py-0.1.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
ncm_api_py-0.1.0-cp310-abi3-win32.whl CPython 3.10 abi3 Windows x86-32 Details
ncm_api_py-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details
ncm_api_py-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details
ncm_api_py-0.1.0-cp310-abi3-manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.28+ x86-64 Details
ncm_api_py-0.1.0-cp310-abi3-manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.28+ ARM64 Details
ncm_api_py-0.1.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
ncm_api_py-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 18.1 MB

Release files / ncm_api_py-0.1.0.tar.gz

Download URL ncm_api_py-0.1.0.tar.gz
Size 28.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f4cd50e2bcd895628fa3d9824bf1bdd3376a5f05a2e90a48e62079fddae1f87e
BLAKE2b-256 checksum
How to use checksums
ee9b5ab38e0b011057e33e51e14aa2c4460a8dc54bcedc8bf50d283ad6482352
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

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

Download URL ncm_api_py-0.1.0-cp310-abi3-win_amd64.whl
Size 2.0 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
8edfacb48d2f929af0de442db527964ee3b3cd86f3e306cf022ea9177ce4fcbc
BLAKE2b-256 checksum
How to use checksums
f78c0f2b8fb1f33167d2c6ba3dc17639a31686d49545a1b7db6a47b9c8a1987f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / ncm_api_py-0.1.0-cp310-abi3-win32.whl

Download URL ncm_api_py-0.1.0-cp310-abi3-win32.whl
Size 1.7 MB
Tags CPython 3.10 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
134dd9c35ccc057ca5bdc7843900c247d7abca7af1e842ae32e0d67770a6be4d
BLAKE2b-256 checksum
How to use checksums
03931b241eb1a714134b4c9a4f2bd7abb57a8f816731466aff6cc86cc743d452
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

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

Download URL ncm_api_py-0.1.0-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
9df6d65e30c9c935be269fe5d0be1d7ffac2bf5b7dd89aaa3bd106acba00955c
BLAKE2b-256 checksum
How to use checksums
c98d40aceef4383cff50d44b91e5584437e7833b094c2b154e449e1233134589
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / ncm_api_py-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL ncm_api_py-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl
Size 2.6 MB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
4bae57fc8814f852efbba8bd92c732ea4fb593dcb46d9656029af76a5f09231b
BLAKE2b-256 checksum
How to use checksums
074e1e91453b1488e492941ae3861a706b4640f770d9ad16e8ef1e76972ce2e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / ncm_api_py-0.1.0-cp310-abi3-manylinux_2_28_x86_64.whl

Download URL ncm_api_py-0.1.0-cp310-abi3-manylinux_2_28_x86_64.whl
Size 2.4 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
31adf83a3a68c0b921756a657362fd1016505143189a21dcad1809a45fa3b2b2
BLAKE2b-256 checksum
How to use checksums
4e38f6892b3703e6e323385a5e8af8258abb5bb42440d524262a19a2a33b9892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / ncm_api_py-0.1.0-cp310-abi3-manylinux_2_28_aarch64.whl

Download URL ncm_api_py-0.1.0-cp310-abi3-manylinux_2_28_aarch64.whl
Size 2.4 MB
Tags CPython 3.10 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
d3be33b166ed9a3c1bd7b2ca57cbbcf9664777ae286be5694444aba2ac107477
BLAKE2b-256 checksum
How to use checksums
ba4b6682ea4df6fe1f1146ed76279fcd85d013dd7cec0941bf710228be726c59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

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

Download URL ncm_api_py-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Size 2.1 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
47708c04f9bfca46221a6d3238c77d87a632043810ab5e8c1b44d0b87c2c0b25
BLAKE2b-256 checksum
How to use checksums
ad1e1a6993f10d9cb6809cb41243fe3439491fdaae552493ed48eb3f7f422e49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

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

Download URL ncm_api_py-0.1.0-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
deb7f2c6829c27678e00ebf72023b74c4c07fd50866a3872a6fe48808703a419
BLAKE2b-256 checksum
How to use checksums
7a451cf64fe3a62eb789b4e1670fd8db494a33b3f01c62cbe469149be5bf817c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release history Release notifications | RSS feed

0.1.1

6 release files

This release

0.1.0 This release

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