Skip to main content

Python bindings for MusicLibrary - access NetEase Cloud Music and KuGou Music APIs

Project description

MusicLibrary Python 绑定

Python Version License Platform

这是 MusicLibrary 的 Python 语言绑定,提供了对网易云音乐、酷狗音乐等音乐平台 API 的 Python 接口访问。

📦 安装

从 PyPI 安装(推荐)

pip install pymusiclibrary

从源码安装

从源码安装需要先构建 wheel 包,详见下方 构建指南

# 克隆仓库
git clone https://github.com/2061360308/MusicLibrary.git
cd MusicLibrary/src/python

# 安装依赖
pip install -r requirements.txt

# 按照 [构建指南](#-构建指南) 构建完成后安装
pip install dist/musiclibrary-*.whl

🚀 快速开始

响应对象 Response

所有 API 请求返回统一的 Response 对象,自动解析 JSON 响应字符串。

属性说明

属性 类型 说明
status int HTTP 状态码,200 表示成功,解析失败默认 500
headers dict 响应头信息,解析失败默认空字典
body / data dict 响应体数据,解析失败默认空字典
cookies str 从 headers 获取的 Set-Cookie
from MusicLibrary.neteaseCloudMusicApi import NeteaseCloudMusicApi

ncm = NeteaseCloudMusicApi()
response = ncm.playlist_mylike()

# 检查状态码
if response.status == 200:
    # 获取数据(body 和 data 等价)
    songs = response.body.get('playlist', {}).get('tracks', [])
    songs = response.data.get('playlist', {}).get('tracks', [])

# 获取 Cookie(登录接口返回)
cookies = response.cookies

# 查看完整响应(自动格式化打印)
print(response)

错误处理:当响应解析失败时,Response 会自动使用默认值(status=500, headers={}, body={}),不会抛出异常。

网易云音乐

from MusicLibrary.neteaseCloudMusicApi import NeteaseCloudMusicApi, NcmProcessEnv

# 创建 API 实例
ncm = NeteaseCloudMusicApi()

# 使用封装好的方法
response = ncm.playlist_mylike()
print(response)

# 获取歌曲详情
response = ncm.song_detail(ids="347230")
print(response)

# 搜索歌曲
response = ncm.search_default()
print(response)

酷狗音乐

from MusicLibrary.kuGouMusicApi import KuGouMusicApi, Platform, KugouProcessEnv

# 创建 API 实例(使用轻量版平台)
kugou = KuGouMusicApi(KugouProcessEnv(platform=Platform.LITE))

# 获取新歌速递
response1 = kugou.top_song()
print(response1)

# 获取专辑详情
response2 = kugou.album_detail(id='10729818')
print(response2)

📚 API 文档

网易云音乐 API

网易云音乐 API 已完整封装为方法,直接调用即可。

示例方法:

  • login_cellphone() - 手机号登录
  • playlist_mylike() - 获取用户歌单
  • song_detail() - 获取歌曲详情
  • search_default() - 搜索歌曲
  • lyric() - 获取歌词
  • artist_detail() - 获取艺术家详情
  • album() - 获取专辑信息
  • 更多方法请查看 neteaseCloudMusicApi.py 源码

酷狗音乐 API

酷狗音乐提供了封装好的方法,直接调用即可。

示例方法:

  • top_song() - 获取新歌速递
  • album_detail() - 获取专辑详情
  • search_default() - 搜索歌曲
  • 更多方法请查看 kuGouMusicApi.py 源码

平台配置

from MusicLibrary.kuGouMusicApi import Platform, KugouProcessEnv

# 支持的平台类型
kugou = KuGouMusicApi(KugouProcessEnv(platform=Platform.LITE))   # 概念版
kugou = KuGouMusicApi(KugouProcessEnv(platform=Platform.WEB))    # 普通版

⚠️ 注意事项

线程安全

重要KuGouMusicApiNeteaseCloudMusicApi 等 API 对象不能跨线程使用。如果需要多线程访问,请为每个线程创建独立的实例。

from MusicLibrary.neteaseCloudMusicApi import NeteaseCloudMusicApi

# 错误示例 - 不要这样做!
api = NeteaseCloudMusicApi(None)
Thread(target=api.request, args=('/api/path', '{}')).start()

# 正确做法
def worker():
    api = NeteaseCloudMusicApi()
    return api.login_cellphone(phone="xxx", password="yyy")

Thread(target=worker).start()

缓存

原始 Node.js 项目支持缓存功能,相同请求会返回缓存数据。当前 Python 绑定版本尚未实现缓存功能,请根据业务需求自行处理。

🔧 构建指南

上游仓库已完成预编译,只需要从对方 Release 下载对应平台的预编译库即可。

Windows 平台

# 1. 下载并配置预编译库
python setLibArch.py win64  # 或 win32、winarm

# 2. 设置环境变量(CMD)
set MUSICLIB_ARCH=win64

# 3. 构建 wheel
python -m build --wheel

PowerShell 设置环境变量:

$env:MUSICLIB_ARCH="win64"
python -m build --wheel

Linux 平台

# 1. 下载并配置预编译库
python setLibArch.py linux64  # 或 linuxarm

# 2. 设置环境变量
export MUSICLIB_ARCH=linux64

# 3. 构建 wheel
python -m build --wheel

macOS 平台

# 1. 下载并配置预编译库
python setLibArch.py macos64  # 或 macosarm

# 2. 设置环境变量
export MUSICLIB_ARCH=macos64

# 3. 构建 wheel
python -m build --wheel

支持的架构

架构参数 平台 说明
win64 Windows 64 位 Windows
win32 Windows 32 位 Windows
winarm Windows ARM64 Windows
linux64 Linux x86_64 Linux
linuxarm Linux ARM64 Linux
macos64 macOS Intel 芯片 macOS
macosarm macOS Apple Silicon (M1/M2/M3)

🤝 贡献

欢迎提交 Issue 和 Pull Request!

贡献指南:

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

🔗 相关链接

⚖️ 许可证

本项目采用 MIT 许可证开源。详见 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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

pymusiclibrary-0.0.2-py3-abi3-win_arm64.whl (1.3 MB view details)

Uploaded Python 3Windows ARM64

pymusiclibrary-0.0.2-py3-abi3-win_amd64.whl (1.5 MB view details)

Uploaded Python 3Windows x86-64

pymusiclibrary-0.0.2-py3-abi3-win32.whl (1.3 MB view details)

Uploaded Python 3Windows x86

pymusiclibrary-0.0.2-py3-abi3-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded Python 3macOS 11.0+ x86-64

pymusiclibrary-0.0.2-py3-abi3-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 ca39df8c0650166bdc30ff7504ade481a69d9420682440b4247aa08070b921c3
MD5 7c730be5abfc32cb20f47742d18d4598
BLAKE2b-256 189685a45654992efc1ff4beac99479feeb09fa9bc3801851e1a716f3c17c5b3

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 cebc27cdf8527a9385b1e5fec2cf71ddd48a6c6d4889a8eefec696d2fde63ee3
MD5 d2521c3a2cc413075b4a5412ba3b7866
BLAKE2b-256 f41d97ef632cf41003bae53f636e1ad1f472feb942ef1227ef529afb2163e7e6

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-win32.whl.

File metadata

  • Download URL: pymusiclibrary-0.0.2-py3-abi3-win32.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-win32.whl
Algorithm Hash digest
SHA256 a552629c1cee6481dd458078415194a6140f1237e15de67e775677b73edc8c33
MD5 0281646283baf4f2b2d97f68bf282ce9
BLAKE2b-256 59370a3eff89ddea625ddfe81ef345e1176a058556797d7299520782f7be7dfd

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 74012e0376eeae0da20f87adb6aa4ff4906f68baee695562ca931e42c737fe4a
MD5 28568507b14475774c8b47c706c0ab12
BLAKE2b-256 f8da109a201dc9bfbf4d53f0093cf0b415ddf430f6f4c36f54a712d0d712ade2

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 40c4bbf235f34c040c6c41004202b89b93b671aaad33d78f9d7ad99bef363c1d
MD5 4e57356c90080e25496150b02ff65012
BLAKE2b-256 602ec9b5729f91dae177163f1f6ff86944020e1981923af9e26b1b91a49db735

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 bf0eb62adba9732e0a3be9e8464a8b840a698b85ba9ae7beadde2c3a84cf49c2
MD5 7e46b4e8a4f0d496cf008784534e6e1d
BLAKE2b-256 74a129f979d48de8400e7d080ef446463ade3f883d9faf68ed0a439b7f567098

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.2-py3-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.2-py3-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c52133b4d50ca7d29f4bd281da7cda8df7fedd9b0e94b7450582370cbc0d25bb
MD5 1ef00c3ff879b8f88fd3afd14c97043b
BLAKE2b-256 5616b5bb68c24d19cc036ab2ff36bfaf5e9f3d3e757ea82ab3a4fd40582a546f

See more details on using hashes here.

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