跳转到主要内容

Python接口,用于访问Last.fm和Libre.fm

项目描述

pyLast

PyPI version Supported Python versions PyPI downloads Test Coverage (Codecov) Code style: Black DOI

一个Python接口,用于访问Last.fm和其他API兼容网站,如Libre.fm

使用pydoc实用程序获取使用说明,或查看tests/中的示例。

安装

通过pip安装

python3 -m pip install pylast

安装最新开发版本

python3 -m pip install -U git+https://github.com/pylast/pylast

或从requirements.txt安装

-e https://github.com/pylast/pylast.git#egg=pylast

注意

  • pyLast 5.3+ 支持 Python 3.8-3.13。
  • pyLast 5.2+ 支持 Python 3.8-3.12。
  • pyLast 5.1 支持 Python 3.7-3.11。
  • pyLast 5.0 支持 Python 3.7-3.10。
  • pyLast 4.3 - 4.5 支持 Python 3.6-3.10。
  • pyLast 4.0 - 4.2 支持 Python 3.6-3.9。
  • pyLast 3.2 - 3.3 支持 Python 3.5-3.8。
  • pyLast 3.0 - 3.1 支持 Python 3.5-3.7。
  • pyLast 2.2 - 2.4 支持 Python 2.7.10+,3.4-3.7。
  • pyLast 2.0 - 2.1 支持 Python 2.7.10+,3.4-3.6。
  • pyLast 1.7 - 1.9 支持 Python 2.7,3.3-3.6。
  • pyLast 1.0 - 1.6 支持 Python 2.7,3.3-3.4。
  • pyLast 0.5 支持 Python 2,3。
  • pyLast < 0.5 支持 Python 2。

特性

  • 简单的公共接口。
  • 访问 Last.fm 网络服务公开的所有数据。
  • Scrobbling 支持。
  • 完全面向对象的设计。
  • 代理支持。
  • 某些网络服务调用的内部缓存支持(默认禁用)。
  • 支持其他 API 兼容网络,如 Libre.fm。

入门指南

以下是一些简单的代码示例以供您入门。为了从 pyLast 创建任何对象,您需要一个代表 Last.fm 或任何其他 API 兼容社交音乐网络的 Network 对象。您可以获取一个为 Last.fm 预配置的对象,并按如下方式使用它

import pylast

# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from https://www.last.fm/api/account/create for Last.fm
API_KEY = "b25b959554ed76058ac220b7b2e0a026"  # this is a sample key
API_SECRET = "425b55975eed76058ac220b7b4e8a054"

# In order to perform a write operation you need to authenticate yourself
username = "your_user_name"
password_hash = pylast.md5("your_password")

network = pylast.LastFMNetwork(
    api_key=API_KEY,
    api_secret=API_SECRET,
    username=username,
    password_hash=password_hash,
)

或者,您可以使用会话密钥进行认证,而不是使用用户名和密码创建 network

import pylast

SESSION_KEY_FILE = os.path.join(os.path.expanduser("~"), ".session_key")
network = pylast.LastFMNetwork(API_KEY, API_SECRET)
if not os.path.exists(SESSION_KEY_FILE):
    skg = pylast.SessionKeyGenerator(network)
    url = skg.get_web_auth_url()

    print(f"Please authorize this script to access your account: {url}\n")
    import time
    import webbrowser

    webbrowser.open(url)

    while True:
        try:
            session_key = skg.get_web_auth_session_key(url)
            with open(SESSION_KEY_FILE, "w") as f:
                f.write(session_key)
            break
        except pylast.WSError:
            time.sleep(1)
else:
    session_key = open(SESSION_KEY_FILE).read()

network.session_key = session_key

我们开始了

# Now you can use that object everywhere
track = network.get_track("Iron Maiden", "The Nomad")
track.love()
track.add_tags(("awesome", "favorite"))

# Type help(pylast.LastFMNetwork) or help(pylast) in a Python interpreter
# to get more help about anything and see examples of how it works

更多示例请参考 hugovk/lastfm-toolstests/

测试

tests/ 目录包含与 Last.fm 的集成和单元测试,以及大量的代码示例。

对于集成测试,您需要在 Last.fm 上有一个测试账户,该账户将因测试数据而变得杂乱,以及一个 API 密钥和密钥。您可以将 example_test_pylast.yaml 复制到 test_pylast.yaml 并填写凭据,或者将它们设置为环境变量,如下所示

export PYLAST_USERNAME=TODO_ENTER_YOURS_HERE
export PYLAST_PASSWORD_HASH=TODO_ENTER_YOURS_HERE
export PYLAST_API_KEY=TODO_ENTER_YOURS_HERE
export PYLAST_API_SECRET=TODO_ENTER_YOURS_HERE

运行所有单元和集成测试

python3 -m pip install -e ".[tests]"
pytest

或者运行单个测试用例

pytest -k test_scrobble

要使用覆盖率运行

pytest -v --cov pylast --cov-report term-missing
coverage report # for command-line report
coverage html   # for HTML report
open htmlcov/index.html

日志记录

从您的代码中启用

import logging
import pylast

logging.basicConfig(level=logging.INFO)


network = pylast.LastFMNetwork(...)

从 pytest 中启用

pytest --log-cli-level info -k test_album_search_images

要查看 API 返回的数据,请使用 level=logging.DEBUG--log-cli-level debug

项目详情


发布历史 发布通知 | RSS 源

下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

pylast-5.3.0.tar.gz (41.6 kB 查看哈希值)

上传时间 源代码

构建分发

pylast-5.3.0-py3-none-any.whl (29.1 kB 查看哈希值)

上传时间 Python 3

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面