Skip to main content

优雅的异步高性能 Python 音乐 API 库 (网易云音乐)

Project description

pycloudmusic

issues forks stars license

优雅的异步高性能 Python 音乐 API 库

开箱即用/简单/快速 Python >= 3.9

基于 NeteaseCloudMusicApi 改写

项目文档 (文档不更新时请清理浏览器缓存): https://docs.sakuratools.top/pycloudmusic/ | 打不开时候备用: https://github.com/FengLiuFeseliud/pycloudmusic.docs

快速入门

安装

使用 pip 即可安装 pycloudmusic

pip install pycloudmusic

输出对象

pycloudmusic 下的所有对象被打印时都会格式化输出类所有变量, 方便查看对象数据

from pycloudmusic import Music163Api
import asyncio


async def main():
    musicapi = Music163Api()
    # 获取歌曲 421531
    # https://music.163.com/#/song?id=421531
    music = await musicapi.music(421531)
    # 打印歌曲信息
    print(music)
    print("=" * 50)

asyncio.run(main())

此处数据为打印歌单对象

printobj.png

可迭代对象

pycloudmusic 下的部分对象可以被迭代使用

如下获取歌单 PlayList 对象后可以直接 for in PlayList 对象, 将迭代返回按歌单曲目生成的 Music 对象

from pycloudmusic import Music163Api
import asyncio


async def main():
    musicapi = Music163Api()
    # 获取歌单 7487291782
    # https://music.163.com/#/playlist?id=7487291782
    playlist = await musicapi.playlist(7487291782)
    # 打印歌单信息
    print(playlist)
    print("=" * 50)

    # 打印歌单曲目
    for music in playlist:
        print(music.name, music.artist_str, music.id)

asyncio.run(main())

因此可以使用推导式快速生成 task 列表下载歌单曲目

from pycloudmusic import Music163Api
import asyncio


async def main():
    musicapi = Music163Api()
    # 获取歌单 7487291782
    # https://music.163.com/#/playlist?id=7487291782
    playlist = await musicapi.playlist(7487291782)
    # 创建任务
    tasks = [asyncio.create_task(music.play()) for music in playlist]
    # 等待任务
    await asyncio.wait(tasks)

asyncio.run(main())

模拟登录

pycloudmusic 支持邮箱登录, 二维码登录, 手机密码登录, 手机验证码登录

这里可以用邮箱登录长期保持 Cookie 有效性,邮箱登录不需要人为操作

from pycloudmusic import LoginMusic163
import asyncio


async def main():
    login = LoginMusic163()
    # 邮箱登录
    code, cookie, musicapi = await login.email("you login email", "you login password")
    # 验证登录
    print("=" * 60)
    print(code, cookie, musicapi)
    print("=" * 60)
    print(await musicapi.my())

asyncio.run(main())

评论

pycloudmusic 支持网易云的所有评论, 对支持评论的对象使用 Music163Comment 的方法即可

"""获取歌曲评论"""

from pycloudmusic import Music163Api, Page
import asyncio


async def main():
    musicapi = Music163Api()
    # 获取歌曲
    # https://music.163.com/song?id=1902224491&userid=492346933
    music = await musicapi.music(1902224491)
    # 按时间获取评论
    async for comments in Page(music.comments, hot=False):
        for comment in comments:
            print(f"{comment.user_str}:  {comment.content}")

asyncio.run(main())

错误处理

API 返回 200 以外状态码触发 Music163BadCode 错误

错误对象.code 获取 API 状态码,错误对象.data 获取 API 数据

from pycloudmusic import Music163Api, Music163BadCode
import asyncio


async def main():
    musicapi = Music163Api()

    try:
        playlist = await musicapi.playlist(7487291782)
    except Music163BadCode as err:
        print(err, err.code, err.data)

asyncio.run(main())

代理设置

pycloudmusic 支持 proxy 时出现错误触发回调来更新 proxy

from pycloudmusic import Music163Api, set_proxy, set_proxy_callback
import asyncio

# 正常这里是多个 proxy ip
proxy = [
    "http://117.114.149.66:55443"
]

# 设置 proxy
set_proxy(proxy[0])

# proxy 更新回调
def proxy_callback(err):
    # 如果 proxy 时出现错误这里更新
    # 第一个返回值新的 proxy ip
    # 第二个返回值 aiohttp.BasicAuth proxy 身份验证, 没有为 None
    return proxy[0], None

# 设置 proxy 更新回调
set_proxy_callback(proxy_callback)

async def main():
    musicapi = Music163Api()
    # 获取歌曲 421531
    # https://music.163.com/#/song?id=421531
    music = await musicapi.music(421531)
    await music.play()
    # 打印歌曲信息
    print(music)
    print("=" * 50)

asyncio.run(main())

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

pycloudmusic-0.1.4.5.tar.gz (19.5 kB view details)

Uploaded Source

File details

Details for the file pycloudmusic-0.1.4.5.tar.gz.

File metadata

  • Download URL: pycloudmusic-0.1.4.5.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for pycloudmusic-0.1.4.5.tar.gz
Algorithm Hash digest
SHA256 e6653b760e5b85610ff66d62583b6544c24c984318e25f67dfcdd47adc8425c2
MD5 bb3efe7f139f8c9323f133f17ed278fa
BLAKE2b-256 3e1c98ff9dbe7096746b37d4e7eb0443fb8981859e9918342fe7a09c974d016b

See more details on using hashes here.

Supported by

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