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 musiclibrary

从源码安装

从源码安装需要先构建 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.1-py3-cp311-win_arm64.whl (1.3 MB view details)

Uploaded Python 3Windows ARM64

pymusiclibrary-0.0.1-py3-cp311-win_amd64.whl (1.5 MB view details)

Uploaded Python 3Windows x86-64

pymusiclibrary-0.0.1-py3-cp311-win32.whl (1.3 MB view details)

Uploaded Python 3Windows x86

pymusiclibrary-0.0.1-py3-cp311-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded Python 3macOS 11.0+ x86-64

pymusiclibrary-0.0.1-py3-cp311-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.1-py3-cp311-win_arm64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.1-py3-cp311-win_arm64.whl
Algorithm Hash digest
SHA256 af0a804d3695d250a90d6b4ee3acbd507fc413348500c711b66f11e499da71c5
MD5 431b4f1cc3fda1b24eb6676bb9a86bb4
BLAKE2b-256 982819c01bb2991d34eebb76b7928722d7adf3f293928f366b70cc69dd6c4151

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.1-py3-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.1-py3-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 58d72236ad8f0b493f2045bd4bdb7b69f5cb9db75d0ab1d437c0e508a3825cd0
MD5 e35aff110df734f984986a4a97bc2e14
BLAKE2b-256 ed1ca9ad16d9ff2856aca49d72eaec137b5562b2c58a892d7044b1ce9d972034

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.1-py3-cp311-win32.whl.

File metadata

  • Download URL: pymusiclibrary-0.0.1-py3-cp311-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.1-py3-cp311-win32.whl
Algorithm Hash digest
SHA256 1f1991c3e1a2dd332eb83cbda62ed01ae7f2851793e91c8b8c4bed0db11f6c36
MD5 38e763a721925d28a9a9f38558ff41b4
BLAKE2b-256 260ee3b9c5f90f275deed740e9f876c44e99666e1cbe2db20e4ac2ff4920d2e8

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.1-py3-cp311-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.1-py3-cp311-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 9c84bd2e957149a87797165483bd9990b1a6884a07d3c4bdf7a722f9748f69a4
MD5 b9099967b8b76d54c1f28ceaa214db8b
BLAKE2b-256 47b8ff5039c950ecbb8188c3b7177aa6c3ff0c981ef60270d22ddc68ff930d4c

See more details on using hashes here.

File details

Details for the file pymusiclibrary-0.0.1-py3-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pymusiclibrary-0.0.1-py3-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b8d6e928ef6778218b621713ddbbcbe6050da5669957f1a6cdff539edee097ff
MD5 9ef743966957705f0590f598a5201528
BLAKE2b-256 a4b3e3c2cd068733699ce82498884697c6461f3dfaf1dec8d6ff89b4fc0fdf38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymusiclibrary-0.0.1-py3-abi3-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 199fe6a4f300261b6e17b8b7a613508312d95bc24bdf0085af7ae6f843895d6b
MD5 8d95af3ce3569aa98b0d0a5749df4fed
BLAKE2b-256 80f5eaf8253953a044c8f419e4fae5219a4ed7bb90d3b47dde32c3f9c948b88a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymusiclibrary-0.0.1-py3-abi3-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0836fa73bc00899e856c86e94556d0c7abc31e424014db96585752b97b190434
MD5 124bf80b7e5c7acfc6b0dedf321e0949
BLAKE2b-256 42312d082cd735115072acd91099945ece38f89de2fcd3015b3ffd9807c5fa43

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