Skip to main content

A Python interface to Last.fm and Libre.fm

Project description

pyLast

PyPI version Supported Python versions PyPI downloads GitHub Actions status Codecov Licence DOI Code style: Black

A Python interface to Last.fm and other API-compatible websites such as Libre.fm.

Use the pydoc utility for help on usage or see tests/ for examples.

Installation

Install via pip:

python3 -m pip install pylast

Install latest development version:

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

Or from requirements.txt:

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

Features

  • Simple public interface.
  • Access to all the data exposed by the Last.fm web services.
  • Scrobbling support.
  • Full object-oriented design.
  • Proxy support.
  • Internal caching support for some web services calls (disabled by default).
  • Support for other API-compatible networks like Libre.fm.

Getting started

Here's some simple code example to get you started. In order to create any object from pyLast, you need a Network object which represents a social music network that is Last.fm or any other API-compatible one. You can obtain a pre-configured one for Last.fm and use it as follows:

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,
)

Alternatively, instead of creating network with a username and password, you can authenticate with a session key:

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

And away we go:

# 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

More examples in hugovk/lastfm-tools and tests/.

Testing

The tests/ directory contains integration and unit tests with Last.fm, and plenty of code examples.

For integration tests you need a test account at Last.fm that will become cluttered with test data, and an API key and secret. Either copy example_test_pylast.yaml to test_pylast.yaml and fill out the credentials, or set them as environment variables like:

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

To run all unit and integration tests:

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

Or run just one test case:

pytest -k test_scrobble

To run with coverage:

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

Logging

To enable from your own code:

import logging
import pylast

logging.basicConfig(level=logging.INFO)


network = pylast.LastFMNetwork(...)

To enable from pytest:

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

To also see data returned from the API, use level=logging.DEBUG or --log-cli-level debug instead.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylast-7.0.2.tar.gz (435.7 kB view details)

Uploaded Source

Built Distribution

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

pylast-7.0.2-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file pylast-7.0.2.tar.gz.

File metadata

  • Download URL: pylast-7.0.2.tar.gz
  • Upload date:
  • Size: 435.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pylast-7.0.2.tar.gz
Algorithm Hash digest
SHA256 825e2b5d9144c5491d9c353511169a1595813e6a1ad203faf7525cd2d1d1828e
MD5 01d5137315ca9e3fb1c11b0862299812
BLAKE2b-256 dc561729aa02df4bf959f31f6bd024775f8be0aaa08fd18a1d2bbdaab3c38e9e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylast-7.0.2.tar.gz:

Publisher: deploy.yml on pylast/pylast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylast-7.0.2-py3-none-any.whl.

File metadata

  • Download URL: pylast-7.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pylast-7.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c995e078670b3a8e3116a31b17d1f0d89c4d020407f6967ee9ffab2aeecd9de7
MD5 2cbf36c99052b759847590cbde19b338
BLAKE2b-256 0794677dade2b8ed48631de3fd34b320ebfd59b7a66f831640831112d7a7190b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylast-7.0.2-py3-none-any.whl:

Publisher: deploy.yml on pylast/pylast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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