A Python interface to Last.fm and Libre.fm
Find a file
Eugene Simonov b4c8dc7282
Add type annotations to methods that take timestamp parameter (#442)
Co-authored-by: Eugene Simonov <eugene.simonov@evergen.com.au>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
2023-12-29 12:00:10 -07:00
.github Update actions/checkout action to v4 (#436) 2023-10-01 08:37:46 +03:00
src/pylast Add type annotations to methods that take timestamp parameter (#442) 2023-12-29 12:00:10 -07:00
tests Refactor: make helper methods static 2023-06-03 18:10:01 +03:00
.codecov.yml Codecov: Avoid "Missing base report" [CI skip] 2018-03-01 11:47:03 +02:00
.editorconfig Use actions/setup-python's pip cache 2021-11-21 17:44:49 +02:00
.flake8 Use hynek/build-and-inspect-python-package 2023-06-06 19:20:56 +03:00
.gitignore Ignore new '.pytest_cache' directory 2018-04-11 11:25:42 +03:00
.pre-commit-config.yaml [pre-commit.ci] pre-commit autoupdate (#437) 2023-10-02 14:16:48 -06:00
.scrutinizer.yml Add pypy3; track coverage in Scrutinizer 2014-12-26 17:16:41 +02:00
CHANGELOG.md New changes are documented in GH Releases 2021-04-30 22:53:37 +03:00
COPYING [pre-commit.ci] pre-commit autoupdate (#434) 2023-08-22 14:22:24 +03:00
example_test_pylast.yaml Rename test_pylast_example.yaml -> example_test_pylast.yaml (makes tab-autocompletion happier) 2014-03-03 08:59:36 +02:00
LICENSE.txt http -> https [CI skip] 2017-09-18 13:52:19 +03:00
pyproject.toml [pre-commit.ci] pre-commit autoupdate (#434) 2023-08-22 14:22:24 +03:00
pytest.ini Is an xfail passing unexpectedly? Make it fail 2022-01-24 21:15:11 +02:00
README.md Drop support for EOL Python 3.7 2023-06-03 17:41:35 +03:00
RELEASING.md Update link to deploy action 2022-01-31 12:50:28 +02:00
tox.ini Drop support for EOL Python 3.7 2023-06-03 17:41:35 +03:00

pyLast

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

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

Note:

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

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.