Package: pylast
pyLastA Python interface toLast.fmand other API-compatible websites
such asLibre.fm.Use the pydoc utility for help on usage or seetests/for examples.InstallationInstall via pip:python3-mpipinstallpylastInstall latest development version:python3-mpipinstall-Ugit+https://github.com/pylast/pylastOr from requirements.txt:-e https://github.com/pylast/pylast.git#egg=pylastNote: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.FeaturesSimple 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 startedHere's some simple code example to get you started. In order to create any object from
pyLast, you need aNetworkobject 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:importpylast# 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.fmAPI_KEY="b25b959554ed76058ac220b7b2e0a026"# this is a sample keyAPI_SECRET="425b55975eed76058ac220b7b4e8a054"# In order to perform a write operation you need to authenticate yourselfusername="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 creatingnetworkwith a username and password,
you can authenticate with a session key:importpylastSESSION_KEY_FILE=os.path.join(os.path.expanduser("~"),".session_key")network=pylast.LastFMNetwork(API_KEY,API_SECRET)ifnotos.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")importtimeimportwebbrowserwebbrowser.open(url)whileTrue:try:session_key=skg.get_web_auth_session_key(url)withopen(SESSION_KEY_FILE,"w")asf:f.write(session_key)breakexceptpylast.WSError:time.sleep(1)else:session_key=open(SESSION_KEY_FILE).read()network.session_key=session_keyAnd away we go:# Now you can use that object everywheretrack=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 worksMore examples inhugovk/lastfm-toolsandtests/.TestingThetests/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 copyexample_test_pylast.yamlto test_pylast.yaml and fill out
the credentials, or set them as environment variables like:exportPYLAST_USERNAME=TODO_ENTER_YOURS_HEREexportPYLAST_PASSWORD_HASH=TODO_ENTER_YOURS_HEREexportPYLAST_API_KEY=TODO_ENTER_YOURS_HEREexportPYLAST_API_SECRET=TODO_ENTER_YOURS_HERETo run all unit and integration tests:python3-mpipinstall-e".[tests]"pytestOr run just one test case:pytest-ktest_scrobbleTo run with coverage:pytest-v--covpylast--cov-reportterm-missing
coveragereport# for command-line reportcoveragehtml# for HTML reportopenhtmlcov/index.htmlLoggingTo enable from your own code:importloggingimportpylastlogging.basicConfig(level=logging.INFO)network=pylast.LastFMNetwork(...)To enable from pytest:pytest--log-cli-levelinfo-ktest_album_search_imagesTo also see data returned from the API, uselevel=logging.DEBUGor--log-cli-level debuginstead.