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.
Export your scrobbles and loved tracks to JSON Lines, from Python or the command line.
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
pyLast can export a user's complete listening history, with MusicBrainz IDs, to a
JSON Lines file: one JSON object per line, easy to convert to
anything else. You only need an API key, not a password, for public profiles.
Scrobbles are written newest first, a page at a time, so an interrupted export can be
resumed by running the same command again: it continues from the oldest scrobble already
in the file. Dates are Unix timestamps or ISO 8601, and dates without a timezone are
UTC. See python -m pylast export --help for everything.
From Python:
import pylast
from pylast.export import export_scrobbles
network = pylast.LastFMNetwork(api_key=API_KEY)
network.enable_rate_limit()
user = network.get_user("your_username")
# Write everything to a file, resuming if the file already exists:
export_scrobbles(user, "your_username.jsonl")
# Or work with the records yourself; this streams one page at a time:
for scrobble in user.get_scrobbles():
print(scrobble.datetime, scrobble.artist, scrobble.title, scrobble.track_mbid)
Each record has timestamp, artist, title, album, artist_mbid, track_mbid,
album_mbid and url. user.get_loves() and export_loved() do the same for loved
tracks. Transient API errors are retried with exponential backoff.
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: